<?php

namespace Tests\Feature;

use App\Models\EventType;
use App\Models\Package;
use App\Models\Tenant;
use App\Models\TenantPackage;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Artisan;
use Tests\TestCase;

class SeedDemoSwitcherTenantsTest extends TestCase
{
    use RefreshDatabase;

    public function test_starter_empty_tenant_has_one_remaining_event(): void
    {
        config(['app.env' => 'local']);
        app()->detectEnvironment(fn () => 'local');

        $this->seedDemoPackages();
        $this->seedEventTypes();

        Artisan::call('demo:seed-switcher');

        $starterEmpty = Tenant::where('slug', 'demo-standard-empty')->firstOrFail();
        $starterPackage = TenantPackage::where('tenant_id', $starterEmpty->id)->with('package')->firstOrFail();

        $this->assertSame('starter', $starterPackage->package->slug);
        $this->assertSame(0, $starterPackage->used_events);
        $this->assertSame(1, $starterPackage->package->max_events_per_year - $starterPackage->used_events);
    }

    public function test_standard_wedding_tenant_consumes_event(): void
    {
        config(['app.env' => 'local']);
        app()->detectEnvironment(fn () => 'local');

        $this->seedDemoPackages();
        $this->seedEventTypes();

        Artisan::call('demo:seed-switcher');

        $standardWedding = Tenant::where('slug', 'demo-starter-wedding')->firstOrFail();
        $standardPackage = TenantPackage::where('tenant_id', $standardWedding->id)->with('package')->firstOrFail();

        $this->assertSame('standard', $standardPackage->package->slug);
        $this->assertSame(1, $standardPackage->used_events);
        $this->assertSame(4, $standardPackage->package->max_events_per_year - $standardPackage->used_events);
    }

    private function seedDemoPackages(): void
    {
        Package::factory()->create([
            'slug' => 'starter',
            'name' => 'Starter',
            'type' => 'endcustomer',
            'max_events_per_year' => 1,
        ]);

        Package::factory()->create([
            'slug' => 'standard',
            'name' => 'Standard',
            'type' => 'endcustomer',
            'max_events_per_year' => 5,
        ]);

        Package::factory()->create([
            'slug' => 's-small-reseller',
            'name' => 'Partner Start',
            'type' => 'reseller',
            'max_events_per_year' => 5,
            'included_package_slug' => 'starter',
        ]);
    }

    private function seedEventTypes(): void
    {
        $types = [
            'wedding' => ['de' => 'Hochzeit', 'en' => 'Wedding'],
            'corporate' => ['de' => 'Business', 'en' => 'Corporate'],
            'birthday' => ['de' => 'Geburtstag', 'en' => 'Birthday'],
            'festival' => ['de' => 'Festival', 'en' => 'Festival'],
        ];

        foreach ($types as $slug => $name) {
            EventType::factory()->create([
                'slug' => $slug,
                'name' => $name,
            ]);
        }
    }
}
