<?php

namespace App\Mail;

use App\Models\Consultation;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

class NewBookingAdminEmail extends Mailable implements ShouldQueue
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     */
    public function __construct(public Consultation $consultation)
    {
        $admin = $this->getAdminUser();
        $this->locale = $admin?->preferred_language ?? 'en';
    }

    /**
     * Get the message envelope.
     */
    public function envelope(): Envelope
    {
        $admin = $this->getAdminUser();
        $locale = $admin?->preferred_language ?? 'en';

        return new Envelope(
            subject: $locale === 'ar'
                ? '[إجراء مطلوب] طلب استشارة جديد'
                : '[Action Required] New Consultation Request',
        );
    }

    /**
     * Get the message content definition.
     */
    public function content(): Content
    {
        $admin = $this->getAdminUser();
        $locale = $admin?->preferred_language ?? 'en';

        return new Content(
            markdown: 'emails.admin.new-booking.'.$locale,
            with: [
                'consultation' => $this->consultation,
                'client' => $this->consultation->user,
                'clientName' => $this->consultation->getClientName(),
                'clientEmail' => $this->consultation->getClientEmail(),
                'clientPhone' => $this->consultation->getClientPhone(),
                'isGuest' => $this->consultation->isGuest(),
                'formattedDate' => $this->getFormattedDate($locale),
                'formattedTime' => $this->getFormattedTime(),
                'reviewUrl' => $this->getReviewUrl(),
            ],
        );
    }

    /**
     * Get the admin user.
     */
    public function getAdminUser(): ?User
    {
        return User::query()->where('user_type', 'admin')->first();
    }

    /**
     * Get formatted date based on locale.
     */
    public function getFormattedDate(string $locale): string
    {
        $date = $this->consultation->booking_date;

        return $locale === 'ar'
            ? $date->format('d/m/Y')
            : $date->format('m/d/Y');
    }

    /**
     * Get formatted time.
     */
    public function getFormattedTime(): string
    {
        $time = $this->consultation->booking_time;

        return Carbon::parse($time)->format('h:i A');
    }

    /**
     * Get the review URL for admin dashboard.
     */
    public function getReviewUrl(): string
    {
        return route('admin.consultations.show', $this->consultation);
    }

    /**
     * Get the attachments for the message.
     *
     * @return array<int, \Illuminate\Mail\Mailables\Attachment>
     */
    public function attachments(): array
    {
        return [];
    }
}
