<?php

declare(strict_types=1);

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\User;
use App\Services\Profile\CreatorJourneyService;
use App\Support\UsernamePolicy;
use Illuminate\Http\JsonResponse;

final class ProfileJourneyController extends Controller
{
    public function __construct(private readonly CreatorJourneyService $journeys)
    {
    }

    public function __invoke(string $username): JsonResponse
    {
        $normalized = UsernamePolicy::normalize($username);

        $user = User::query()
            ->whereRaw('LOWER(username) = ?', [$normalized])
            ->where('is_active', true)
            ->whereNull('deleted_at')
            ->firstOrFail();

        return response()->json([
            'data' => $this->journeys->publicPayloadForUser($user),
            'meta' => [
                'username' => (string) $user->username,
                'generated_at' => now()->toIso8601String(),
            ],
        ]);
    }
}