<?php

namespace App\Actions\Streamer;

use App\Models\Streamer;

final class ValidateStreamerCodeAction
{
    /**
     * @return array{valid: bool, message: string, streamer: ?array}
     */
    public function handle(string $code): array
    {
        $streamer = Streamer::active()->byCode($code)->with('user')->first();

        if (! $streamer) {
            return [
                'valid' => false,
                'message' => 'Streamer code not found or inactive.',
                'streamer' => null,
            ];
        }

        return [
            'valid' => true,
            'message' => 'Streamer code applied successfully!',
            'streamer' => [
                'code' => $streamer->code,
                'name' => $streamer->user->name ?? 'Unknown',
                'truename' => $streamer->user->truename ?? null,
                'photo_url' => $streamer->user->photo ? asset($streamer->user->photo) : null,
                'percentage' => $streamer->percentage,
            ],
        ];
    }
}
