<?php

namespace App\Agent\Tools\Campaign;

use App\Agent\Choices;
use App\Agent\Tools\Campaign\Concerns\ActsOnFocusedCampaign;
use App\Agent\Workspace;
use App\Assets\AssetLibrary;
use App\Campaigns\NameMatch;
use App\Models\Campaign;
use App\Models\CampaignAd;
use App\Models\Creative;
use App\Support\AssetOriginal;
use Illuminate\Contracts\JsonSchema\JsonSchema;
use Illuminate\Support\Collection;
use Laravel\Ai\Contracts\Tool;
use Laravel\Ai\Tools\Request;
use Stringable;

/**
 * Bring creatives made in the generator into this conversation.
 *
 * The generator runs outside any chat, so what it produces belongs to the user
 * but to no conversation. Without this the two halves of the product could not
 * reach each other: the chat would ask for an upload while a finished creative
 * sat one screen away.
 *
 * Called with no id it lists what is available and imports nothing, because the
 * user should choose. A variation group is several creatives on purpose, and
 * turning all of them into ads is a decision, not a default.
 */
class ImportCreatives implements Tool
{
    use ActsOnFocusedCampaign;

    /** @var list<string> */
    private const LINKEDIN_IMAGE_RATIOS = ['1.91:1', '1:1', '4:5'];

    /** @var list<string> */
    private const LINKEDIN_VIDEO_RATIOS = ['16:9', '9:16', '1:1', '4:5'];

    /**
     * How many to put on screen.
     *
     * Was 25 with no way to reach an older one, which is a silent cap: the
     * picker looked complete and was not. The renderer gives a search box once
     * a set is large, so the number can be higher than anyone would scroll.
     */
    private const MOST_SHOWN = 60;

    public function __construct(
        private readonly Workspace $workspace,
        private readonly Choices $choices,
        private readonly AssetLibrary $assets,
    ) {}

    /**
     * What this creative is, in the words of the thing it advertises.
     *
     * Was the shape and nothing else, so a buyer with four square images to
     * choose between read "1:1 Image" four times and had no way to tell them
     * apart. The generator writes a title for every creative and it was going
     * nowhere: "Women's Light-Wash Cuffed Straight-Leg Jeans" is the answer to
     * which one is this. The shape moves to the card footer.
     *
     * Generated titles are occasionally long, and one source prefixes them with
     * the product URL, so they are trimmed to something a card can hold.
     */
    private function describe(Creative $creative): string
    {
        $title = $this->withoutDuplicatedColumns($creative, trim((string) ($creative->title ?? '')));

        if ($title !== '') {
            return $title;
        }

        return 'Untitled creative';
    }

    /**
     * Strips the ratio and type back out of a title that was built from them.
     *
     * Creatives made before 23 Sep were titled "{project}-{ratio}-{name}-{type}",
     * so the card said 1:1 and Image in the label and again in the hint, and the
     * real name was pushed past the truncation. The generator no longer does
     * this, but the library is full of titles that already do.
     *
     * Matched against this creative's own ratio and type rather than a guess at
     * the shape of a title, so a creative genuinely called "16:9 Launch Film"
     * keeps its name. Everything between the ratio and the type is the name,
     * which is why the leading project segment goes with the ratio: it was only
     * ever the product name repeated.
     */
    private function withoutDuplicatedColumns(Creative $creative, string $title): string
    {
        if ($title === '') {
            return '';
        }

        $ratio = trim((string) ($creative->ratio ?? ''));

        if ($ratio !== '' && preg_match('/^(?:.*?-)?'.preg_quote($ratio, '/').'-(.+)$/s', $title, $found) === 1) {
            $title = trim($found[1]);
        }

        $type = ucfirst((string) ($creative->type ?? ''));

        if ($type !== '' && str_ends_with($title, '-'.$type)) {
            $title = mb_substr($title, 0, -mb_strlen('-'.$type));
        }

        // The old titles were joined on hyphens and the name was cut to 30
        // characters before joining, so a name truncated at a word boundary
        // leaves its own trailing hyphen behind once the type is removed:
        // "120Hr Hybrid ANC Escape-". Strip separators from both ends rather
        // than only whitespace.
        return trim($title, " \t\n\r-–—·|");
    }

    /** The image/video kind, ratio, and video duration shown in the card footer. */
    private function shape(Creative $creative): string
    {
        $parts = [$creative->type === 'video' ? 'Video' : 'Image'];

        if (filled($creative->ratio)) {
            $parts[] = trim((string) $creative->ratio);
        }

        if ($creative->type === 'video' && filled($creative->duration)) {
            $parts[] = ((int) $creative->duration).'s';
        }

        return implode(' - ', $parts);
    }

