<?php

namespace App\Http\Controllers;

use App\Http\Requests\PushSubscriptionRequest;
use App\Services\System\PushSubscriptionService;
use Illuminate\Http\JsonResponse;

class PushSubscriptionController extends Controller
{
    public function __construct(
        private readonly PushSubscriptionService $pushSubscriptionService,
    ) {}

    public function store(PushSubscriptionRequest $request): JsonResponse
    {
        $this->pushSubscriptionService->updateOrCreateSubscription(
            $request->validated(),
            $request->user()->id,
        );

        return response()->json(['message' => 'Subscription berhasil disimpan.']);
    }

    public function destroy(PushSubscriptionRequest $request): JsonResponse
    {
        $this->pushSubscriptionService->deleteSubscription(
            $request->validated('endpoint'),
            $request->user()->id,
        );

        return response()->json(['message' => 'Subscription berhasil dihapus.']);
    }
}
