<?php

namespace App\Jobs;

use App\Mail\PaymentFailedDay3Reminder;
use App\Mail\PaymentFailedDay5Reminder;
use App\Models\User;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Mail;
use InvalidArgumentException;

final class SendPaymentFailedReminderJob implements ShouldQueue
{
    use Queueable;

    public function __construct(
        public readonly int $userId,
        public readonly int $day,
    ) {
        $this->onQueue('notifications');
    }

    public function handle(): void
    {
        $user = User::find($this->userId);

        if ($user === null) {
            return;
        }

        if ($user->grace_period_until === null) {
            return;
        }

        $mailable = match ($this->day) {
            3 => new PaymentFailedDay3Reminder($user),
            5 => new PaymentFailedDay5Reminder($user),
            default => throw new InvalidArgumentException("Unsupported reminder day: {$this->day}"),
        };

        Mail::to($user->email)->send($mailable);
    }
}
