<?php

namespace Tests\Unit\Services\Keywords;

use App\Services\Keywords\AiKeywordCandidateMatcher;
use App\Services\Keywords\HeuristicKeywordMatcher;
use App\Services\Keywords\KeywordCandidateEnricher;
use App\Services\Keywords\ProductKeywordParser;
use Laravel\Ai\StructuredAnonymousAgent;
use Tests\TestCase;

class KeywordCandidateEnricherTest extends TestCase
{
    private function enricher(): KeywordCandidateEnricher
    {
        return new KeywordCandidateEnricher(
            new AiKeywordCandidateMatcher,
            new HeuristicKeywordMatcher,
            new ProductKeywordParser,
        );
    }

    public function test_it_prefers_imported_keywords_before_ai_or_heuristic_matches(): void
    {
        config(['google-ads.ai_keyword_matching' => true]);

        StructuredAnonymousAgent::fake([
            ['matches' => [
                [
                    'index' => 1,
                    'term' => 'office chair',
                    'match_reason' => 'AI match should not win when import keyword exists.',
                ],
            ]],
        ]);

        $enriched = $this->enricher()->enrich(
            [[
                'name' => 'Ergonomic Mesh Office Chair',
                'url' => 'https://shop.example.com/chair',
                'product_keywords' => [
                    'primary' => 'mesh office chair',
                    'related' => ['office chair'],
                ],
            ]],
            [[
                'term' => 'mesh office chair',
                'avg_monthly_searches' => 120000,
                'competition' => 'HIGH',
            ]],
        );

        $this->assertSame('mesh office chair', $enriched[0]['matched_keyword']);
        $this->assertSame('import', $enriched[0]['keyword_match_source']);
    }

    public function test_it_uses_ai_matches_when_available(): void
    {
        config(['google-ads.ai_keyword_matching' => true]);

        StructuredAnonymousAgent::fake([
            ['matches' => [
                [
                    'index' => 1,
                    'term' => 'office chair',
                    'match_reason' => 'The product is an office chair with mesh upholstery.',
                ],
            ]],
        ]);

        $enriched = $this->enricher()->enrich(
            [
                [
                    'name' => 'Ergonomic Mesh Office Chair',
                    'url' => 'https://shop.example.com/chair',
                ],
            ],
            [
                [
                    'term' => 'office chair',
                    'avg_monthly_searches' => 201000,
                    'competition' => 'HIGH',
                    'low_top_of_page_bid_micros' => 2500000,
                ],
            ],
            'Office',
            'Chairs',
        );

        $this->assertSame('office chair', $enriched[0]['matched_keyword']);
        $this->assertSame(201000, $enriched[0]['keyword_search_volume']);
        $this->assertSame('HIGH', $enriched[0]['keyword_competition']);
        $this->assertSame('ai', $enriched[0]['keyword_match_source']);
        $this->assertStringContainsString('mesh upholstery', $enriched[0]['keyword_match_reason']);
    }

    public function test_it_falls_back_to_heuristic_matching_when_ai_returns_nothing(): void
    {
        config(['google-ads.ai_keyword_matching' => false]);

        $enriched = $this->enricher()->enrich(
            [
                [
                    'name' => 'Ergonomic Mesh Office Chair',
                    'url' => 'https://shop.example.com/chair',
                ],
            ],
            [
                [
                    'term' => 'office chair',
                    'avg_monthly_searches' => 201000,
                    'competition' => 'HIGH',
                ],
            ],
        );

        $this->assertSame('office chair', $enriched[0]['matched_keyword']);
        $this->assertSame('heuristic', $enriched[0]['keyword_match_source']);
        $this->assertStringContainsString('token overlap', $enriched[0]['keyword_match_reason']);
    }

    public function test_it_leaves_candidates_unchanged_when_no_keyword_matches(): void
    {
        config(['google-ads.ai_keyword_matching' => false]);

        $candidate = [
            'name' => 'Mystery Gadget',
            'url' => 'https://shop.example.com/gadget',
        ];

        $enriched = $this->enricher()->enrich(
            [$candidate],
            [
                [
                    'term' => 'camping tent',
                    'avg_monthly_searches' => 50000,
                    'competition' => 'MEDIUM',
                ],
            ],
        );

        $this->assertSame($candidate, $enriched[0]);
    }

    public function test_it_returns_candidates_unchanged_when_keyword_results_are_empty(): void
    {
        $candidate = [
            'name' => 'Office Chair',
            'url' => 'https://shop.example.com/chair',
        ];

        $enriched = $this->enricher()->enrich([$candidate], []);

        $this->assertSame($candidate, $enriched[0]);
    }
}
