<?php

namespace App\Models;

use CodeIgniter\Model;

class AscImportModel extends Model
{
    protected $table            = 'asc_imports';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $returnType       = 'array';
    protected $useSoftDeletes   = false;
    protected $allowedFields    = [
        'name', 'academic_year', 'semester', 'filename',
        'is_active', 'total_schedules', 'imported_by', 'notes'
    ];

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

    // Ambil semua import, terbaru di atas
    public function getAll()
    {
        return $this->orderBy('created_at', 'DESC')->findAll();
    }

    // Ambil import yang sedang aktif
    public function getActive()
    {
        return $this->where('is_active', 1)->first();
    }

    // Set satu import sebagai aktif, nonaktifkan yang lain
    public function setActive($id)
    {
        // Nonaktifkan semua (gunakan db->table agar tidak kena proteksi WHERE)
        $this->db->table($this->table)->update(['is_active' => 0]);

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

    

    // Update total jadwal setelah proses import
    public function updateTotalSchedules($id, $total)
    {
        return $this->update($id, ['total_schedules' => $total]);
    }
}