<?php

namespace App\Models;

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

/**
 * @property int $id
 * @property string $name
 * @property string|null $description
 * @property string|null $provider
 * @property string|null $remote_id
 * @property int $status
 * @property int $subscribers_count
 * @property string|null $default_from_name
 * @property string|null $default_from_email
 * @property string|null $default_reply_to
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\EmailCampaign> $campaigns
 * @property-read int|null $campaigns_count
 *
 * @method static \Illuminate\Database\Eloquent\Builder<static>|EmailMarketingList newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|EmailMarketingList newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|EmailMarketingList query()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|EmailMarketingList whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|EmailMarketingList whereDefaultFromEmail($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|EmailMarketingList whereDefaultFromName($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|EmailMarketingList whereDefaultReplyTo($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|EmailMarketingList whereDescription($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|EmailMarketingList whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|EmailMarketingList whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|EmailMarketingList whereProvider($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|EmailMarketingList whereRemoteId($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|EmailMarketingList whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|EmailMarketingList whereSubscribersCount($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|EmailMarketingList whereUpdatedAt($value)
 *
 * @mixin \Eloquent
 */
class EmailMarketingList extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'description',
        'provider',
        'remote_id',
        'status',
        'subscribers_count',
        'default_from_name',
        'default_from_email',
        'default_reply_to',
    ];

    protected $casts = [
        'subscribers_count' => 'integer',
    ];

    public function campaigns()
    {
        return $this->hasMany(EmailCampaign::class, 'email_marketing_list_id');
    }

    public function subscribers(): BelongsToMany
    {
        return $this->belongsToMany(
            EmailMarketingSubscriber::class,
            'email_marketing_list_subscriber',
            'email_marketing_list_id',
            'email_marketing_subscriber_id',
        )->withPivot(['joined_at', 'source'])->withTimestamps();
    }

    public function recountSubscribers(): void
    {
        $count = $this->subscribers()->where('email_marketing_subscribers.status', 'subscribed')->count();
        $this->updateQuietly(['subscribers_count' => $count]);
    }
}
