<?php

namespace App\Mcp\Tools\Performance;

use App\Campaigns\CampaignSuggestionReader;
use App\Campaigns\WhaleAdopsReader;
use Illuminate\Contracts\JsonSchema\JsonSchema;
use Laravel\Mcp\Request;
use Laravel\Mcp\Response;
use Laravel\Mcp\Server\Attributes\Description;
use Laravel\Mcp\Server\Attributes\Name;
use Laravel\Mcp\Server\Tool;

#[Name('campaign_follow_up')]
#[Description('Follow-up questions about the latest campaign named in this chat. Do not use this for a question about how a specific campaign is performing, and do not use it for a different campaign. Returns 3, 5, 7, 15, and 30 day windows as context. Answer the question asked and give the suggestions that fit it. After that short answer, add Recommended decision, What should determine your decision, and My recommendation, with headings that match the question. Do not print a table or list the windows. When the user asks why clicks do not complete the action, place the drop with the returned counts. When those counts do not name the cause, add the usual checks for that stage and label each one as a check. Do not state a check as a finding. Do not use a sample funnel.')]
class CampaignFollowUp extends Tool
{
    public function __construct(
        private readonly WhaleAdopsReader $adops,
        private readonly CampaignSuggestionReader $suggestions,
    ) {}

    public function schema(JsonSchema $schema): array
    {
        return [
            'campaign_id' => $schema->string()
                ->description('Platform campaign id from the earlier turn.')
                ->required(),
            'platform' => $schema->string()
                ->description('meta, google, or linkedin. Use the platform from the earlier turn. Defaults to meta.'),
            'to' => $schema->string()
                ->description('End date YYYY-MM-DD for every window. Use the end date named in the earlier answer. Omit for today.'),
        ];
    }

    public function handle(Request $request): Response
    {
        if (! $this->adops->isAvailable()) {
            return Response::error('Whale adops tables are not available.');
        }

        $campaignId = trim((string) $request->get('campaign_id', ''));
        $platform = strtolower(trim((string) $request->get('platform', 'meta'))) ?: 'meta';

        if ($campaignId === '') {
            return Response::error('campaign_id is required.');
        }

        if (! in_array($platform, ['meta', 'google', 'linkedin'], true)) {
            return Response::text(json_encode([
                'ok' => false,
                'platform' => $platform,
                'message' => 'Only Meta, Google, and LinkedIn campaign data are in Whale so far.',
            ], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
        }

        $to = $request->get('to');
        $to = is_string($to) ? trim($to) : null;

        return Response::text(json_encode(
            $this->suggestions->followUp($platform, $campaignId, $to === '' ? null : $to),
            JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE,
        ));
    }
}
