<?php

declare(strict_types=1);

use App\Filament\Pages\CrossEnvironmentComparePage;
use App\Filament\Resources\ManagedEnvironmentResource;
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->makeCrossEnvironmentCompareFixture();

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

    $session = $this->setAdminWorkspaceContext($fixture['user'], $fixture['workspace']);
    $query = [
        'source_environment_id' => (int) $fixture['sourceEnvironment']->getKey(),
        'target_environment_id' => (int) $fixture['targetEnvironment']->getKey(),
        'policy_type' => ['deviceConfiguration'],
    ];

    $this->withSession($session)
        ->get(CrossEnvironmentComparePage::getUrl(parameters: $query, panel: 'admin'))
        ->assertOk()
        ->assertSee('Cross-environment compare')
        ->assertSee('Compare preview')
        ->assertSee('WiFi Corp')
        ->assertSee('Windows Compliance')
        ->assertSee('Source environment: '.$fixture['sourceEnvironment']->name)
        ->assertSee('Target environment: '.$fixture['targetEnvironment']->name)
        ->assertSee(ManagedEnvironmentResource::getUrl('view', ['record' => $fixture['sourceEnvironment']], panel: 'admin'), false)
        ->assertSee(ManagedEnvironmentResource::getUrl('view', ['record' => $fixture['targetEnvironment']], panel: 'admin'), false);

    Livewire::withQueryParams($query)
        ->actingAs($fixture['user'])
        ->test(CrossEnvironmentComparePage::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->makeCrossEnvironmentCompareFixture();

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

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

    Livewire::withQueryParams([
        'source_environment_id' => (int) $fixture['sourceEnvironment']->getKey(),
        'target_environment_id' => (int) $fixture['targetEnvironment']->getKey(),
        'policy_type' => ['deviceConfiguration'],
    ])
        ->actingAs($fixture['user'])
        ->test(CrossEnvironmentComparePage::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->makeCrossEnvironmentCompareFixture();

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

    $this->withSession($session)
        ->get(CrossEnvironmentComparePage::getUrl(parameters: [
            'source_environment_id' => (int) $fixture['sourceEnvironment']->getKey(),
            'target_environment_id' => (int) $fixture['sourceEnvironment']->getKey(),
        ], panel: 'admin'))
        ->assertOk()
        ->assertSee('Choose two different environments.')
        ->assertDontSee('data-testid="cross-environment-compare-preview"', false)
        ->assertDontSee('Promotion preflight');
});