<?php

namespace Functional\Users\Http\Controllers;

use Functional\Users\Notifications\PushTestNotification;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

/**
 * Abonnement Web Push (PWA). Le front envoie sa subscription ; on la stocke sur l'utilisateur.
 * L'envoi réel des rappels est piloté par le back (commande planifiée) — cf couche streaks.
 */
class PushController
{
    /** POST /api/push/subscribe — enregistre/actualise l'abonnement push du navigateur. */
    public function subscribe(Request $request): JsonResponse
    {
        $data = $request->validate([
            'endpoint' => ['required', 'string'],
            'keys.p256dh' => ['required', 'string'],
            'keys.auth' => ['required', 'string'],
        ]);

        $request->user()->updatePushSubscription(
            $data['endpoint'],
            $data['keys']['p256dh'],
            $data['keys']['auth'],
        );

        return response()->json(['status' => 'subscribed']);
    }

    /** POST /api/push/unsubscribe — supprime l'abonnement (désactivation des rappels). */
    public function unsubscribe(Request $request): Response
    {
        $request->validate(['endpoint' => ['required', 'string']]);
        $request->user()->deletePushSubscription($request->input('endpoint'));

        return response()->noContent();
    }

    /** POST /api/push/test — envoie une notif de test (bouton "tester" côté profil). */
    public function test(Request $request): JsonResponse
    {
        $request->user()->notify(new PushTestNotification());

        return response()->json(['status' => 'sent']);
    }
}
