<?php

namespace App\Services;

use App\Models\Notification;
use App\Models\User;
use Illuminate\Support\Str;

class NotificationService
{
    public function __construct(
        protected PushNotificationService $push
    ) {}

    public function notify(User $user, string $type, string $title, ?string $body = null, array $data = []): Notification
    {
        $notification = Notification::create([
            'id' => Str::uuid()->toString(),
            'user_id' => $user->id,
            'type' => $type,
            'title' => $title,
            'body' => $body,
            'data' => $data,
        ]);

        $this->push->sendToUser($user, $title, $body ?? '', $data);

        return $notification;
    }

    public function notifyOrderStatus(User $user, int $orderId, string $orderNumber, string $statusLabel): Notification
    {
        return $this->notify($user, 'order_status', "Commande {$orderNumber}", "Statut : {$statusLabel}", [
            'order_id' => $orderId,
            'order_number' => $orderNumber,
        ]);
    }

    public function notifyDeliveryStatus(User $user, int $orderId, string $orderNumber, string $statusLabel): Notification
    {
        return $this->notify($user, 'delivery_status', "Livraison {$orderNumber}", $statusLabel, [
            'order_id' => $orderId,
            'order_number' => $orderNumber,
        ]);
    }

    public function notifyEmpletteQuote(User $user, int $empletteId, string $message = 'Votre demande d\'emplette a été traitée.'): Notification
    {
        return $this->notify($user, 'emplette_quote', 'Devis emplette prêt', $message, [
            'emplette_id' => $empletteId,
        ]);
    }
}
