<?php

namespace App\Controllers\View;

use App\Controllers\BaseController;
use App\Models\AttendanceRecordModel;
use App\Models\BadgePrintLogModel;
use App\Models\ClassSectionModel;
use App\Models\ConfigurationModel;
use App\Models\ScoreCommentModel;
use App\Models\SemesterScoreModel;
use App\Models\StaffModel;
use App\Models\StudentClassModel;
use App\Models\StudentModel;
use App\Models\TeacherClassModel;
use App\Models\UserModel;

require_once APPPATH . 'ThirdParty/fpdf/fpdf.php';

class PrintablesBaseController extends BaseController
{
    protected $userModel;
    protected $studentModel;
    protected $db;
    protected $classSectionModel;
    protected $configModel;
    protected $schoolYear;
    protected $semester;
    protected $staffModel;
    protected $teacherClassModel;
    protected $semesterScoreModel;
    protected $studentClassModel;
    protected $stickerWidth;
    protected $stickerHeight;
    protected $pageW;
    protected $pageH;
    protected $marginL;
    protected $marginT;
    protected $gapX;
    protected $gapY;
    protected $badgePrintLogModel;
    protected $scoreCommentModel;
    protected $attendanceRecordModel;

    public function __construct()
    {
        $this->db = \Config\Database::connect();
        $this->userModel = new UserModel();
        $this->studentModel = new StudentModel();
        $this->classSectionModel = new ClassSectionModel();
        $this->configModel = new ConfigurationModel();
        $this->semesterScoreModel = new SemesterScoreModel();
        $this->studentClassModel = new StudentClassModel();
        $this->teacherClassModel = new TeacherClassModel();
        $this->staffModel = new StaffModel();
        $this->badgePrintLogModel = new BadgePrintLogModel();
        $this->scoreCommentModel = new ScoreCommentModel();
        $this->attendanceRecordModel = new AttendanceRecordModel();

        $this->schoolYear = $this->configModel->getConfig('school_year');
        $this->semester = $this->configModel->getConfig('semester');
        $this->stickerWidth  = $this->configModel->getConfig('stickerWidth');
        $this->stickerHeight = $this->configModel->getConfig('stickerHeight');
        $this->pageW = $this->configModel->getConfig('pageWidth');
        $this->pageH = $this->configModel->getConfig('pageHeight');
        $this->marginL = $this->configModel->getConfig('leftMargin');
        $this->marginT = $this->configModel->getConfig('topMargin');
        $this->gapX = $this->configModel->getConfig('horizontalGap');
        $this->gapY = $this->configModel->getConfig('verticalGap');
    }

    protected function firstExisting(array $paths)
    {
        foreach ($paths as $p) {
            if (is_string($p) && file_exists($p)) {
                return $p;
            }
        }
        return null;
    }

    protected function resolveClassName($db, $classId)
    {
        $tables = [
            ['classSection', 'class_section_id'],
            ['classSection', 'id'],
            ['class_sections', 'class_section_id'],
            ['class_sections', 'id'],
        ];

        foreach ($tables as [$table, $pk]) {
            $result = $db->table($table)
                ->select('class_section_name')
                ->where($pk, $classId)
                ->get()
                ->getRowArray();

            if ($result) {
                return $result['class_section_name'] ?? null;
            }
        }

        return null;
    }

    protected function fetchStudentsByClass(int $sectionId, ?string $schoolYear)
    {
        $b = $this->studentClassModel
            ->select('s.id, s.firstname, s.lastname, s.registration_grade, s.gender')
            ->join('students s', 's.id = student_class.student_id', 'inner')
            ->where('student_class.class_section_id', $sectionId)
            ->orderBy('s.lastname', 'ASC');

        if (!empty($schoolYear)) {
            $b->where('student_class.school_year', $schoolYear);
        }

        return $b->findAll();
    }
}
