<?php

namespace App\Domains\Ai\Actions;

use App\Domains\Ai\Services\AiService;

class GenerateAbVariants
{
    public function __construct(private AiService $ai) {}

    /** @return array<int, string> */
    public function handle(string $targetUrl, int $count = 3): array
    {
        $prompt = "Generate {$count} A/B test URL slug variants (3-8 chars each, lowercase, hyphens only, comma-separated) for: {$targetUrl}. Reply with ONLY the comma-separated slugs.";

        $result = $this->ai->complete($prompt);

        if (empty($result)) {
            return [];
        }

        return array_map(
            fn ($s) => preg_replace('/[^a-z0-9-]/', '', strtolower(trim($s))),
            explode(',', $result)
        );
    }
}
