<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
 * Records each reminder dispatched to parents for an open promotion
 * record (see plan section 14).
 */
class PromotionReminderLog extends BaseModel
{
    protected $table = 'promotion_reminders_log';

    public $timestamps = true;

    public const TYPE_FIRST = 'first';
    public const TYPE_HALFWAY = 'halfway';
    public const TYPE_FINAL = 'final';
    public const TYPE_EXPIRATION = 'expiration';
    public const TYPE_MANUAL = 'manual';

    public const ALLOWED_TYPES = [
        self::TYPE_FIRST,
        self::TYPE_HALFWAY,
        self::TYPE_FINAL,
        self::TYPE_EXPIRATION,
        self::TYPE_MANUAL,
    ];

    protected $fillable = [
        'promotion_id',
        'student_id',
        'parent_id',
        'reminder_type',
        'channel',
        'subject',
        'message',
        'sent_at',
        'sent_by',
    ];

    protected $casts = [
        'promotion_id' => 'integer',
        'student_id' => 'integer',
        'parent_id' => 'integer',
        'sent_by' => 'integer',
        'sent_at' => 'datetime',
    ];

    public function promotion(): BelongsTo
    {
        return $this->belongsTo(StudentPromotionRecord::class, 'promotion_id', 'promotion_id');
    }

    public function scopeForPromotion(Builder $q, int $promotionId): Builder
    {
        return $q->where('promotion_id', $promotionId);
    }
}
