<?php

namespace App\Modules\Pilkades\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Modules\Pilkades\Http\Requests\Public\RegisterDeviceTokenRequest;
use App\Modules\Pilkades\Models\DeviceToken;
use Illuminate\Http\JsonResponse;

class DeviceTokenController extends Controller
{
    /**
     * Registers (or updates) an FCM device token from the Android app.
     * No push is sent from this endpoint — it only stores the token so a
     * future notification-sending feature (out of MVP scope) can use it.
     */
    public function store(RegisterDeviceTokenRequest $request): JsonResponse
    {
        $token = DeviceToken::updateOrCreate(
            ['fcm_token' => $request->validated('fcm_token')],
            [
                'platform' => $request->validated('platform', 'android'),
                'desa_id' => $request->validated('desa_id'),
            ],
        );

        return response()->json(['status' => 'ok', 'id' => $token->id]);
    }
}
