<?php

namespace Tests\Feature;

use App\Domain\Subscription\MetricsService;
use App\Models\Tenant;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class DemoClinicTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        $this->seed();
    }

    public function test_demo_command_builds_an_evaluation_clinic(): void
    {
        $this->artisan('medibook:demo')->assertSuccessful();

        $tenant = Tenant::withoutGlobalScope('tenant')->where('slug', 'demo')->first();
        $this->assertNotNull($tenant);
        $this->assertTrue($tenant->is_demo);
        $this->assertDatabaseHas('users', ['email' => 'demo@medibookai.com']);

        // Seeded content present
        $this->assertGreaterThanOrEqual(2, $tenant->dentists()->withoutGlobalScope('tenant')->count());
        $this->assertGreaterThanOrEqual(3, \App\Models\Patient::withoutGlobalScope('tenant')->where('tenant_id', $tenant->id)->count());
    }

    public function test_demo_excluded_from_mrr_and_counts(): void
    {
        $this->artisan('medibook:demo')->assertSuccessful();

        $metrics = app(MetricsService::class);
        $this->assertSame(0, $metrics->mrr());
        $this->assertSame(0, $metrics->counts()['trialing']); // demo trial excluded
    }

    public function test_fresh_rebuilds_without_duplicating(): void
    {
        $this->artisan('medibook:demo')->assertSuccessful();
        $this->artisan('medibook:demo', ['--fresh' => true])->assertSuccessful();

        $this->assertSame(1, Tenant::withoutGlobalScope('tenant')->where('slug', 'demo')->count());
    }
}
