<?php

namespace App\Mail;

use Cog\Laravel\Love\ReactionType\Models\ReactionType;
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 ReservationCreatedMail extends Mailable implements ShouldQueue
{
    use Queueable;
    use SerializesModels;

    protected $reaction;
    protected $post;
    protected $buttonUrl;
    public $locale;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($reaction)
    {
        \Log::info('ReservationCreatedMail: Constructor called');

        $this->reaction = $reaction;

        // Get the reacter (person making the reservation)
        $reacter = $reaction->getReacter()->getReacterable();
        \Log::info('ReservationCreatedMail: Reacter loaded', ['name' => $reacter->name]);

        // Get the post (event being reserved) using the reactant's getReactable method
        $reactant = $reaction->getReactant();
        $this->post = $reactant->getReactable();

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

        \Log::info('ReservationCreatedMail: Post loaded', [
            'post_id' => $this->post ? $this->post->id : null,
            'post_title' => $this->post && $this->post->translations->first() ? $this->post->translations->first()->title : null,
            'has_meeting' => $this->post && $this->post->meeting ? 'yes' : 'no'
        ]);

        // 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('ReservationCreatedMail: 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('ReservationCreatedMail: Button URL created', ['url' => $this->buttonUrl, 'slug' => $translation->slug]);
            }
        }
    }

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

        \Log::info('ReservationCreatedMail: Building email', [
            'locale' => $this->locale,
            'template' => 'emails.reactions.reservation-confirmation',
            'subject' => trans('messages.Reservation_confirmation', [], $this->locale),
        ]);

        return $this
            ->from(timebank_config('mail.support.email'), timebank_config('mail.support.name'))
            ->subject(trans('messages.Reservation_confirmation', [], $this->locale))
            ->view('emails.reactions.reservation-confirmation')
            ->with([
                'reacter' => $this->reaction->getReacter()->getReacterable(),
                'post' => $this->post,
                'buttonUrl' => $this->buttonUrl,
            ]);
    }
}
