<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;

class ReservationUpdateMail extends Mailable implements ShouldQueue
{
    use Queueable;
    use SerializesModels;

    protected $reacter;
    protected $post;
    protected $message;
    protected $organizer;
    protected $buttonUrl;
    public $locale;

    /**
     * Create a new message 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)
    {
        Log::info('ReservationUpdateMail: Constructor called', [
            'reacter_id' => $reacter->id,
            'post_id' => $post->id
        ]);

        $this->reacter = $reacter;
        $this->post = $post;
        $this->message = $message;
        $this->organizer = $organizer;

        // Eager load the relationships we need
        if ($this->post) {
            $this->post->load(['translations', 'meeting']);
        }

        // Support lang_preference for User, Organization, Bank, and Admin models
        $this->locale = $reacter->lang_preference ?? config('app.fallback_locale');

        // Ensure locale is not empty
        if (empty($this->locale)) {
            $this->locale = config('app.fallback_locale');
        }

        Log::info('ReservationUpdateMail: Locale set', ['locale' => $this->locale]);

        // Button should link to the event/post using the slug
        if ($this->post && isset($this->post->id)) {
            // Get the translation for the user's locale
            $translation = $this->post->translations->where('locale', $this->locale)->first();

            // Fall back to first translation if locale not found
            if (!$translation) {
                $translation = $this->post->translations->first();
            }

            if ($translation && $translation->slug) {
                $this->buttonUrl = LaravelLocalization::localizeURL(route('post.show_by_slug', ['slug' => $translation->slug]), $this->locale);
                Log::info('ReservationUpdateMail: Button URL created', ['url' => $this->buttonUrl]);
            }
        }
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        // Set application locale for this email
        app()->setLocale($this->locale);

        Log::info('ReservationUpdateMail: Building email', [
            'locale' => $this->locale,
            'template' => 'emails.reactions.reservation-update',
            'subject' => trans('messages.Reservation_update', [], $this->locale),
        ]);

        return $this
            ->from(timebank_config('mail.support.email'), timebank_config('mail.support.name'))
            ->subject(trans('messages.Reservation_update', [], $this->locale))
            ->view('emails.reactions.reservation-update')
            ->with([
                'reacter' => $this->reacter,
                'post' => $this->post,
                'customMessage' => $this->message,
                'organizer' => $this->organizer,
                'buttonUrl' => $this->buttonUrl,
            ]);
    }
}
