<?php

namespace App\Models;

use CodeIgniter\Model;

class ExpenseModel extends Model
{
    protected $table = 'expenses';
    protected $primaryKey = 'id';

    protected $allowedFields = [
        'category',
        'amount',
        'receipt_path',
        'description',
        'retailor',
        'date_of_purchase',
        'purchased_by',
        'added_by',
        'school_year',
        'semester',
        'status',
        'status_reason',
        'reimbursement_id',
        'approved_by',
        'updated_by'
    ];

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

    protected $validationRules = [
        // Add your validation rules here as needed
    ];

    public function getReimbursedExpensesWithDetails(array $filters = [])
    {
        $db = \Config\Database::connect();
        $builder = $db->table('expenses');
        $hasReimbursementSchoolYear = $db->fieldExists('school_year', 'reimbursements');
        $hasReimbursementSemester = $db->fieldExists('semester', 'reimbursements');
        $hasExpenseSchoolYear = $db->fieldExists('school_year', 'expenses');
        $hasExpenseSemester = $db->fieldExists('semester', 'expenses');

        $select = [
            'expenses.*',
            'r.id AS reimb_id',
            'r.amount AS reimb_amount',
            'r.reimbursement_method',
            'r.check_number',
            'r.receipt_path AS reimb_receipt',
            'r.status AS reimb_status',
            'r.created_at AS reimb_created_at',
            'r.reimbursed_to AS reimb_recipient_id',
            'reimb.firstname AS reimb_firstname',
            'reimb.lastname AS reimb_lastname',
            'approver.firstname AS approver_firstname',
            'approver.lastname AS approver_lastname',
        ];

        if ($hasReimbursementSchoolYear) {
            $select[] = 'r.school_year';
        } elseif ($hasExpenseSchoolYear) {
            $select[] = 'expenses.school_year AS school_year';
        }

        if ($hasReimbursementSemester) {
            $select[] = 'r.semester';
        } elseif ($hasExpenseSemester) {
            $select[] = 'expenses.semester AS semester';
        } else {
            $select[] = 'NULL AS semester';
        }

        $builder->select(implode(",\n", $select), false);
        $builder->join('reimbursements r', 'r.expense_id = expenses.id', 'inner');
        $builder->join('users reimb', 'reimb.id = r.reimbursed_to', 'left');
        $builder->join('users approver', 'approver.id = r.approved_by', 'left');

        if (!empty($filters['school_year'])) {
            if ($hasReimbursementSchoolYear) {
                $builder->where('r.school_year', $filters['school_year']);
            } elseif ($hasExpenseSchoolYear) {
                $builder->where('expenses.school_year', $filters['school_year']);
            }
        }
        if (!empty($filters['semester'])) {
            if ($hasReimbursementSemester) {
                $builder->where('r.semester', $filters['semester']);
            } elseif ($hasExpenseSemester) {
                $builder->where('expenses.semester', $filters['semester']);
            }
        }
        if (!empty($filters['status'])) {
            $builder->where('r.status', $filters['status']);
        }
        if (!empty($filters['user_id'])) {
            $builder->where('r.reimbursed_to', $filters['user_id']);
        }

        $builder->orderBy('r.created_at', 'DESC');

        return $builder;
    }
}
