<?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 ReservationCancelledMail extends Mailable implements ShouldQueue
{
    use Queueable;
    use SerializesModels;

    protected $data;
    protected $post;
    protected $reacter;
    protected $buttonUrl;
    public $locale;

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

        $this->data = $data;

        // Load the reacter based on the data
        $reacterType = $data['reacter_type'];
        $this->reacter = $reacterType::find($data['reacter_id']);
        \Log::info('ReservationCancelledMail: Reacter loaded', ['name' => $data['reacter_name']]);

        // Load the post with its relationships
        $this->post = \App\Models\Post::with(['translations', 'meeting'])->find($data['post_id']);

        \Log::info('ReservationCancelledMail: 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'
        ]);

        // Use the locale from the data
        $this->locale = $data['reacter_locale'];

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

        \Log::info('ReservationCancelledMail: 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('ReservationCancelledMail: 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('ReservationCancelledMail: Building email', [
            'locale' => $this->locale,
            'template' => 'emails.reactions.reservation-cancelled',
            'subject' => trans('messages.Reservation_cancelled', [], $this->locale),
        ]);

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