<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class AcademicYear extends Model
{
    protected $fillable = [
        'name',
        'start_date',
        'end_date',
        'is_active',
        'description',
    ];

    protected $casts = [
        'start_date' => 'date',
        'end_date' => 'date',
        'is_active' => 'boolean',
    ];

    /**
     * Get the active academic year
     */
    public static function getActive()
    {
        return self::where('is_active', true)->first();
    }

    /**
     * Activate this academic year and deactivate others
     */
    public function activate()
    {
        // Deactivate all other academic years
        self::where('id', '!=', $this->id)->update(['is_active' => false]);
        
        // Activate this academic year
        $this->update(['is_active' => true]);
    }
}
