<?php

namespace App\Models;

use CodeIgniter\Model;

class ParentNotificationModel extends Model
{
    protected $table         = 'parent_notifications';
    protected $primaryKey    = 'id';
    protected $useTimestamps = true;
    protected $createdField  = 'created_at';
    protected $updatedField  = 'updated_at';

    protected $allowedFields = [
        'student_id','code','incident_date','channel','to_address','subject',
        'status','response','semester','school_year'
    ];

    public function hasSent(int $studentId, string $code, string $incidentYmd, string $channel='email', ?string $to=null): bool
    {
        $qb = $this->where('student_id', $studentId)
                   ->where('code', $code)
                   ->where('incident_date', $incidentYmd)
                   ->where('channel', $channel);
        if ($to) $qb->where('to_address', $to);

        $row = $qb->orderBy('id','DESC')->first();
        return $row && ($row['status'] ?? '') === 'sent';
    }
}
