<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class EmailScheduledRecipient extends Model
{
    use HasFactory;

    protected $fillable = [
        'email_scheduled_id',
        'user_id',
        'email_address',
        'status',
        'error_message',
        'sent_at'
    ];

    protected $casts = [
        'sent_at' => 'datetime',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
    ];

    public function scheduledEmail()
    {
        return $this->belongsTo(EmailScheduled::class, 'email_scheduled_id');
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function markAsSent()
    {
        $this->update([
            'status' => 'sent',
            'sent_at' => now()
        ]);
    }

    public function markAsFailed($errorMessage = null)
    {
        $this->update([
            'status' => 'failed',
            'error_message' => $errorMessage
        ]);
    }

    public function markAsBounced($errorMessage = null)
    {
        $this->update([
            'status' => 'bounced',
            'error_message' => $errorMessage
        ]);
    }
}
