<?php

namespace Tests\Unit\Actions;

use App\Actions\CreateChannelAction;
use App\Models\Language;
use App\Models\PlatformAccount;
use App\Models\PlatformChannel;
use App\Models\PlatformInstance;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class CreateChannelActionTest extends TestCase
{
    use RefreshDatabase;

    private CreateChannelAction $action;

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

    public function test_creates_channel_and_attaches_account(): void
    {
        $instance = PlatformInstance::factory()->create(['url' => 'https://lemmy.world']);
        $account = PlatformAccount::factory()->create([
            'instance_url' => 'https://lemmy.world',
            'is_active' => true,
        ]);
        $language = Language::factory()->create();

        $channel = $this->action->execute(
            'test_community',
            8,
            $instance->id, $language->id, 'A description');

        $this->assertInstanceOf(PlatformChannel::class, $channel);
        $this->assertEquals('test_community', $channel->name);
        $this->assertSame(8, $channel->channel_id);
        $this->assertEquals('Test_community', $channel->display_name);
        $this->assertEquals($instance->id, $channel->platform_instance_id);
        $this->assertEquals($language->id, $channel->language_id);
        $this->assertEquals('A description', $channel->description);
        $this->assertTrue($channel->is_active);

        // Verify account is attached
        $this->assertTrue($channel->platformAccounts->contains($account));
        $this->assertEquals(1, $channel->platformAccounts->first()->pivot->priority);
    }

    public function test_creates_channel_without_language(): void
    {
        $instance = PlatformInstance::factory()->create(['url' => 'https://lemmy.world']);
        PlatformAccount::factory()->create([
            'instance_url' => 'https://lemmy.world',
            'is_active' => true,
        ]);

        $channel = $this->action->execute(
            'test_community',
            8,
            $instance->id);

        $this->assertNull($channel->language_id);
    }

    public function test_fails_when_no_active_accounts(): void
    {
        $instance = PlatformInstance::factory()->create(['url' => 'https://lemmy.world']);
        // Create inactive account
        PlatformAccount::factory()->create([
            'instance_url' => 'https://lemmy.world',
            'is_active' => false,
        ]);

        $this->expectException(\RuntimeException::class);
        $this->expectExceptionMessage('No active platform accounts found for this instance');

        $this->action->execute(
            'test_community',
            8,
            $instance->id);
    }

    public function test_fails_when_no_accounts_at_all(): void
    {
        $instance = PlatformInstance::factory()->create();

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

        $this->action->execute(
            'test_community',
            8,
            $instance->id);
    }
}
