<?php

namespace App\Notifications;

use App\Models\Reservation;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class ReservationApproved extends Notification
{

    public function __construct(
        public Reservation $reservation
    ) {}

    public function via(object $notifiable): array
    {
        $channels = ['database'];
        if (app()->environment('production')) {
            $channels[] = 'mail';
        }
        return $channels;
    }

    public function toMail(object $notifiable): MailMessage
    {
        $listing = $this->reservation->listing;
        $availability = $this->reservation->availability;
        
        return (new MailMessage)
            ->subject('Rezervasyonunuz Onaylandı - ' . $listing->title)
            ->greeting('Merhaba ' . $notifiable->name . '!')
            ->line('Rezervasyonunuz onaylandı.')
            ->line('Rezervasyon Kodu: ' . $this->reservation->reservation_code)
            ->line('İlan: ' . $listing->title)
            ->line('Tarih: ' . $this->reservation->reservation_date->format('d.m.Y'))
            ->line('Saat: ' . $availability->start_time->format('H:i') . ' - ' . $availability->end_time->format('H:i'))
            ->line('Kişi Sayısı: ' . $this->reservation->guest_count)
            ->line('Toplam Tutar: ₺' . number_format($this->reservation->total_amount, 2))
            ->action('Rezervasyon Detayları', url('/reservations/' . $this->reservation->id))
            ->line('İyi eğlenceler dileriz!');
    }

    public function toArray(object $notifiable): array
    {
        return [
            'type' => 'reservation_approved',
            'title' => 'Rezervasyon Onaylandı',
            'message' => 'Rezervasyonunuz onaylandı: ' . $this->reservation->listing->title,
            'reservation_id' => $this->reservation->id,
            'reservation_code' => $this->reservation->reservation_code,
            'listing_id' => $this->reservation->listing_id,
            'listing_title' => $this->reservation->listing->title,
            'reservation_date' => $this->reservation->reservation_date->format('Y-m-d'),
            'guest_count' => $this->reservation->guest_count,
            'total_amount' => $this->reservation->total_amount,
            'action_url' => '/reservations',
        ];
    }
}
