<?php

namespace App\Http\Controllers\Api;

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

class UpcomingInspectionController extends BaseApiController
{
    protected string $model = UpcomingInspection::class;
    protected array $with = ['project'];
    protected array $searchable = ['inspectionType', 'inspector'];
    protected array $filters = ['status' => 'status', 'project' => 'project_id'];

    protected function rules(Request $request, ?Model $model = null): array
    {
        $required = $model ? 'sometimes' : 'required';
        return [
            'inspectionType' => "$required|string",
            'project' => 'nullable|string|exists:projects,id',
            'inspector' => 'nullable|string',
            'scheduledDate' => 'nullable|date',
            'status' => 'nullable|string',
            'notes' => '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;
    }
}
