<?php


namespace App\Models;

use CodeIgniter\Model;

class ContactUsModel extends Model
{
    protected $table            = 'contactus';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;

    protected $returnType       = 'array';
    protected $useSoftDeletes   = false;

    protected $allowedFields    = [
        'sender_id',
        'reciever_id',
        'subject',
        'message',
        'created_at',
        'updated_at'
    ];

    // Timestamps (set manually or use auto timestamps below)
    protected $useTimestamps = true;
    protected $createdField  = 'created_at';
    protected $updatedField  = 'updated_at';

    protected $validationRules = [
        'sender_id'    => 'required|integer',
        'reciever_id'  => 'required|integer',
        'subject'      => 'required|string|max_length[255]',
        'message'      => 'required|string',
    ];

    protected $validationMessages = [];
    protected $skipValidation     = false;

    /**
     * Retrieve messages by semester and school year.
     *
     * @param string $semester
     * @param string $school_year
     * @return array
     */
    public function getMessagesBySemesterAndYear($semester, $school_year)
    {
        return $this->findAll();
    }

    /**
     * Retrieve messages for a specific sender or receiver.
     *
     * @param int $userId
     * @return array
     */
    public function getMessagesForUser($userId)
    {
        return $this->where('sender_id', $userId)
            ->orWhere('reciever_id', $userId)
            ->findAll();
    }

    /**
     * Retrieve a message by ID.
     *
     * @param int $id
     * @return array|null
     */
    public function getMessageById($id)
    {
        return $this->find($id);
    }

    /**
     * Update a message by ID.
     *
     * @param int $id
     * @param array $data
     * @return bool
     */
    public function updateMessage($id, $data)
    {
        return $this->update($id, $data);
    }
}
