<?php

declare(strict_types=1);

use App\Filament\Pages\CrossTenantComparePage;
use App\Filament\Resources\TenantResource;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\Feature\Concerns\BuildsPortfolioCompareFixtures;

uses(RefreshDatabase::class, BuildsPortfolioCompareFixtures::class);

it('renders a reproducible compare preview and promotion preflight for two authorized tenants', function (): void {
    $fixture = $this->makeCrossTenantCompareFixture();

    $this->createPortfolioCompareSubject(
        tenant: $fixture['sourceTenant'],
        displayName: 'WiFi Corp',
        snapshot: ['settings' => [['key' => 'wifi', 'value' => 1]]],
    );
    $this->createPortfolioCompareSubject(
        tenant: $fixture['targetTenant'],
        displayName: 'WiFi Corp',
        snapshot: ['settings' => [['key' => 'wifi', 'value' => 1]]],
    );
    $this->createPortfolioCompareSubject(
        tenant: $fixture['sourceTenant'],
        displayName: 'Windows Compliance',
        snapshot: ['settings' => [['key' => 'compliance', 'value' => 1]]],
    );
    $this->createPortfolioCompareSubject(
        tenant: $fixture['targetTenant'],
        displayName: 'Windows Compliance',
        snapshot: ['settings' => [['key' => 'compliance', 'value' => 2]]],
    );

    $session = $this->setAdminWorkspaceContext($fixture['user'], $fixture['workspace']);
    $query = [
        'source_tenant_id' => (int) $fixture['sourceTenant']->getKey(),
        'target_tenant_id' => (int) $fixture['targetTenant']->getKey(),
        'policy_type' => ['deviceConfiguration'],
    ];

    $this->withSession($session)
        ->get(CrossTenantComparePage::getUrl(parameters: $query, panel: 'admin'))
        ->assertOk()
        ->assertSee('Cross-tenant compare')
        ->assertSee('Compare preview')
        ->assertSee('WiFi Corp')
        ->assertSee('Windows Compliance')
        ->assertSee('Source tenant: '.$fixture['sourceTenant']->name)
        ->assertSee('Target tenant: '.$fixture['targetTenant']->name)
        ->assertSee(TenantResource::getUrl('view', ['record' => $fixture['sourceTenant']], panel: 'admin'), false)
        ->assertSee(TenantResource::getUrl('view', ['record' => $fixture['targetTenant']], panel: 'admin'), false);

    Livewire::withQueryParams($query)
        ->actingAs($fixture['user'])
        ->test(CrossTenantComparePage::class)
        ->assertActionVisible('generatePromotionPreflight')
        ->assertActionEnabled('generatePromotionPreflight')
        ->call('generatePromotionPreflight')
        ->assertHasNoErrors()
        ->assertSee('Promotion preflight')
        ->assertSee('WiFi Corp')
        ->assertSee('Windows Compliance');
});

it('shows only one dominant promotion action at a time on the compare page', function (): void {
    $fixture = $this->makeCrossTenantCompareFixture();

    $this->createPortfolioCompareSubject(
        tenant: $fixture['sourceTenant'],
        displayName: 'Promotable Policy',
        snapshot: ['settings' => [['key' => 'wifi', 'value' => 1]]],
    );

    $this->setAdminWorkspaceContext($fixture['user'], $fixture['workspace']);

    Livewire::withQueryParams([
        'source_tenant_id' => (int) $fixture['sourceTenant']->getKey(),
        'target_tenant_id' => (int) $fixture['targetTenant']->getKey(),
        'policy_type' => ['deviceConfiguration'],
    ])
        ->actingAs($fixture['user'])
        ->test(CrossTenantComparePage::class)
        ->assertActionVisible('generatePromotionPreflight')
        ->assertActionHidden('executePromotion')
        ->call('generatePromotionPreflight')
        ->assertDontSee('Generate promotion preflight')
        ->assertSee('Execute promotion')
        ->assertActionVisible('executePromotion');
});

it('rejects the same tenant as source and target without rendering compare results', function (): void {
    $fixture = $this->makeCrossTenantCompareFixture();

    $session = $this->setAdminWorkspaceContext($fixture['user'], $fixture['workspace']);

    $this->withSession($session)
        ->get(CrossTenantComparePage::getUrl(parameters: [
            'source_tenant_id' => (int) $fixture['sourceTenant']->getKey(),
            'target_tenant_id' => (int) $fixture['sourceTenant']->getKey(),
        ], panel: 'admin'))
        ->assertOk()
        ->assertSee('Choose two different tenants.')
        ->assertDontSee('data-testid="cross-tenant-compare-preview"', false)
        ->assertDontSee('Promotion preflight');
});