<?php

namespace App\Models;

use CodeIgniter\Model;

class EventModel extends Model
{
    protected $table = 'events';
    protected $primaryKey = 'id';

    protected $allowedFields = [
        'event_name',
        'event_category',
        'description',
        'amount',
        'flyer',
        'expiration_date',
        'semester',
        'school_year',
        'created_by',
        'created_at',
        'updated_at'
    ];

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

    /**
     * Get all upcoming events (not expired).
     *
     * @return array
     */
public function getUpcomingEvents($schoolYear = null)
{
    $builder = $this->where('expiration_date >=', date('Y-m-d'));

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

    return $builder->orderBy('expiration_date', 'ASC')->findAll();
}

/**
 * Get events by name (partial match).
 *
 * @param string $name
 * @param string|null $schoolYear
 * @return array
 */
public function getEventsByName(string $name, $schoolYear = null)
{
    $builder = $this->like('event_name', $name);

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

    return $builder->orderBy('expiration_date', 'ASC')->findAll();
}

/**
 * Get all events.
 *
 * @param string|null $schoolYear
 * @return array
 */
public function getAllEvents($schoolYear = null)
{
    $builder = $this;

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

    return $builder->orderBy('created_at', 'DESC')->findAll();
}

/**
 * Add a new event.
 *
 * @param array $data
 * @return int|string|false
 */
public function addEvent(array $data)
{
    return $this->insert($data);
}

/**
 * Get events by expiration date.
 *
 * @param string $date
 * @param string|null $schoolYear
 * @return array
 */
public function getEventsByDate($date, $schoolYear = null)
{
    $builder = $this->where('expiration_date', $date);

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

    return $builder->findAll();
}


/**
 * Get all active (not expired) events.
 *
 * @param string|null $schoolYear
 * @param string|null $semester
 * @return array
 */
public function getActiveEvents($schoolYear = null, $semester = null)
{
    $builder = $this->where('expiration_date >=', date('Y-m-d'));

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

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

    return $builder->orderBy('expiration_date', 'ASC')->findAll();
}

/**
 * Get event by ID.
 *
 * @param int $id
 * @param string|null $schoolYear
 * @return array|null
 */
public function getEvent($id, $schoolYear = null)
{
    $builder = $this->where('id', $id);

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

    return $builder->first();
}

/**
 * Update event by ID.
 *
 * @param int $id
 * @param array $data
 * @param string|null $schoolYear
 * @return bool
 */
public function updateEvent($id, $data, $schoolYear = null)
{
    if ($schoolYear) {
        // Ensure only updates for this school year
        $this->where('id', $id)->where('school_year', $schoolYear)->set($data)->update();
        return true;
    }

    return $this->update($id, $data);
}

/**
 * Delete event by ID.
 *
 * @param int $id
 * @param string|null $schoolYear
 * @return bool
 */
public function deleteEvent($id, $schoolYear = null)
{
    if ($schoolYear) {
        return $this->where('id', $id)->where('school_year', $schoolYear)->delete();
    }

    return $this->delete($id);
}

}
