<?php

namespace App\Notifications;

use App\Models\Request as RequestModel;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class RequestStatusNotification extends Notification
{
    use Queueable;

    protected $request;
    protected $status;
    protected $actionBy;

    /**
     * Constructeur de la notification
     */
    public function __construct(RequestModel $request, string $status, $actionBy)
    {
        $this->request = $request;
        $this->status = $status;
        $this->actionBy = $actionBy;
    }

    /**
     * Canal de notification
     * Envoi email au responsable (créateur) lorsque la demande est validée ou rejetée
     *
     * @return array<int, string>
     */
    public function via(object $notifiable): array
    {
        $channels = ['database'];
        if (in_array($this->status, ['validée', 'rejetée']) && $notifiable->email) {
            $channels[] = 'mail';
        }
        return $channels;
    }

    /**
     * Représentation de la notification en tableau
     *
     * @return array<string, mixed>
     */
    public function toArray(object $notifiable): array
    {
        $statusText = $this->status === 'validée' ? 'approuvée' : 'rejetée';
        $iconType = $this->status === 'validée' ? 'validated' : 'rejected';

        $ref = $this->request->reference ?? $this->request->request_number ?? 'N/A';
        $msg = "Votre demande {$ref} a été {$statusText} par " . ($this->actionBy->full_name ?? $this->actionBy->name);
        if ($this->status === 'rejetée' && !empty($this->request->rejection_reason)) {
            $msg .= '. Motif : ' . $this->request->rejection_reason;
        }

        return [
            'request_id' => $this->request->id,
            'request_number' => $ref,
            'status' => $this->status,
            'status_text' => $statusText,
            'icon_type' => $iconType,
            'action_by' => $this->actionBy->full_name ?? $this->actionBy->name,
            'action_date' => now()->format('d/m/Y H:i'),
            'message' => $msg,
            'rejection_reason' => $this->request->rejection_reason ?? null,
            'url' => route('requests.index'),
        ];
    }

    /**
     * Représentation de la notification en email (optionnel).
     */
    public function toMail(object $notifiable): MailMessage
    {
        $statusText = $this->status === 'validée' ? 'approuvée' : 'rejetée';
        $ref = $this->request->reference ?? $this->request->request_number ?? 'N/A';
        return (new MailMessage)
            ->subject("Demande {$statusText}")
            ->greeting('Bonjour ' . ($notifiable->full_name ?? $notifiable->name ?? '') . ',')
            ->line("Votre demande {$ref} a été {$statusText}.")
            ->line("Action effectuée par : " . ($this->actionBy->full_name ?? $this->actionBy->name))
            ->action('Voir la demande', route('requests.index'))
            ->when($this->status === 'rejetée' && !empty($this->request->rejection_reason), fn($m) => $m->line('Motif du rejet : ' . $this->request->rejection_reason))
            ->line('Merci d\'utiliser notre système de gestion des stocks!');
    }
}
