<?php

namespace Tests\Feature\Livewire;

use App\Livewire\Feeds;
use App\Models\Feed;
use App\Models\Language;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;

class FeedsTest extends TestCase
{
    use RefreshDatabase;

    public function test_add_button_renders_when_feeds_exist(): void
    {
        Feed::factory()->create(['name' => 'Existing Feed']);

        Livewire::test(Feeds::class)
            ->assertSee('Add Feed')
            ->assertSee('Existing Feed');
    }

    public function test_add_button_renders_in_empty_state(): void
    {
        Livewire::test(Feeds::class)
            ->assertSee('No feeds')
            ->assertSee('Add Feed');
    }

    public function test_open_create_modal_shows_modal(): void
    {
        Livewire::test(Feeds::class)
            ->assertSet('showCreateModal', false)
            ->call('openCreateModal')
            ->assertSet('showCreateModal', true);
    }

    public function test_create_feed_requires_name(): void
    {
        $language = Language::factory()->english()->create();

        Livewire::test(Feeds::class)
            ->call('openCreateModal')
            ->set('newProvider', 'guardian')
            ->set('newLanguageId', $language->id)
            ->call('createFeed')
            ->assertHasErrors(['newName' => 'required']);
    }

    public function test_create_feed_requires_provider(): void
    {
        $language = Language::factory()->english()->create();

        Livewire::test(Feeds::class)
            ->call('openCreateModal')
            ->set('newName', 'Tech News')
            ->set('newLanguageId', $language->id)
            ->call('createFeed')
            ->assertHasErrors(['newProvider' => 'required']);
    }

    public function test_create_feed_requires_language(): void
    {
        Livewire::test(Feeds::class)
            ->call('openCreateModal')
            ->set('newName', 'Tech News')
            ->set('newProvider', 'guardian')
            ->call('createFeed')
            ->assertHasErrors(['newLanguageId' => 'required']);
    }

    public function test_create_feed_rejects_unknown_provider(): void
    {
        $language = Language::factory()->english()->create();

        Livewire::test(Feeds::class)
            ->call('openCreateModal')
            ->set('newName', 'Tech News')
            ->set('newProvider', 'nonexistent')
            ->set('newLanguageId', $language->id)
            ->call('createFeed')
            ->assertHasErrors('newProvider');

        $this->assertDatabaseCount('feeds', 0);
    }

    public function test_create_feed_succeeds_and_resolves_url(): void
    {
        $language = Language::factory()->english()->create();

        Livewire::test(Feeds::class)
            ->call('openCreateModal')
            ->set('newName', 'Guardian News')
            ->set('newProvider', 'guardian')
            ->set('newLanguageId', $language->id)
            ->set('newDescription', 'British daily')
            ->call('createFeed')
            ->assertHasNoErrors()
            ->assertSet('showCreateModal', false);

        $this->assertDatabaseHas('feeds', [
            'name' => 'Guardian News',
            'provider' => 'guardian',
            'language_id' => $language->id,
            'url' => 'https://www.theguardian.com/international/rss',
            'description' => 'British daily',
            'is_active' => true,
        ]);
    }

    public function test_create_feed_leaves_description_null_when_blank(): void
    {
        $language = Language::factory()->english()->create();

        Livewire::test(Feeds::class)
            ->call('openCreateModal')
            ->set('newName', 'Guardian News')
            ->set('newProvider', 'guardian')
            ->set('newLanguageId', $language->id)
            ->call('createFeed')
            ->assertHasNoErrors();

        $this->assertDatabaseHas('feeds', [
            'name' => 'Guardian News',
            'description' => null,
        ]);
    }

    public function test_create_feed_surfaces_invalid_provider_language_combination(): void
    {
        // Belga only supports 'en'; a non-'en' language triggers InvalidArgumentException in the action.
        $language = Language::factory()->create(['short_code' => 'nl', 'name' => 'Dutch', 'is_active' => true]);

        Livewire::test(Feeds::class)
            ->call('openCreateModal')
            ->set('newName', 'Belga NL')
            ->set('newProvider', 'belga')
            ->set('newLanguageId', $language->id)
            ->call('createFeed')
            ->assertHasErrors('newProvider')
            ->assertSet('showCreateModal', true);

        $this->assertDatabaseCount('feeds', 0);
    }

    public function test_toggle_flips_active_state(): void
    {
        $feed = Feed::factory()->create(['is_active' => true]);

        Livewire::test(Feeds::class)
            ->call('toggle', $feed->id);

        $this->assertFalse($feed->fresh()->is_active);
    }
}
