<?php

namespace Tests\Unit\Actions;

use App\Actions\CreateFeedAction;
use App\Models\Feed;
use App\Models\Language;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class CreateFeedActionTest extends TestCase
{
    use RefreshDatabase;

    private CreateFeedAction $action;

    protected function setUp(): void
    {
        parent::setUp();
        $this->action = new CreateFeedAction;
    }

    public function test_creates_vrt_feed_with_correct_url(): void
    {
        $language = Language::factory()->create(['short_code' => 'en', 'is_active' => true]);

        $feed = $this->action->execute('VRT News', 'vrt', $language->id, 'Test description');

        $this->assertInstanceOf(Feed::class, $feed);
        $this->assertEquals('VRT News', $feed->name);
        $this->assertEquals('https://www.vrt.be/vrtnws/en/', $feed->url);
        $this->assertEquals('website', $feed->type);
        $this->assertEquals('vrt', $feed->provider);
        $this->assertEquals($language->id, $feed->language_id);
        $this->assertEquals('Test description', $feed->description);
        $this->assertTrue($feed->is_active);
    }

    public function test_creates_belga_feed_with_correct_url(): void
    {
        $language = Language::factory()->create(['short_code' => 'en', 'is_active' => true]);

        $feed = $this->action->execute('Belga News', 'belga', $language->id);

        $this->assertStringStartsWith('https://capi.belga.press/belgapress/api/public/pressreleases?', $feed->url);
        $this->assertStringContainsString('newsroomId=70', $feed->url);
        $this->assertEquals('website', $feed->type);
        $this->assertEquals('belga', $feed->provider);
        $this->assertNull($feed->description);
    }

    public function test_creates_guardian_feed_with_correct_url(): void
    {
        $language = Language::factory()->create(['short_code' => 'en', 'is_active' => true]);

        $feed = $this->action->execute('Guardian News', 'guardian', $language->id);

        $this->assertEquals('https://www.theguardian.com/international/rss', $feed->url);
        $this->assertEquals('rss', $feed->type);
        $this->assertEquals('guardian', $feed->provider);
        $this->assertNull($feed->description);
    }

    public function test_creates_vrt_feed_with_dutch_language(): void
    {
        $language = Language::factory()->create(['short_code' => 'nl', 'is_active' => true]);

        $feed = $this->action->execute('VRT Nieuws', 'vrt', $language->id);

        $this->assertEquals('https://www.vrt.be/vrtnws/nl/', $feed->url);
    }

    public function test_returns_existing_feed_for_duplicate_url(): void
    {
        $language = Language::factory()->create(['short_code' => 'en', 'is_active' => true]);

        $first = $this->action->execute('VRT News', 'vrt', $language->id);
        $second = $this->action->execute('VRT News Duplicate', 'vrt', $language->id);

        $this->assertEquals($first->id, $second->id);
        $this->assertEquals(1, Feed::count());
    }

    public function test_throws_exception_for_invalid_provider_language_combination(): void
    {
        $language = Language::factory()->create(['short_code' => 'fr', 'is_active' => true]);

        $this->expectException(\InvalidArgumentException::class);

        $this->action->execute('Feed', 'belga', $language->id);
    }
}
