<?php

namespace App\Models;

use App\Enums\Semester;
use Illuminate\Database\Eloquent\Attributes\Appends;
use Illuminate\Database\Eloquent\Attributes\Guarded;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

#[Guarded(['id'])]
#[Appends(['formatted_start_date', 'formatted_end_date'])]
class AcademicTerm extends Model
{
    use HasFactory;

    public function tuitionInvoices(): HasMany
    {
        return $this->hasMany(TuitionInvoice::class);
    }

    protected function casts(): array
    {
        return [
            'semester' => Semester::class,
            'start_date' => 'date',
            'end_date' => 'date',
            'is_active' => 'boolean',
        ];
    }

    protected function formattedStartDate(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->start_date->format('d M Y'),
        );
    }

    protected function formattedEndDate(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->end_date->format('d M Y'),
        );
    }
}
