<?php

declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class WinbackCampaign extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'cancellation_reason',
        'email_sequence',
        'offer_type',
        'offer_value',
        'offer_duration_days',
        'coupon_code',
        'status',
    ];

    protected function casts(): array
    {
        return [
            'email_sequence' => 'array',
            'offer_value' => 'decimal:2',
            'offer_duration_days' => 'integer',
        ];
    }

    public function recipients(): HasMany
    {
        return $this->hasMany(WinbackRecipient::class, 'campaign_id');
    }

    /** @param  Builder<self>  $query */
    public function scopeActive(Builder $query): Builder
    {
        return $query->where('status', 'active');
    }

    /** @param  Builder<self>  $query */
    public function scopeForReason(Builder $query, string $reason): Builder
    {
        return $query->where('cancellation_reason', $reason);
    }
}
