<?php

namespace App\Models;

use CodeIgniter\Model;

class MessageModel extends Model
{
    protected $table = 'messages';
    protected $primaryKey = 'id';

    protected $allowedFields = [
        'sender_id',
        'recipient_id',
        'subject',
        'message',
        'sent_datetime',
        'read_status',
        'read_datetime',
        'message_number',
        'priority',
        'attachment',
        'status',
        'semester'
    ];

    protected $useTimestamps = false; // Since you're manually handling date fields
    protected $returnType = 'array';

    /**
     * Get unread messages for a specific recipient.
     *
     * @param int $recipientId
     * @return array
     */
    public function getUnreadMessages($recipientId)
    {
        return $this->where('recipient_id', $recipientId)
            ->where('read_status', 0)
            ->orderBy('sent_datetime', 'DESC')
            ->findAll();
    }

    /**
     * Mark a message as read and update the read_datetime.
     *
     * @param int $messageId
     * @return bool
     */
    public function markAsRead($messageId)
    {
        return $this->update($messageId, [
            'read_status' => 1,
            'read_datetime' => utc_now() // Set the current datetime when marked as read
        ]);
    }

    /**
     * Get messages by priority.
     *
     * @param string $priority
     * @return array
     */
    public function getMessagesByPriority($priority = 'normal')
    {
        return $this->where('priority', $priority)
            ->orderBy('sent_datetime', 'DESC')
            ->findAll();
    }

    /**
     * Get messages by status.
     *
     * @param string $status
     * @return array
     */
    public function getMessagesByStatus($status = 'sent')
    {
        return $this->where('status', $status)
            ->orderBy('sent_datetime', 'DESC')
            ->findAll();
    }

    /**
     * Get inbox messages for a user.
     *
     * @param int $userId
     * @return array
     */
    public function getInboxMessages($userId)
    {
        return $this->where('recipient_id', $userId)
            ->where('status', 'received')
            ->orderBy('sent_datetime', 'DESC')
            ->findAll();
    }

    /**
     * Get sent messages for a user.
     *
     * @param int $userId
     * @return array
     */
    public function getSentMessages($userId)
    {
        return $this->where('sender_id', $userId)
            ->where('status', 'sent')
            ->orderBy('sent_datetime', 'DESC')
            ->findAll();
    }

    /**
     * Get draft messages for a user.
     *
     * @param int $userId
     * @return array
     */
    public function getDraftMessages($userId)
    {
        return $this->where('sender_id', $userId)
            ->where('status', 'draft')
            ->orderBy('sent_datetime', 'DESC')
            ->findAll();
    }

    /**
     * Get trashed messages for a user.
     *
     * @param int $userId
     * @return array
     */
    public function getTrashedMessages($userId)
    {
        return $this->groupStart()
            ->where('sender_id', $userId)
            ->orWhere('recipient_id', $userId)
            ->groupEnd()
            ->where('status', 'trashed')
            ->orderBy('sent_datetime', 'DESC')
            ->findAll();
    }

    /**
     * Get messages for a specific semester and school year.
     *
     * @param string $semester
     * @param string|null $school_year
     * @return array
     */
    public function getMessagesBySemesterAndYear($semester, $school_year = null)
    {
        return $this->where('semester', $semester)
            ->orderBy('sent_datetime', 'DESC')
            ->findAll();
    }
}
