<?php

namespace App\Models;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Link;
use ApiPlatform\Metadata\QueryParameter;
use ApiPlatform\OpenApi\Model\Operation;
use ApiPlatform\OpenApi\Model\Response;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use ApiPlatform\Laravel\Eloquent\Filter\EqualsFilter;

#[ApiResource(
    description: 'French departments reference data (100 departments)',
    operations: [
        new GetCollection(
            openapi: new Operation(
                summary: 'List all French departments',
                description: 'Returns all 100 French departments with their name and region.',
                responses: [
                    '200' => new Response(description: 'Collection of departments'),
                ],
            ),
        ),
        new Get(
            uriTemplate: '/departments/{department_id}',
            uriVariables: ['department_id' => new Link(fromClass: Department::class, identifiers: ['department_id'])],
            openapi: new Operation(
                summary: 'Get a single department by its code',
                description: 'Returns a department with its name and region.',
                responses: [
                    '200' => new Response(description: 'Department details'),
                    '404' => new Response(description: 'Department not found'),
                ],
            ),
        ),
    ]
)]
#[QueryParameter(key: 'department_id', description: 'Filter by department code (e.g. 76, 75, 13)', filter: EqualsFilter::class)]
#[QueryParameter(key: 'department_name', description: 'Filter by department name', filter: EqualsFilter::class)]
#[QueryParameter(key: 'region', description: 'Filter by region name (e.g. Normandie)', filter: EqualsFilter::class)]
class Department extends Model
{
    use HasFactory;
    protected $primaryKey = 'department_id';
    public $incrementing = false;
    protected $keyType = 'string';

    protected $fillable = [
        'department_id',
        'department_name',
        'region_name'
    ];

    protected $hidden = ['taxes'];

    protected $table = 'departments';
    public $timestamps = false;

    /**
     * Get the taxes for the department.
     */
    public function taxes()
    {
        return $this->hasMany(Taxe::class, 'department_id', 'department_id');
    }
}
