<?php

namespace App\Jobs;

use App\Mail\ReservationUpdateMail;
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\Log;
use Illuminate\Support\Facades\Mail;

class SendReservationUpdateMail implements ShouldQueue
{
    use Dispatchable;
    use InteractsWithQueue;
    use Queueable;
    use SerializesModels;

    public $tries = 3;
    public $backoff = [5, 30, 300]; // wait for 5, 30, or 300 sec before worker tries again

    protected $reacter;
    protected $post;
    protected $message;
    protected $organizer;

    /**
     * Create a new job instance.
     *
     * @param mixed $reacter The profile who made the reservation
     * @param mixed $post The post/event
     * @param string $message The custom message from organizer
     * @param mixed $organizer The organizer sending the message
     * @return void
     */
    public function __construct($reacter, $post, $message, $organizer)
    {
        $this->reacter = $reacter;
        $this->post = $post;
        $this->message = $message;
        $this->organizer = $organizer;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        Log::info('SendReservationUpdateMail: Starting to process job', [
            'reacter_type' => get_class($this->reacter),
            'reacter_id' => $this->reacter->id,
            'post_id' => $this->post->id
        ]);

        // Send the email to the reacter
        Mail::to($this->reacter->email)->send(
            new ReservationUpdateMail($this->reacter, $this->post, $this->message, $this->organizer)
        );

        Log::info('SendReservationUpdateMail: Email sent successfully', [
            'to' => $this->reacter->email,
            'post_id' => $this->post->id
        ]);
    }
}
