<?php

declare(strict_types=1);

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Jobs\IngestUserDiscoveryEventJob;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Symfony\Component\HttpFoundation\Response;

final class DiscoveryEventController extends Controller
{
    public function store(Request $request): JsonResponse
    {
        $payload = $request->validate([
            'event_id' => ['nullable', 'uuid'],
            'event_type' => ['required', 'string', 'in:view,click,favorite,download,dwell,scroll'],
            'artwork_id' => ['required', 'integer', 'exists:artworks,id'],
            'occurred_at' => ['nullable', 'date'],
            'algo_version' => ['nullable', 'string', 'max:64'],
            'meta' => ['nullable', 'array'],
        ]);

        $eventId = (string) ($payload['event_id'] ?? (string) Str::uuid());
        $algoVersion = (string) ($payload['algo_version'] ?? config('discovery.algo_version', 'clip-cosine-v1'));
        $occurredAt = isset($payload['occurred_at'])
            ? (string) $payload['occurred_at']
            : now()->toIso8601String();

        IngestUserDiscoveryEventJob::dispatch(
            eventId: $eventId,
            userId: (int) $request->user()->id,
            artworkId: (int) $payload['artwork_id'],
            eventType: (string) $payload['event_type'],
            algoVersion: $algoVersion,
            occurredAt: $occurredAt,
            meta: (array) ($payload['meta'] ?? [])
        )->onQueue((string) config('discovery.queue', 'default'));

        return response()->json([
            'queued' => true,
            'event_id' => $eventId,
            'algo_version' => $algoVersion,
        ], Response::HTTP_ACCEPTED);
    }
}
