<?php

namespace App\Notifications;

use App\Filament\Resources\TenantFeedbackResource;
use App\Models\TenantFeedback;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Str;

class TenantFeedbackSubmitted extends Notification implements ShouldQueue
{
    use Queueable;

    public function __construct(protected TenantFeedback $feedback)
    {
        $this->feedback->loadMissing('tenant', 'event');
    }

    public function via(object $notifiable): array
    {
        return ['mail'];
    }

    public function toMail(object $notifiable): MailMessage
    {
        $tenantName = $this->resolveName($this->feedback->tenant?->name) ?: __('emails.tenant_feedback.unknown_tenant');
        $eventName = $this->resolveName($this->feedback->event?->name) ?: ($this->feedback->metadata['event_name'] ?? null);
        $sentiment = $this->feedback->sentiment ? Str::headline($this->feedback->sentiment) : __('emails.tenant_feedback.unknown');
        $rating = $this->feedback->rating ? sprintf('%d/5', $this->feedback->rating) : null;

        $subject = __('emails.tenant_feedback.subject', [
            'tenant' => $tenantName,
            'sentiment' => $sentiment,
        ]);
        $lines = [
            __('emails.tenant_feedback.tenant', ['tenant' => $tenantName]),
            __('emails.tenant_feedback.category', ['category' => $this->feedback->category ? Str::headline($this->feedback->category) : '—']),
            __('emails.tenant_feedback.sentiment', ['sentiment' => $sentiment]),
        ];

        if ($eventName) {
            $lines[] = __('emails.tenant_feedback.event', ['event' => $eventName]);
        }

        if ($rating) {
            $lines[] = __('emails.tenant_feedback.rating', ['rating' => $rating]);
        }

        if ($this->feedback->title) {
            $lines[] = __('emails.tenant_feedback.title', ['subject' => $this->feedback->title]);
        }

        if ($this->feedback->message) {
            $lines[] = __('emails.tenant_feedback.message');
            $lines[] = $this->feedback->message;
        }

        $url = TenantFeedbackResource::getUrl('view', ['record' => $this->feedback], panel: 'superadmin');

        $lines[] = __('emails.tenant_feedback.received_at', ['date' => $this->feedback->created_at?->toDayDateTimeString()]);

        return (new MailMessage)
            ->subject($subject)
            ->view('emails.notifications.basic', [
                'title' => $subject,
                'preheader' => $subject,
                'heroTitle' => $subject,
                'lines' => $lines,
                'cta' => $url ? [[
                    'label' => __('emails.tenant_feedback.open'),
                    'url' => $url,
                ]] : [],
            ]);
    }

    protected function resolveName(mixed $name): ?string
    {
        if (is_string($name) && $name !== '') {
            return $name;
        }

        if (is_array($name)) {
            return $name['de'] ?? $name['en'] ?? reset($name) ?: null;
        }

        return null;
    }
}
