<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PushSubscriptionController extends Controller
{
    /**
     * Register or update push subscription for the authenticated user.
     */
    public function store(Request $request)
    {
        $validated = $request->validate([
            'endpoint' => 'required|string|max:500',
            'keys' => 'required|array',
            'keys.p256dh' => 'required|string',
            'keys.auth' => 'required|string',
        ]);

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

        return response()->json(['message' => 'Subscription saved']);
    }

    /**
     * Remove push subscription.
     */
    public function destroy(Request $request)
    {
        $endpoint = $request->input('endpoint');
        if ($endpoint) {
            $request->user()->deletePushSubscription($endpoint);
        }
        return response()->json(['message' => 'Subscription removed']);
    }
}
