<?php

namespace Tests\Feature\Http\Controllers\Api\V1;

use App\Models\Feed;
use App\Models\Language;
use App\Models\PlatformAccount;
use App\Models\PlatformChannel;
use App\Models\PlatformInstance;
use App\Models\Route;
use App\Models\Setting;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class OnboardingControllerTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();

        // Create a language for testing
        Language::factory()->create([
            'id' => 1,
            'short_code' => 'en',
            'name' => 'English',
            'native_name' => 'English',
            'is_active' => true,
        ]);
    }

    public function test_status_shows_needs_onboarding_when_no_components_exist(): void
    {
        $response = $this->getJson('/api/v1/onboarding/status');

        $response->assertStatus(200)
            ->assertJson([
                'success' => true,
                'data' => [
                    'needs_onboarding' => true,
                    'current_step' => 'platform',
                    'has_platform_account' => false,
                    'has_feed' => false,
                    'has_channel' => false,
                    'has_route' => false,
                    'onboarding_skipped' => false,
                ],
            ]);
    }

    public function test_status_shows_feed_step_when_platform_account_exists(): void
    {
        PlatformAccount::factory()->create(['is_active' => true]);

        $response = $this->getJson('/api/v1/onboarding/status');

        $response->assertStatus(200)
            ->assertJson([
                'success' => true,
                'data' => [
                    'needs_onboarding' => true,
                    'current_step' => 'feed',
                    'has_platform_account' => true,
                    'has_feed' => false,
                    'has_channel' => false,
                    'has_route' => false,
                ],
            ]);
    }

    public function test_status_shows_channel_step_when_platform_account_and_feed_exist(): void
    {
        $language = Language::first();
        PlatformAccount::factory()->create(['is_active' => true]);
        Feed::factory()->language($language)->create(['is_active' => true]);

        $response = $this->getJson('/api/v1/onboarding/status');

        $response->assertStatus(200)
            ->assertJson([
                'success' => true,
                'data' => [
                    'needs_onboarding' => true,
                    'current_step' => 'channel',
                    'has_platform_account' => true,
                    'has_feed' => true,
                    'has_channel' => false,
                    'has_route' => false,
                ],
            ]);
    }

    public function test_status_shows_route_step_when_platform_account_feed_and_channel_exist(): void
    {
        $language = Language::first();
        PlatformAccount::factory()->create(['is_active' => true]);
        Feed::factory()->language($language)->create(['is_active' => true]);
        // Reuse the seeded language: PlatformChannelFactory otherwise creates one via
        // faker->unique()->languageCode(), which can randomly draw 'en' and collide with
        // the short_code created in setUp().
        PlatformChannel::factory()->create(['is_active' => true, 'language_id' => $language->id]);

        $response = $this->getJson('/api/v1/onboarding/status');

        $response->assertStatus(200)
            ->assertJson([
                'success' => true,
                'data' => [
                    'needs_onboarding' => true,
                    'current_step' => 'route',
                    'has_platform_account' => true,
                    'has_feed' => true,
                    'has_channel' => true,
                    'has_route' => false,
                ],
            ]);
    }

    public function test_status_shows_no_onboarding_needed_when_all_components_exist(): void
    {
        $language = Language::first();
        PlatformAccount::factory()->create(['is_active' => true]);
        Feed::factory()->language($language)->create(['is_active' => true]);
        // Reuse the seeded language — see note above re: languageCode() collisions.
        PlatformChannel::factory()->create(['is_active' => true, 'language_id' => $language->id]);
        Route::factory()->create(['is_active' => true]);

        $response = $this->getJson('/api/v1/onboarding/status');

        $response->assertStatus(200)
            ->assertJson([
                'success' => true,
                'data' => [
                    'needs_onboarding' => false,
                    'current_step' => null,
                    'has_platform_account' => true,
                    'has_feed' => true,
                    'has_channel' => true,
                    'has_route' => true,
                ],
            ]);
    }

    public function test_status_shows_no_onboarding_needed_when_skipped(): void
    {
        // No components exist but onboarding is skipped
        Setting::create([
            'key' => 'onboarding_skipped',
            'value' => 'true',
        ]);

        $response = $this->getJson('/api/v1/onboarding/status');

        $response->assertStatus(200)
            ->assertJson([
                'success' => true,
                'data' => [
                    'needs_onboarding' => false,
                    'current_step' => null,
                    'has_platform_account' => false,
                    'has_feed' => false,
                    'has_channel' => false,
                    'has_route' => false,
                    'onboarding_skipped' => true,
                ],
            ]);
    }

    public function test_options_returns_languages_and_platform_instances(): void
    {
        PlatformInstance::factory()->create([
            'platform' => 'lemmy',
            'url' => 'https://lemmy.world',
            'name' => 'Lemmy World',
            'is_active' => true,
        ]);

        $response = $this->getJson('/api/v1/onboarding/options');

        $response->assertStatus(200)
            ->assertJsonStructure([
                'success',
                'data' => [
                    'languages' => [
                        '*' => ['id', 'short_code', 'name', 'native_name', 'is_active'],
                    ],
                    'platform_instances' => [
                        '*' => ['id', 'platform', 'url', 'name', 'description', 'is_active'],
                    ],
                ],
            ]);
    }

    public function test_complete_onboarding_returns_success(): void
    {
        $response = $this->postJson('/api/v1/onboarding/complete');

        $response->assertStatus(200)
            ->assertJson([
                'success' => true,
                'data' => ['completed' => true],
            ]);
    }

    public function test_skip_onboarding_creates_setting(): void
    {
        $response = $this->postJson('/api/v1/onboarding/skip');

        $response->assertStatus(200)
            ->assertJson([
                'success' => true,
                'data' => ['skipped' => true],
            ]);

        $this->assertDatabaseHas('settings', [
            'key' => 'onboarding_skipped',
            'value' => 'true',
        ]);
    }

    public function test_skip_onboarding_updates_existing_setting(): void
    {
        // Create existing setting with false value
        Setting::create([
            'key' => 'onboarding_skipped',
            'value' => 'false',
        ]);

        $response = $this->postJson('/api/v1/onboarding/skip');

        $response->assertStatus(200);

        $this->assertDatabaseHas('settings', [
            'key' => 'onboarding_skipped',
            'value' => 'true',
        ]);

        // Ensure only one setting exists
        $this->assertEquals(1, Setting::where('key', 'onboarding_skipped')->count());
    }

    public function test_reset_skip_removes_setting(): void
    {
        // Create skipped setting
        Setting::create([
            'key' => 'onboarding_skipped',
            'value' => 'true',
        ]);

        $response = $this->postJson('/api/v1/onboarding/reset-skip');

        $response->assertStatus(200)
            ->assertJson([
                'success' => true,
                'data' => ['reset' => true],
            ]);

        $this->assertDatabaseMissing('settings', [
            'key' => 'onboarding_skipped',
        ]);
    }

    public function test_reset_skip_works_when_no_setting_exists(): void
    {
        $response = $this->postJson('/api/v1/onboarding/reset-skip');

        $response->assertStatus(200)
            ->assertJson([
                'success' => true,
                'data' => ['reset' => true],
            ]);
    }

    public function test_onboarding_flow_integration(): void
    {
        // 1. Initial status - needs onboarding
        $response = $this->getJson('/api/v1/onboarding/status');
        $response->assertJson(['data' => ['needs_onboarding' => true, 'current_step' => 'platform']]);

        // 2. Skip onboarding
        $response = $this->postJson('/api/v1/onboarding/skip');
        $response->assertJson(['data' => ['skipped' => true]]);

        // 3. Status after skip - no longer needs onboarding
        $response = $this->getJson('/api/v1/onboarding/status');
        $response->assertJson(['data' => ['needs_onboarding' => false, 'onboarding_skipped' => true]]);

        // 4. Reset skip
        $response = $this->postJson('/api/v1/onboarding/reset-skip');
        $response->assertJson(['data' => ['reset' => true]]);

        // 5. Status after reset - needs onboarding again
        $response = $this->getJson('/api/v1/onboarding/status');
        $response->assertJson(['data' => ['needs_onboarding' => true, 'onboarding_skipped' => false]]);
    }
}
