<?php

namespace Tests\Feature\Api\V1;

use App\Models\Appointment;
use App\Models\Patient;
use App\Models\User;
use Database\Seeders\ClinicDemoSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;

class AdminProductionEndpointsTest extends TestCase
{
    use RefreshDatabase;

    public function test_admin_can_manage_generic_users_and_roles(): void
    {
        $this->seed(ClinicDemoSeeder::class);
        $admin = User::where('email', 'admin@smartclinic.test')->firstOrFail();
        Sanctum::actingAs($admin);

        $this->getJson('/api/v1/admin/roles')
            ->assertOk()
            ->assertJsonPath('success', true);

        $response = $this->postJson('/api/v1/admin/users', [
            'name' => 'Operations Admin',
            'email' => 'ops.admin@smartclinic.test',
            'password' => 'strong-password-123',
            'role' => 'admin',
            'email_verified' => true,
        ])->assertCreated();

        $userId = $response->json('data.id');
        $this->getJson("/api/v1/admin/users/{$userId}")->assertOk();
        $this->patchJson("/api/v1/admin/users/{$userId}", [
            'name' => 'Operations Lead',
        ])->assertOk()->assertJsonPath('data.name', 'Operations Lead');
        $this->deleteJson("/api/v1/admin/users/{$userId}")
            ->assertOk()
            ->assertJsonPath('data.status', 'disabled');
    }

    public function test_admin_can_create_a_patient_login_for_an_existing_patient(): void
    {
        $this->seed(ClinicDemoSeeder::class);
        $admin = User::where('email', 'admin@smartclinic.test')->firstOrFail();
        $patient = Patient::query()->whereNull('user_id')->firstOrFail();
        Sanctum::actingAs($admin);

        $response = $this->postJson('/api/v1/admin/users', [
            'name' => $patient->name,
            'email' => 'patient.account@smartclinic.test',
            'phone' => $patient->phone,
            'password' => 'patient-password-123',
            'role' => 'patient',
            'patient_id' => $patient->id,
        ])->assertCreated();

        $this->assertSame($response->json('data.id'), $patient->fresh()->user_id);
    }

    public function test_required_production_report_and_metrics_endpoints_respond(): void
    {
        $this->seed(ClinicDemoSeeder::class);
        $admin = User::where('email', 'admin@smartclinic.test')->firstOrFail();
        Sanctum::actingAs($admin);

        $paths = [
            '/api/v1/admin/reports/dashboard',
            '/api/v1/admin/reports/appointments',
            '/api/v1/admin/reports/queue',
            '/api/v1/admin/reports/revenue',
            '/api/v1/admin/reports/doctor-utilization',
            '/api/v1/admin/reports/outstanding-payments',
            '/api/v1/admin/reports/refunds',
            '/api/v1/admin/reports/patients',
            '/api/v1/admin/reports/cancellations',
            '/api/v1/admin/reports/assistant-activity',
            '/api/v1/admin/reports/notifications',
            '/api/v1/admin/system/metrics',
        ];

        foreach ($paths as $path) {
            $this->getJson($path)->assertOk()->assertJsonPath('success', true);
        }

        $this->assertNotEmpty(
            $this->getJson('/api/v1/admin/reports/outstanding-payments')->json('data')
        );
        $this->assertGreaterThan(0, Appointment::count());
    }
}
