<?php

namespace Tests\Feature;

use App\Mcp\Servers\CampaignPerformanceServer;
use App\Mcp\Tools\Performance\ListAdopsCatalog;
use App\Mcp\Tools\Performance\QueryAdops;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\Concerns\CreatesAdopsWhaleTables;
use Tests\TestCase;

class CampaignPerformanceAdopsQueryTest extends TestCase
{
    use CreatesAdopsWhaleTables;
    use RefreshDatabase;

    public function test_catalog_lists_geo_account_and_breakdown_datasets(): void
    {
        $this->createAdopsWhaleTables();

        $response = CampaignPerformanceServer::tool(ListAdopsCatalog::class, []);

        $response->assertOk();
        $response->assertSee('stats_geo_daily');
        $response->assertSee('accounts');
        $response->assertSee('stats_breakdown_daily');
        $response->assertSee('query_adops');
    }

    public function test_query_groups_spend_by_country(): void
    {
        $this->travelTo('2026-09-22 12:00:00');
        $this->createAdopsWhaleTables();
        $this->seedAdopsCampaignMeta('1201', 'Italy packages');
        $this->seedAdopsGeoDaily('1201', 'US', '2026-09-22', 12.5, 20, 400);
        $this->seedAdopsGeoDaily('1201', 'AE', '2026-09-22', 3.25, 4, 80);

        $response = CampaignPerformanceServer::tool(QueryAdops::class, [
            'dataset' => 'stats_geo_daily',
            'group_by' => 'country,campaign_id',
            'from' => '2026-09-16',
            'to' => '2026-09-22',
        ]);

        $response->assertOk();
        $response->assertSee('US');
        $response->assertSee('AE');
        $response->assertSee('12.5');
        $response->assertSee('3.25');
        $response->assertSee('Italy packages');
    }

    public function test_query_lists_accounts_matching_a_name(): void
    {
        $this->createAdopsWhaleTables();
        $this->seedAdopsAccount('1950320145707342', 'TalkGPT');
        $this->seedAdopsAccount('3010881492442898', 'BING-KnowledgeDomain-NEW-OBM');

        $response = CampaignPerformanceServer::tool(QueryAdops::class, [
            'dataset' => 'accounts',
            'search' => 'TalkGPT',
        ]);

        $response->assertOk();
        $response->assertSee('TalkGPT');
        $response->assertSee('1950320145707342');
        $response->assertDontSee('BING-KnowledgeDomain');
    }

    public function test_query_rejects_an_unknown_dataset(): void
    {
        $this->createAdopsWhaleTables();

        $response = CampaignPerformanceServer::tool(QueryAdops::class, [
            'dataset' => 'users',
        ]);

        $response->assertHasErrors(['Unknown dataset']);
    }

    public function test_query_rejects_sql_in_group_by(): void
    {
        $this->createAdopsWhaleTables();
        $this->seedAdopsGeoDaily('1201', 'US', now()->toDateString(), 1, 1);

        $response = CampaignPerformanceServer::tool(QueryAdops::class, [
            'dataset' => 'stats_geo_daily',
            'group_by' => 'country; drop table arb_adops_campaigns',
        ]);

        $response->assertHasErrors(['Cannot group by']);
    }
}
