<?php

namespace App\Models;

use App\Enums\AustralianState;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Town extends Model
{
    /** @use HasFactory<\Database\Factories\TownFactory> */
    use HasFactory;

    protected $fillable = [
        'town_pid',
        'town_name',
        'state',
        'population',
        'town_class',
        'date_retired',
    ];

    protected function casts(): array
    {
        return [
            'state' => AustralianState::class,
            'date_retired' => 'datetime',
        ];
    }

    public function scopeActive(Builder $query): void
    {
        $query->whereNull('date_retired');
    }

    public function scopeSearch(Builder $query, string $term): void
    {
        $query->where('town_name', 'like', $term.'%');
    }
}
