<?php

declare(strict_types=1);

namespace App\Http\Controllers\Studio;

use App\Http\Controllers\Controller;
use App\Services\Studio\CreatorStudioEventService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

final class StudioEventsApiController extends Controller
{
    public function __construct(
        private readonly CreatorStudioEventService $events,
    ) {
    }

    public function store(Request $request): JsonResponse
    {
        $payload = $request->validate([
            'event_type' => ['required', 'string', Rule::in($this->events->allowedEvents())],
            'module' => ['sometimes', 'nullable', 'string', 'max:40'],
            'surface' => ['sometimes', 'nullable', 'string', 'max:120'],
            'item_module' => ['sometimes', 'nullable', 'string', 'max:40'],
            'item_id' => ['sometimes', 'nullable', 'integer'],
            'meta' => ['sometimes', 'array'],
        ]);

        $this->events->record($request->user(), $payload);

        return response()->json([
            'ok' => true,
        ], 202);
    }
}