<?php

namespace Tests\Feature;

use App\Http\Middleware\EnsurePlatformSession;
use App\Models\Branch;
use App\Models\Member;
use App\Models\Organization;
use App\Models\Patient;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class CareAdminOverviewTest extends TestCase
{
    use RefreshDatabase;

    protected User $admin;

    protected Organization $organization;

    protected Branch $branch;

    protected function setUp(): void
    {
        parent::setUp();
        $this->withoutMiddleware(EnsurePlatformSession::class);

        $this->admin = User::create([
            'public_id' => 'admin-overview',
            'name' => 'Hospital Admin',
            'email' => 'admin-overview@example.com',
        ]);

        $this->organization = Organization::create([
            'owner_ref' => $this->admin->public_id,
            'name' => 'Overview Hospital',
            'slug' => 'overview-hospital',
            'settings' => [
                'onboarded' => true,
                'facility_type' => 'hospital',
                'plan' => 'pro',
                'plan_expires_at' => now()->addMonth()->toIso8601String(),
            ],
        ]);

        Member::create([
            'owner_ref' => $this->admin->public_id,
            'organization_id' => $this->organization->id,
            'user_ref' => $this->admin->public_id,
            'role' => 'hospital_admin',
        ]);

        $this->branch = Branch::create([
            'owner_ref' => $this->admin->public_id,
            'organization_id' => $this->organization->id,
            'name' => 'Main',
            'is_active' => true,
        ]);

        Patient::create([
            'owner_ref' => $this->admin->public_id,
            'organization_id' => $this->organization->id,
            'branch_id' => $this->branch->id,
            'patient_number' => 'P-OV-1',
            'first_name' => 'Kwame',
            'last_name' => 'Mensah',
            'gender' => 'male',
            'date_of_birth' => '1990-01-01',
        ]);
    }

    public function test_admin_patients_page_is_analytics_not_a_directory_list(): void
    {
        $this->actingAs($this->admin)
            ->get(route('care.patients.index'))
            ->assertOk()
            ->assertSee('Registered patients')
            ->assertSee('Patients by branch')
            ->assertDontSee('Barcode / QR scan')
            ->assertDontSee('P-OV-1');

        $this->actingAs($this->admin)
            ->get(route('care.patients.index', ['view' => 'list']))
            ->assertOk()
            ->assertSee('P-OV-1')
            ->assertSee('Kwame');
    }

    public function test_admin_appointments_billing_inventory_and_reports_are_data_driven(): void
    {
        $this->actingAs($this->admin)
            ->get(route('care.appointments.index'))
            ->assertOk()
            ->assertSee('Scheduled today')
            ->assertSee('Appointments by status')
            ->assertDontSee('Walk-in');

        $this->actingAs($this->admin)
            ->get(route('care.bills.index'))
            ->assertOk()
            ->assertSee('Revenue today')
            ->assertSee('Outstanding')
            ->assertSee('Invoices by status (current book)')
            ->assertDontSee('Call next');

        $this->actingAs($this->admin)
            ->get(route('care.pharmacy.drugs.index'))
            ->assertOk()
            ->assertSee('Catalog items')
            ->assertSee('Est. stock value')
            ->assertDontSee('Search drugs');

        $this->actingAs($this->admin)
            ->get(route('care.reports.index'))
            ->assertOk()
            ->assertSee('New patients MTD')
            ->assertSee('Appointments MTD')
            ->assertSee('Lab pending')
            ->assertSee('Detailed reports')
            ->assertSee('Finance');
    }
}
