<?php

namespace App\Models;

use CodeIgniter\Model;

class EventChargesModel extends Model
{
    protected $table = 'event_charges';
    protected $primaryKey = 'id';

    protected $allowedFields = [
        'event_id',
        'parent_id',
        'student_id',
        'participation',
        'charged',
        'waiver_signed',
        'event_paid',
        'event_payment_id',
        'class_section_id',
        'external_firstname',
        'external_lastname',
        'external_note',
        'external_parent_firstname',
        'external_parent_lastname',
        'external_parent_phone',
        'external_parent_email',
        'semester',
        'school_year',
        'updated_by',
        'created_at',
        'updated_at'
    ];

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

    protected $returnType = 'array'; // Or 'object' if preferred

    public function getChargesWithEventInfo($parentId = null, $schoolYear = null, $semester = null)
    {
        $builder = $this->select('event_charges.*, events.event_name, events.amount AS event_amount, events.description AS event_description')
                        ->join('events', 'events.id = event_charges.event_id', 'left');

        if ($parentId) {
            $builder->where('event_charges.parent_id', $parentId);
        }

        if ($schoolYear) {
            $builder->where('event_charges.school_year', $schoolYear);
        }

        if ($semester) {
            $builder->where('event_charges.semester', $semester);
        }

        return $builder->findAll();
    }


}
