<?php

namespace App\Jobs;

use App\Mail\CertificateReadyMail;
use App\Models\Certificate;
use App\Models\EmailLog;
use App\Models\Program;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Mail;

class SendCertificateEmailJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public int $tries   = 3;
    public int $backoff = 60;

    public function __construct(public readonly Certificate $certificate) {}

    public function handle(): void
    {
        $cert = $this->certificate->refresh();
        $cert->load(['participant', 'program']);

        $email = $cert->participant->email;

        if (! $email) {
            return;
        }

        try {
            Mail::to($email)->send(new CertificateReadyMail($cert));

            $cert->update([
                'status'     => 'emailed',
                'emailed_at' => now(),
            ]);

            EmailLog::create([
                'program_id'     => $cert->program_id,
                'participant_id' => $cert->participant_id,
                'certificate_id' => $cert->id,
                'recipient_email'=> $email,
                'subject'        => 'Sijil Digital Program — ' . $cert->program->title,
                'email_type'     => 'certificate_ready',
                'status'         => 'sent',
                'sent_at'        => now(),
            ]);
        } catch (\Throwable $e) {
            EmailLog::create([
                'program_id'     => $cert->program_id,
                'participant_id' => $cert->participant_id,
                'certificate_id' => $cert->id,
                'recipient_email'=> $email,
                'subject'        => 'Sijil Digital Program — ' . $cert->program->title,
                'email_type'     => 'certificate_ready',
                'status'         => 'failed',
                'error_message'  => $e->getMessage(),
            ]);

            throw $e;
        }
    }

    public static function dispatchBatch(Program $program): void
    {
        $program->certificates()
            ->whereIn('status', ['generated'])
            ->whereNull('emailed_at')
            ->with('participant')
            ->each(function (Certificate $cert) {
                if ($cert->participant->email) {
                    static::dispatch($cert);
                }
            });
    }
}
