<?php

namespace App\Notifications;

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

class ReservationRejected 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;
        
        $mail = (new MailMessage)
            ->subject('Rezervasyonunuz Reddedildi - ' . $listing->title)
            ->greeting('Merhaba ' . $notifiable->name . ',')
            ->line('Üzgünüz, rezervasyon talebiniz reddedildi.')
            ->line('İlan: ' . $listing->title)
            ->line('Tarih: ' . $this->reservation->reservation_date->format('d.m.Y'));

        if ($this->reservation->owner_note) {
            $mail->line('Kaptanın Notu: ' . $this->reservation->owner_note);
        }

        return $mail
            ->action('Diğer İlanları Keşfet', url('/listings'))
            ->line('Başka bir tarih veya ilan için rezervasyon yapabilirsiniz.');
    }

    public function toArray(object $notifiable): array
    {
        return [
            'type' => 'reservation_rejected',
            'title' => 'Rezervasyon Reddedildi',
            'message' => $this->reservation->owner_note
                ? 'Rezervasyonunuz reddedildi: ' . $this->reservation->listing->title . ' - Neden: ' . $this->reservation->owner_note
                : 'Rezervasyonunuz reddedildi: ' . $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'),
            'owner_note' => $this->reservation->owner_note,
            'action_url' => '/reservations',
        ];
    }
}
