<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class CharacterSyncController extends Controller
{
    /**
     * Optional push channel for "hot" data the resource has in memory but
     * hasn't necessarily flushed to the database yet (e.g. exact position,
     * current vehicle). The FrameworkAdapter reads player data straight
     * from the `fivem` connection for everything else; this cache is
     * consulted first by controllers that care about last-few-seconds
     * freshness. A 2-minute TTL means a server that stops pushing (crash,
     * resource restart) naturally falls back to plain DB reads.
     */
    public function store(Request $request): JsonResponse
    {
        $data = $request->validate([
            'citizen_id' => ['required', 'string', 'max:100'],
            'snapshot' => ['required', 'array'],
        ]);

        Cache::put("character-snapshot:{$data['citizen_id']}", $data['snapshot'], now()->addMinutes(2));

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