<?php

namespace App\Models;

class CampaignRecipientModel extends TenantModel
{
    protected $table         = 'campaign_recipients';
    protected $primaryKey    = 'id';
    protected $returnType    = 'object';
    protected $useTimestamps = false;

    protected $allowedFields = [
        'campaign_id',
        'guest_id',
        'status',
        'error',
        'sent_at',
    ];

    public function forCampaign(int $campaignId): array
    {
        return $this->select('campaign_recipients.*, guests.full_name as guest_name, guests.email as guest_email')
            ->join('guests', 'guests.id = campaign_recipients.guest_id', 'left')
            ->where('campaign_id', $campaignId)
            ->orderBy('campaign_recipients.sent_at', 'desc')
            ->findAll();
    }

    public function countByStatus(int $campaignId, string $status): int
    {
        return $this->where('campaign_id', $campaignId)->where('status', $status)->countAllResults();
    }
}
