<?php

namespace App\Http\Controllers\Api;

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

class CertificationController extends BaseApiController
{
    protected string $model = Certification::class;
    protected array $with = ['vendor'];
    protected array $searchable = ['documentName', 'documentType'];
    protected array $filters = ['status' => 'status', 'vendor' => 'vendor_id'];

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

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