<?php

namespace App\Http\Controllers\Api;

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

class ComplianceController extends BaseApiController
{
    protected string $model = Compliance::class;
    protected array $with = ['project'];
    protected array $searchable = ['name', 'type', 'authority'];
    protected array $filters = ['status' => 'status', 'type' => 'type', 'project' => 'project_id'];

    protected function rules(Request $request, ?Model $model = null): array
    {
        $required = $model ? 'sometimes' : 'required';
        return [
            'name' => "$required|string",
            'type' => 'nullable|string',
            'project' => 'nullable|string|exists:projects,id',
            'authority' => 'nullable|string',
            'issueDate' => 'nullable|date',
            'expiryDate' => 'nullable|date',
            'status' => 'nullable|string',
            'description' => 'nullable|string',
            'image' => 'nullable',
        ];
    }

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