<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use App\Models\BaseModel;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class TeacherSubmissionNotificationHistory extends BaseModel
{
    protected $table = 'teacher_submission_notification_history';

    /**
     * CI: timestamps disabled (table uses sent_at)
     */
    public $timestamps = false;

    protected $fillable = [
        'teacher_id',
        'class_section_id',
        'admin_id',
        'notification_category',
        'message',
        'status',
        'school_year',
        'semester',
        'sent_at',
    ];

    protected $casts = [
        'teacher_id'       => 'integer',
        'class_section_id' => 'integer',
        'admin_id'         => 'integer',
        'sent_at'          => 'datetime',
    ];

    /* ============================================================
     * Relationships (optional)
     * ============================================================
     */

    public function teacher(): BelongsTo
    {
        // Teachers are users in your schema
        return $this->belongsTo(User::class, 'teacher_id');
    }

    public function admin(): BelongsTo
    {
        // Admins are users too (adjust if you have Admin model)
        return $this->belongsTo(User::class, 'admin_id');
    }

    public function classSection(): BelongsTo
    {
        // Adjust model/table mapping if your ClassSection uses different keying
        return $this->belongsTo(ClassSection::class, 'class_section_id', 'class_section_id');
    }

    /* ============================================================
     * Scopes
     * ============================================================
     */

    public function scopeForTeacher(Builder $q, int $teacherId): Builder
    {
        return $q->where('teacher_id', $teacherId);
    }

    public function scopeForSection(Builder $q, int $classSectionId): Builder
    {
        return $q->where('class_section_id', $classSectionId);
    }

    public function scopeForTerm(Builder $q, string $schoolYear, string $semester): Builder
    {
        return $q->where('school_year', $schoolYear)
                 ->where('semester', $semester);
    }

    public function scopeCategory(Builder $q, string $category): Builder
    {
        return $q->where('notification_category', $category);
    }

    public function scopeStatus(Builder $q, string $status): Builder
    {
        return $q->where('status', $status);
    }

    /* ============================================================
     * Helpers
     * ============================================================
     */

    /**
     * Create a history entry and auto-fill sent_at if missing.
     */
    public static function log(array $data): self
    {
        if (empty($data['sent_at'])) {
            $data['sent_at'] = now();
        }

        return static::create($data);
    }

    /**
     * Optional validation helper (FormRequest/controller).
     */
    public static function rules(bool $updating = false): array
    {
        $req = $updating ? 'sometimes' : 'required';

        return [
            'teacher_id'             => [$req, 'integer', 'min:1', 'exists:users,id'],
            'class_section_id'       => ['nullable', 'integer'],
            'admin_id'               => ['nullable', 'integer', 'exists:users,id'],
            'notification_category'  => [$req, 'string', 'max:120'],
            'message'                => [$req, 'string', 'max:5000'],
            'status'                 => ['nullable', 'string', 'max:50'],
            'school_year'            => ['nullable', 'string', 'max:20'],
            'semester'               => ['nullable', 'string', 'max:20'],
            'sent_at'                => ['nullable', 'date'],
        ];
    }
}