<?php

namespace Tests\Feature;

use App\Domain\Subscription\UsageService;
use App\Models\Dentist;
use App\Models\Patient;
use App\Models\PatientFile;
use App\Models\Plan;
use App\Models\Tenant;
use App\Models\User;
use App\Support\Tenancy;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class OwnerUsagePanelTest extends TestCase
{
    use RefreshDatabase;

    protected Tenant $tenant;

    protected function setUp(): void
    {
        parent::setUp();
        $this->seed();
        $this->tenant = Tenant::create(['name' => 'C', 'slug' => 'c']);
        $this->tenant->subscription()->create([
            'plan_id' => Plan::first()->id, 'status' => 'active',
            'starts_at' => now(), 'current_period_ends_at' => now()->addMonth(),
        ]);
        app(Tenancy::class)->set($this->tenant);
    }

    public function test_staff_and_dentist_meters(): void
    {
        User::withoutEvents(fn () => User::create([
            'tenant_id' => $this->tenant->id, 'name' => 'O', 'email' => 'o@x.com', 'password' => 'password123',
        ]));
        Dentist::factory()->count(2)->create();

        $svc = app(UsageService::class);
        $this->assertSame(1, $svc->staff($this->tenant)['used']);
        $this->assertSame(8, $svc->staff($this->tenant)['limit']);
        $this->assertSame(2, $svc->dentists($this->tenant)['used']);
        $this->assertSame(5, $svc->dentists($this->tenant)['limit']);
    }

    public function test_storage_meter_sums_file_sizes(): void
    {
        $patient = Patient::factory()->create();
        PatientFile::create([
            'tenant_id' => $this->tenant->id, 'patient_id' => $patient->id,
            'type' => 'xray', 'path' => 'x/a.jpg', 'original_name' => 'a.jpg',
            'mime' => 'image/jpeg', 'size' => 5 * 1048576, // 5 MB
        ]);
        PatientFile::create([
            'tenant_id' => $this->tenant->id, 'patient_id' => $patient->id,
            'type' => 'xray', 'path' => 'x/b.jpg', 'original_name' => 'b.jpg',
            'mime' => 'image/jpeg', 'size' => 3 * 1048576, // 3 MB
        ]);

        $storage = app(UsageService::class)->storage($this->tenant);
        $this->assertSame(8, $storage['used']);          // MB
        $this->assertSame(10 * 1024, $storage['limit']); // 10 GB in MB
        $this->assertSame('ok', $storage['level']);
    }

    public function test_billing_page_shows_owner_kpis(): void
    {
        $this->tenant->subscription->update(['status' => 'trialing', 'current_period_ends_at' => now()->addDays(10)]);
        $owner = User::withoutEvents(fn () => User::create([
            'tenant_id' => $this->tenant->id, 'name' => 'O', 'email' => 'o2@x.com', 'password' => 'password123',
        ]));
        $owner->assignRole('clinic_owner');
        app(Tenancy::class)->forget();

        $this->actingAs($owner)
            ->get('http://c.'.config('tenancy.central_domain').'/billing')
            ->assertOk()
            ->assertSee(__('Plan usage'))
            ->assertSee(__('Storage'))
            ->assertSee(__('Staff seats'))
            ->assertSee(__('trial days left'));
    }
}
