<?php

namespace App\Models\Email;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphTo;

class Email extends Model {

    /**
     * The attributes that should be cast.
     *
     * @var array
     */
    protected $casts = [
        'sent_at' => 'datetime',
        'failed_at' => 'datetime',
    ];

    /**
     * Get the model that owns the email (e.g., an Order, a User, a Subscription).
     */
    public function owner(): MorphTo {
        return $this->morphTo();
    }

    /**
     * Get the recipients associated with the email.
     */
    public function recipients(): HasMany {
        return $this->hasMany(EmailRecipient::class);
    }

    /**
     * Get all events associated with the email (delivered, opened, bounced, etc.).
     */
    public function events(): HasMany {
        return $this->hasMany(EmailEvent::class);
    }

    /**
     * Get the attachments associated with the email.
     */
    public function attachments(): HasMany {
        return $this->hasMany(EmailAttachment::class);
    }
}