<?php

use App\Enums\AdType;
use App\Models\Ad;
use App\Models\AdPlatform;
use App\Models\Group;
use Database\Seeders\AdPlatformSeeder;
use Illuminate\Support\Facades\Http;

use function Pest\Laravel\actingAs;

beforeEach(function () {
    Group::firstOrCreate(['name' => Group::ADMIN]);
    Group::firstOrCreate(['name' => Group::MEMBERS]);
    $this->seed(AdPlatformSeeder::class);
});

test('seeder creates correct active and coming-soon platforms', function () {
    expect(AdPlatform::count())->toBe(10)
        ->and(AdPlatform::active()->count())->toBe(5)
        ->and(AdPlatform::where('is_active', false)->count())->toBe(5);

    // Affiliate platforms.
    expect(AdPlatform::where('ad_type', AdType::Affiliate)->count())->toBe(6);
    // Link website platforms.
    expect(AdPlatform::where('ad_type', AdType::LinkWebsite)->count())->toBe(2);
});

test('lazada has multiple countries', function () {
    $lazada = AdPlatform::where('slug', 'lazada')->first();

    expect($lazada)->not->toBeNull()
        ->and($lazada->countryList())->toBe(['ID', 'MY', 'PH', 'TH']);
});

test('member can create affiliate campaign with platform and country', function () {
    $member = makeMemberWithCredit(1000);
    $shopee = AdPlatform::where('slug', 'shopee')->first();

    $response = actingAs($member)->post(route('campaigns.store'), [
        'judul' => 'Iklan Shopee',
        'url' => 'https://shopee.co.id/product/123',
        'jam_tayang_mulai' => '08:00',
        'jam_tayang_selesai' => '20:00',
        'budget_harian' => 10000,
        'ad_type' => 'affiliate',
        'ad_platform_id' => $shopee->id,
        'target_country' => 'ID',
    ]);

    $response->assertRedirect(route('campaigns.index'));

    $ad = Ad::first();
    expect($ad->ad_type)->toBe(AdType::Affiliate)
        ->and($ad->ad_platform_id)->toBe($shopee->id)
        ->and($ad->target_country)->toBe('ID')
        ->and($ad->verification_code)->toBeNull(); // affiliate tidak butuh verifikasi
});

test('link_website campaign generates verification code', function () {
    $member = makeMemberWithCredit(1000);
    $website = AdPlatform::where('slug', 'website-umkm')->first();

    actingAs($member)->post(route('campaigns.store'), [
        'judul' => 'Website UMKM',
        'url' => 'https://umkm-contoh.test',
        'jam_tayang_mulai' => '00:00',
        'jam_tayang_selesai' => '23:59',
        'budget_harian' => 5000,
        'ad_type' => 'link_website',
        'ad_platform_id' => $website->id,
    ]);

    $ad = Ad::first();
    expect($ad->ad_type)->toBe(AdType::LinkWebsite)
        ->and($ad->verification_code)->not->toBeNull()
        ->and(str_starts_with($ad->verification_code, 'okeweb-'))->toBeTrue()
        ->and($ad->verified_at)->toBeNull();
});

test('verify endpoint marks verified when code present in site html', function () {
    $member = makeMemberWithCredit(1000);
    $website = AdPlatform::where('slug', 'website-umkm')->first();

    actingAs($member)->post(route('campaigns.store'), [
        'judul' => 'Verify Test',
        'url' => 'https://umkm-verify.test',
        'jam_tayang_mulai' => '00:00',
        'jam_tayang_selesai' => '23:59',
        'budget_harian' => 5000,
        'ad_type' => 'link_website',
        'ad_platform_id' => $website->id,
    ]);

    $ad = Ad::first();
    $code = $ad->verification_code;

    // Fake HTTP: site contains the verification code.
    Http::fake([
        'umkm-verify.test' => Http::response("<html><head><meta name='okeweb' content='{$code}'></head><body>Hello</body></html>", 200),
    ]);

    actingAs($member)->get(route('campaigns.verify', $ad));

    expect($ad->fresh()->verified_at)->not->toBeNull();
});

test('verify fails when code not present in site', function () {
    $member = makeMemberWithCredit(1000);
    $website = AdPlatform::where('slug', 'website-umkm')->first();

    actingAs($member)->post(route('campaigns.store'), [
        'judul' => 'Verify Fail',
        'url' => 'https://no-code.test',
        'jam_tayang_mulai' => '00:00',
        'jam_tayang_selesai' => '23:59',
        'budget_harian' => 5000,
        'ad_type' => 'link_website',
        'ad_platform_id' => $website->id,
    ]);

    $ad = Ad::first();

    Http::fake([
        'no-code.test' => Http::response('<html><body>No code here</body></html>', 200),
    ]);

    actingAs($member)->get(route('campaigns.verify', $ad));

    expect($ad->fresh()->verified_at)->toBeNull();
});

test('create form only shows active platforms as selectable', function () {
    $member = makeMember();

    $response = actingAs($member)->get(route('campaigns.create'));

    $platforms = collect($response->inertiaProps()['adPlatforms']);
    $comingSoon = $platforms->where('is_active', false);

    // Coming-soon platforms present but disabled in UI.
    expect($comingSoon->count())->toBeGreaterThan(0)
        ->and($platforms->where('is_active', true)->count())->toBe(5);
});