    /**
     * Everything else worth knowing before picking it.
     *
     * The shape leads now that the label carries the title: it is what decides
     * where the creative can run, and a buyer choosing between a 1:1 and a 9:16
     * is choosing between feed and story whether or not they think of it that
     * way. "from product_url" is left as the generator words it rather than
     * prettified, because it is the only thing distinguishing two creatives made
     * from the same page by different routes.
     */
    private function detail(Creative $creative, ?string $usedIn): ?string
    {
        return $usedIn ? 'Already in '.$usedIn : null;
    }

    /**
     * What this creative is about, in words, for matching against a campaign.
     *
     * The product URL is the strongest signal because it is the page the
     * creative was made for; the prompt is what someone typed about it.
     */
    private function searchableName(Creative $creative): string
    {
        return trim(implode(' ', array_filter([
            $creative->title,
            $creative->project_name,
            $creative->product_url,
            $creative->prompt,
        ])));
    }

    /**
     * Which campaign, if any, is already running each of these.
     *
     * Reusing a creative is normal and often deliberate, so this is shown
     * rather than prevented.
     *
     * @param  Collection<int, Creative>  $creatives
     * @return array<int, string> asset id to campaign name
     */
    private function alreadyUsed($creatives): array
    {
        $assetIds = $creatives->pluck('asset_id')->filter()->all();

        if ($assetIds === []) {
            return [];
        }

        return CampaignAd::query()
            ->whereIn('asset_id', $assetIds)
            ->with('campaign:id,name')
            ->get()
            ->filter(fn (CampaignAd $ad): bool => $ad->campaign !== null)
            ->mapWithKeys(fn (CampaignAd $ad): array => [$ad->asset_id => $ad->campaign->name])
            ->all();
    }

    public function description(): Stringable|string
    {
        return 'List the creatives the user generated in the creative generator, and bring the '
            .'chosen ones into this conversation so they can be attached to a campaign. Call '
            .'with no arguments to show what is available.';
    }

    public function schema(JsonSchema $schema): array
    {
        return [
            'creative_ids' => $schema->array()->items($schema->integer())
                ->description('Ids to import. Omit to list what is available without importing.'),
        ];
    }

    public static function linkedInCompatibilityGuidance(): string
    {
        return 'Supported LinkedIn image dimensions are 1200x628 (1.91:1), 1200x1200 (1:1), and 960x1200 (4:5). '
            .'Supported LinkedIn video aspect ratios are 16:9, 9:16, 1:1, and 4:5.';
    }

