<?php

namespace App\Http\Controllers\Api\V1\Website;

use App\Domain\Website\Models\CompanyCertificate;
use Illuminate\Database\Eloquent\Model;

class CmsCertificateController extends CmsResourceController
{
    protected function model(): string
    {
        return CompanyCertificate::class;
    }

    protected function rules(bool $creating): array
    {
        return [
            'name' => [$creating ? 'required' : 'sometimes', 'string', 'max:255'],
            'issuing_authority' => ['nullable', 'string', 'max:255'],
            'certificate_number' => ['nullable', 'string', 'max:100'],
            'issue_date' => ['nullable', 'date'],
            'expiry_date' => ['nullable', 'date', 'after_or_equal:issue_date'],
            'description' => ['nullable', 'string', 'max:2000'],
            'is_published' => ['sometimes', 'boolean'],
            'sort_order' => ['sometimes', 'integer', 'min:0', 'max:9999'],
        ];
    }

    protected function present(Model $record): array
    {
        return [
            'id' => $record->uuid,
            'name' => $record->name,
            'issuing_authority' => $record->issuing_authority,
            'certificate_number' => $record->certificate_number,
            'issue_date' => $record->issue_date?->toDateString(),
            'expiry_date' => $record->expiry_date?->toDateString(),
            'has_expired' => $record->hasExpired(),
            'description' => $record->description,
            'is_published' => $record->is_published,
            'sort_order' => $record->sort_order,
        ];
    }
}
