<?php

namespace Tests\Feature\Livewire;

use App\Livewire\Routes;
use App\Models\Feed;
use App\Models\PlatformChannel;
use App\Models\Route;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;

class RoutesAutoApproveTest extends TestCase
{
    use RefreshDatabase;

    private function createRoute(?bool $autoApprove = null): Route
    {
        $feed = Feed::factory()->create();
        $channel = PlatformChannel::factory()->create();

        return Route::create([
            'feed_id' => $feed->id,
            'platform_channel_id' => $channel->id,
            'is_active' => true,
            'priority' => 50,
            'auto_approve' => $autoApprove,
        ]);
    }

    public function test_edit_modal_loads_auto_approve_null(): void
    {
        $route = $this->createRoute(null);

        Livewire::test(Routes::class)
            ->call('openEditModal', $route->feed_id, $route->platform_channel_id)
            ->assertSet('editAutoApprove', '');
    }

    public function test_edit_modal_loads_auto_approve_true(): void
    {
        $route = $this->createRoute(true);

        Livewire::test(Routes::class)
            ->call('openEditModal', $route->feed_id, $route->platform_channel_id)
            ->assertSet('editAutoApprove', '1');
    }

    public function test_edit_modal_loads_auto_approve_false(): void
    {
        $route = $this->createRoute(false);

        Livewire::test(Routes::class)
            ->call('openEditModal', $route->feed_id, $route->platform_channel_id)
            ->assertSet('editAutoApprove', '0');
    }

    public function test_update_route_sets_auto_approve_to_true(): void
    {
        $route = $this->createRoute(null);

        Livewire::test(Routes::class)
            ->call('openEditModal', $route->feed_id, $route->platform_channel_id)
            ->set('editAutoApprove', '1')
            ->call('updateRoute');

        $updated = Route::where('feed_id', $route->feed_id)
            ->where('platform_channel_id', $route->platform_channel_id)
            ->first();

        $this->assertTrue($updated->auto_approve);
    }

    public function test_update_route_sets_auto_approve_to_false(): void
    {
        $route = $this->createRoute(null);

        Livewire::test(Routes::class)
            ->call('openEditModal', $route->feed_id, $route->platform_channel_id)
            ->set('editAutoApprove', '0')
            ->call('updateRoute');

        $updated = Route::where('feed_id', $route->feed_id)
            ->where('platform_channel_id', $route->platform_channel_id)
            ->first();

        $this->assertFalse($updated->auto_approve);
    }

    public function test_update_route_sets_auto_approve_to_null(): void
    {
        $route = $this->createRoute(true);

        Livewire::test(Routes::class)
            ->call('openEditModal', $route->feed_id, $route->platform_channel_id)
            ->set('editAutoApprove', '')
            ->call('updateRoute');

        $updated = Route::where('feed_id', $route->feed_id)
            ->where('platform_channel_id', $route->platform_channel_id)
            ->first();

        $this->assertNull($updated->auto_approve);
    }

    public function test_route_card_shows_auto_approve_on_badge(): void
    {
        $this->createRoute(true);

        Livewire::test(Routes::class)
            ->assertSee('Auto-approve: On');
    }

    public function test_route_card_shows_auto_approve_off_badge(): void
    {
        $this->createRoute(false);

        Livewire::test(Routes::class)
            ->assertSee('Auto-approve: Off');
    }

    public function test_route_card_hides_badge_when_using_global_setting(): void
    {
        $this->createRoute(null);

        Livewire::test(Routes::class)
            ->assertDontSee('Auto-approve: On')
            ->assertDontSee('Auto-approve: Off');
    }
}
