<?php

namespace Tests\Feature;

use Database\Seeders\DatabaseSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class DatabaseSeederTest extends TestCase
{
    use RefreshDatabase;

    public function test_production_seed_skips_demo_tenant_data(): void
    {
        $this->seedDatabaseForEnvironment('production');

        $this->assertDatabaseHas('packages', ['slug' => 'starter']);
        $this->assertDatabaseHas('blog_posts', ['slug' => '10-kreative-fotoaufgaben-fuer-eure-hochzeit']);
        $this->assertDatabaseMissing('tenants', ['slug' => 'demo-tenant']);
    }

    public function test_non_production_seed_includes_demo_tenant(): void
    {
        $this->seedDatabaseForEnvironment('local');

        $this->assertDatabaseHas('tenants', ['slug' => 'demo-tenant']);
    }

    private function seedDatabaseForEnvironment(string $environment): void
    {
        $originalEnvironment = app()->environment();
        app()->detectEnvironment(fn () => $environment);

        try {
            $options = ['--class' => DatabaseSeeder::class];

            if ($environment === 'production') {
                $options['--force'] = true;
            }

            $this->artisan('db:seed', $options);
        } finally {
            app()->detectEnvironment(fn () => $originalEnvironment);
        }
    }
}
