<?php

declare(strict_types=1);

namespace App\Services\Studio;

use App\Models\User;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;

final class CreatorStudioEventService
{
    private const ALLOWED_EVENTS = [
        'studio_opened',
        'studio_module_opened',
        'studio_quick_create_used',
        'studio_filter_used',
        'studio_item_edited',
        'studio_item_archived',
        'studio_item_restored',
        'studio_activity_opened',
        'studio_scheduled_opened',
        'studio_asset_opened',
        'studio_continue_working_used',
        'studio_schedule_created',
        'studio_schedule_updated',
        'studio_schedule_cleared',
        'studio_calendar_item_rescheduled',
        'studio_widget_customized',
        'studio_widget_reordered',
        'studio_asset_reused',
        'studio_comment_replied',
        'studio_comment_moderated',
        'studio_comment_reported',
        'studio_challenge_action_taken',
        'studio_insight_clicked',
        'studio_stale_draft_archived',
    ];

    public function allowedEvents(): array
    {
        return self::ALLOWED_EVENTS;
    }

    public function record(User $user, array $payload): void
    {
        Log::info('creator_studio_event', [
            'user_id' => (int) $user->id,
            'event_type' => (string) $payload['event_type'],
            'module' => Arr::get($payload, 'module'),
            'surface' => Arr::get($payload, 'surface'),
            'item_module' => Arr::get($payload, 'item_module'),
            'item_id' => Arr::get($payload, 'item_id'),
            'meta' => Arr::get($payload, 'meta', []),
            'occurred_at' => now()->toIso8601String(),
        ]);
    }
}