<?php

namespace App\Models;

use CodeIgniter\Model;

class LegerImportModel extends Model
{
    protected $table            = 'leger_imports';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $returnType       = 'array';
    protected $useSoftDeletes   = false;
    protected $allowedFields    = [
        'batch_code', 'classroom_id', 'academic_period_id', 'file_name',
        'class_name_raw', 'semester_raw', 'year_raw',
        'total_students', 'total_subjects', 'imported_by',
    ];

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

    public function getAllWithRelations()
    {
        return $this->select('
                leger_imports.*,
                classrooms.name      as classroom_name,
                grades.name          as grade_name,
                academic_periods.semester,
                academic_years.name  as year_name,
                users.name           as imported_by_name
            ')
            ->join('classrooms',      'classrooms.id = leger_imports.classroom_id',               'left')
            ->join('grades',          'grades.id = classrooms.grade_id',                          'left')
            ->join('academic_periods','academic_periods.id = leger_imports.academic_period_id',   'left')
            ->join('academic_years',  'academic_years.id = academic_periods.academic_year_id',    'left')
            ->join('users',           'users.id = leger_imports.imported_by',                     'left')
            ->orderBy('academic_years.name', 'DESC')
            ->orderBy('academic_periods.semester', 'ASC')
            ->orderBy('grades.order', 'ASC')
            ->orderBy('classrooms.name', 'ASC')
            ->findAll();
    }
}