<?php

namespace Tests\Feature;

use App\Models\AllowedAdAccount;
use App\Models\AllowedPage;
use Database\Seeders\FacebookAllowedPageSeeder;
use Database\Seeders\MetaAdAccountSeeder;
use Illuminate\Database\UniqueConstraintViolationException;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Schema;
use Tests\TestCase;

class MetaCatalogSeederTest extends TestCase
{
    use RefreshDatabase;

    public function test_all_supplied_pages_are_seeded_by_id_and_duplicate_names_are_preserved(): void
    {
        $this->seed(FacebookAllowedPageSeeder::class);

        $this->assertSame(125, AllowedPage::query()->forPlatform('facebook')->active()->count());
        $this->assertSame(0, AllowedPage::query()->forPlatform('meta')->count());
        $this->assertFalse(Schema::hasTable('meta_pages'));
        $this->assertDatabaseHas('allowed_pages', ['platform' => 'facebook', 'page_id' => '382886270228', 'name' => 'Leisure.com']);
        $this->assertDatabaseHas('allowed_pages', ['platform' => 'facebook', 'page_id' => '1058717260664617', 'name' => 'Leisure.com']);
        $this->assertDatabaseHas('allowed_pages', ['platform' => 'facebook', 'page_id' => '1284722614718519', 'name' => 'Attestpath']);
        $this->assertSame(3, AllowedPage::query()->where('name', 'Leisure.com')->count());

        $this->seed(FacebookAllowedPageSeeder::class);

        $this->assertSame(125, AllowedPage::query()->count());
    }

    public function test_the_same_page_id_can_be_allowed_independently_on_different_platforms(): void
    {
        AllowedPage::factory()->create(['platform' => 'facebook', 'page_id' => '12345', 'name' => 'Facebook Brand']);
        AllowedPage::factory()->create(['platform' => 'linkedin', 'page_id' => '12345', 'name' => 'LinkedIn Brand', 'is_active' => false]);

        $this->assertSame(1, AllowedPage::query()->forPlatform('facebook')->active()->count());
        $this->assertSame(0, AllowedPage::query()->forPlatform('linkedin')->active()->count());
        $this->assertDatabaseHas('allowed_pages', ['platform' => 'linkedin', 'page_id' => '12345', 'is_active' => false]);
    }

    public function test_a_page_id_cannot_be_duplicated_on_the_same_platform(): void
    {
        AllowedPage::factory()->create(['platform' => 'facebook', 'page_id' => '12345']);

        $this->expectException(UniqueConstraintViolationException::class);
        AllowedPage::factory()->create(['platform' => 'facebook', 'page_id' => '12345']);
    }

    public function test_all_supplied_accounts_are_seeded_with_disabled_accounts_excluded_from_allowlist(): void
    {
        $this->seed(MetaAdAccountSeeder::class);

        $this->assertSame(44, AllowedAdAccount::query()->forPlatform('meta')->count());
        $this->assertSame(41, AllowedAdAccount::query()->forPlatform('meta')->active()->count());
        $this->assertDatabaseHas('allowed_ad_accounts', [
            'platform' => 'meta',
            'account_id' => 'act_25021599314180332',
            'name' => 'Bing-TheAnswerHub-OBMEDIA',
            'is_active' => true,
        ]);
        $this->assertDatabaseHas('allowed_ad_accounts', [
            'platform' => 'meta',
            'account_id' => 'act_824024712556945',
            'name' => 'CC - AD MEDIA 2',
            'is_active' => false,
        ]);
        $this->assertFalse(AllowedAdAccount::isAllowed('824024712556945'));
        $this->assertFalse(AllowedAdAccount::isAllowed('1495918977704993'));
        $this->assertFalse(AllowedAdAccount::isAllowed('827677745418699'));
        $this->assertTrue(AllowedAdAccount::isAllowed('26632801489687888'));

        $this->seed(MetaAdAccountSeeder::class);

        $this->assertSame(44, AllowedAdAccount::query()->forPlatform('meta')->count());
    }
}