    public function handle(Request $request): Stringable|string
    {
        $user = $this->workspace->owner();

        if (! $user) {
            return 'error: no signed-in user, so there are no generated creatives to look at.';
        }

        $ids = collect((array) ($request->all()['creative_ids'] ?? []))
            ->filter()
            ->map(intval(...))
            ->all();

        // Only finished ones, and only those that produced a usable file. A
        // creative still rendering has nothing to attach yet.
        // Scoped to the signed-in user, which reverses an earlier decision.
        //
        // This showed everyone's creatives, on the reasoning that they are all
        // usable by everyone here and the campaign records who used what. The
        // reasoning was wrong about the product: a creative belongs to whoever
        // generated it and always has, so the shared listing was not enabling
        // anything, it was filling the picker with dozens of unrelated items to
        // scroll past to reach your own. Asked for by name in the 29 Sep feedback
        // and confirmed on 30 Sep that nobody depended on the shared listing.
        //
        // No approved-only filter yet, because creatives have no approved state:
        // the column does not exist and the approval flow is unbuilt. Once it
        // lands this is where it belongs.
        // The observer links a creative once, on completion; retry any whose file was not readable then.
        Creative::query()
            ->where('user_id', $user->id)
            ->where('status', 'completed')
            ->whereNull('asset_id')
            ->whereNotNull('asset_path')
            ->latest('id')
            ->limit(self::MOST_SHOWN)
            ->get()
            ->each(fn (Creative $creative): mixed => $this->assets->fromCreative($creative));

        $available = Creative::query()
            ->where('user_id', $user->id)
            ->where('status', 'completed')
            ->whereNotNull('asset_id')
            ->with('asset')
            ->latest('id')
            ->limit(self::MOST_SHOWN)
            ->get();

        // AssetOriginal rather than a bare exists(), because a single stat saying
        // no is not enough to tell a buyer their file has gone. Mohsin was told
        // creative 14's original was unavailable while it was being served from
        // the same path, byte for byte.
        $missingFiles = $available->filter(fn (Creative $creative): bool => AssetOriginal::isMissing($creative->asset));
        $missingRequested = $missingFiles->whereIn('id', $ids)->pluck('id')->all();

        if ($missingRequested !== []) {
            return 'error: the original file for creative '.implode(', ', $missingRequested)
                .' is unavailable on this server. A preview may still show a cached thumbnail. Restore the original file or choose another creative.';
        }

        $available = $available->reject(fn (Creative $creative): bool => $missingFiles->contains('id', $creative->id))
            // Two creative rows can point at one file, which showed as the same card twice.
            ->unique('asset_id')
            ->values();

        $campaign = $this->workspace->focus();
        if ($campaign instanceof Campaign && $campaign->platform === 'linkedin') {
            $unsupported = $available->filter(function (Creative $creative) use ($campaign): bool {
                $ratios = $creative->type === 'video'
                    ? self::LINKEDIN_VIDEO_RATIOS
                    : self::LINKEDIN_IMAGE_RATIOS;

                return ($campaign->objective === 'VIDEO_VIEWS' && $creative->type !== 'video')
                    || (filled($creative->ratio) && ! in_array($creative->ratio, $ratios, true));
            });
            if ($unsupported->whereIn('id', $ids)->isNotEmpty()) {
                return 'error: this creative format or aspect ratio is unsupported for the LinkedIn objective. '
                    .self::linkedInCompatibilityGuidance();
            }
            if ($available->isNotEmpty() && $unsupported->count() === $available->count()) {
                $formats = $unsupported
                    ->map(fn (Creative $creative): string => sprintf(
                        '%d (%s %s)',
                        $creative->id,
                        $creative->type,
                        $creative->ratio ?: 'unspecified ratio',
                    ))
                    ->implode(', ');

                return 'Finished creatives were found, but none are compatible with this LinkedIn campaign: '
                    .$formats.'. '.self::linkedInCompatibilityGuidance().' Create a compatible creative in the creative '
                    .'generator at '.route('creatives.create').', then return here and click Try again.';
            }
            $available = $available->reject(fn (Creative $creative): bool => $unsupported->contains('id', $creative->id))->values();
        }

        if ($available->isEmpty()) {
            return 'No finished creatives with available original files exist yet. Create an image or video in the creative '
                .'generator at '.route('creatives.create').', then return here to select it. Creative uploads and manual '
                .'creative entry are not available in this chat.';
        }

        if ($ids === []) {
            $campaign = $this->workspace->focus();
            $usedIn = $this->alreadyUsed($available);

            // Newest first is only right when nothing is known about what the
            // creative is for. The campaign says what it is for, so a creative
            // made for the same site comes first.
            $relevant = NameMatch::suggestFirst(
                $available->map(fn (Creative $c): array => ['creative' => $c, 'name' => $this->searchableName($c)])->all(),
                $campaign instanceof Campaign ? [$campaign->landing_url] : [],
            );

            // Shown, not listed by id. A creative is chosen by looking at it,
            // which is the whole reason the preview route exists.
            $this->choices->offer(
                field: 'creative_ids',
                question: 'Which creatives should be used?',
                options: array_map(function (array $row) use ($usedIn): array {
                    $c = $row['creative'];

                    return [
                        'value' => (string) $c->id,
                        'label' => $this->describe($c),
                        'hint' => $this->detail($c, $usedIn[$c->asset_id] ?? null),
                        'footer' => $this->shape($c),
                        'badge' => ($row['suggested'] ?? false) ? 'Suggested' : null,
                        'image' => $c->asset
                            ? route('assets.preview', $c->asset)
                            : null,
                        'video' => $c->asset && $c->asset->kind === 'video'
                            ? route('assets.preview', ['asset' => $c->asset, 'full' => 1])
                            : null,
                        // The film icon remains the fallback if no local or
                        // platform poster can be produced.
                        'icon' => $c->type === 'video' ? 'bi-film' : null,
                        'sends' => 'Import creative '.$c->id,
                    ];
                }, $relevant),
                multiple: true,
            );

            return sprintf(
                '%d creative(s) are on screen for the user to pick. If none fit, they can make one in the creative generator: %s. Say one short line and wait.',
                count($relevant),
                route('creatives.create'),
            );
        }

        $chosen = $available->whereIn('id', $ids);
        if ($campaign instanceof Campaign && $campaign->platform === 'linkedin' && $chosen->pluck('type')->unique()->count() > 1) {
            return 'error: LinkedIn requires one campaign format. Choose images or videos, not both in the same campaign.';
        }

        if ($chosen->isEmpty()) {
            return 'error: none of those ids are finished creatives belonging to this user.';
        }

        // Without focus: false these would each take focus in turn, leaving the
        // conversation pointed at a creative rather than the campaign it was
        // just brought in for, and the next tool finds no campaign.
        foreach ($chosen as $creative) {
            $this->workspace->produced($creative->asset, focus: false);
        }

        $missing = array_diff($ids, $chosen->pluck('id')->all());
        $idsList = $chosen->pluck('id')->implode(', ');

        return sprintf(
            'Imported %d creative(s) into this conversation: %s.%s',
            $chosen->count(),
            $idsList,
            $missing ? ' Not found or not finished: '.implode(', ', $missing).'.' : '',
        );
    }
}
