<?php

namespace App\Http\Controllers\Api;

use App\Models\UpcomingSafety;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class UpcomingSafetyController extends BaseApiController
{
    protected string $model = UpcomingSafety::class;
    protected array $with = ['project'];
    protected array $searchable = ['title', 'instructor'];
    protected array $filters = ['status' => 'status', 'project' => 'project_id'];

    protected function rules(Request $request, ?Model $model = null): array
    {
        $required = $model ? 'sometimes' : 'required';
        return [
            'title' => "$required|string",
            'description' => 'nullable|string',
            'scheduledDate' => 'nullable|date',
            'instructor' => 'nullable|string',
            'project' => 'nullable|string|exists:projects,id',
            'attendees' => 'nullable|integer|min:0',
            'status' => 'nullable|string',
        ];
    }

    protected function transform(Request $request, array $data): array
    {
        if (array_key_exists('project', $data)) {
            $data['project_id'] = $data['project'];
            unset($data['project']);
        }
        return $data;
    }
}
