<?php

namespace App\Models;

use App\Models\BaseModel;
use Illuminate\Support\Facades\DB;

class Expense extends BaseModel
{
    protected $table = 'expenses';

    // ✅ CI: useTimestamps = true (created_at/updated_at)
    public $timestamps = true;

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

    protected $casts = [
        'amount'          => 'decimal:2', // adjust precision if needed
        'date_of_purchase'=> 'date',      // change to 'datetime' if stored as DATETIME

        'purchased_by'    => 'integer',
        'added_by'        => 'integer',
        'reimbursement_id'=> 'integer',
        'approved_by'     => 'integer',
        'updated_by'      => 'integer',
    ];

    /* Optional relationships */
    public function reimbursement()
    {
        return $this->hasOne(Reimbursement::class, 'expense_id');
    }

    public function purchaser()
    {
        return $this->belongsTo(User::class, 'purchased_by');
    }

    public function addedBy()
    {
        return $this->belongsTo(User::class, 'added_by');
    }

    /**
     * Laravel equivalent of getReimbursedExpensesWithDetails().
     *
     * Returns a Query\Builder (so you can add paginate(), get(), etc.)
     *
     * Filters supported:
     * - school_year
     * - semester
     * - status
     * - user_id (reimbursed_to)
     */
    public static function getReimbursedExpensesWithDetails(array $filters = [])
    {
        $q = DB::table('expenses')
            ->selectRaw("
                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.school_year, r.semester, 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
            ")
            ->join('reimbursements as r', 'r.expense_id', '=', 'expenses.id')
            ->leftJoin('users as reimb', 'reimb.id', '=', 'r.reimbursed_to')
            ->leftJoin('users as approver', 'approver.id', '=', 'r.approved_by');

        if (!empty($filters['school_year'])) {
            $q->where('r.school_year', $filters['school_year']);
        }
        if (!empty($filters['semester'])) {
            $q->where('r.semester', $filters['semester']);
        }
        if (!empty($filters['status'])) {
            $q->where('r.status', $filters['status']);
        }
        if (!empty($filters['user_id'])) {
            $q->where('r.reimbursed_to', $filters['user_id']);
        }

        return $q->orderByDesc('r.created_at');
    }
}