<?php

namespace App\Models;

use CodeIgniter\Model;

class AcademicYearModel extends Model
{
    protected $table            = 'academic_years';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $returnType       = 'array';
    protected $useSoftDeletes   = true;
    protected $allowedFields    = [
        'name', 'start_date', 'end_date', 'is_active'
    ];

    protected $useTimestamps = true;
    protected $createdField  = 'created_at';
    protected $updatedField  = 'updated_at';
    protected $deletedField  = 'deleted_at';

    public function getActive()
    {
        return $this->where('is_active', 1)->first();
    }

    public function setActive($id)
    {
        // Nonaktifkan semua dulu
        $this->db->table('academic_years')
                ->where('deleted_at', null)
                ->update(['is_active' => 0]);

        // Aktifkan yang dipilih
        return $this->db->table('academic_years')
                        ->where('id', $id)
                        ->update(['is_active' => 1]);
    }
}