<?php

namespace Tests\Feature;

use App\Http\Middleware\EnsurePlatformSession;
use App\Models\Branch;
use App\Models\Department;
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 CareSelectLabelsTest extends TestCase
{
    use RefreshDatabase;

    public function test_appointment_form_shows_branch_on_department_options_and_searchable_patient(): void
    {
        $this->withoutMiddleware(EnsurePlatformSession::class);

        $owner = User::create([
            'public_id' => 'select-owner',
            'name' => 'Owner',
            'email' => 'select-owner@example.com',
        ]);

        $organization = Organization::create([
            'owner_ref' => $owner->public_id,
            'name' => 'Select Clinic',
            'slug' => 'select-clinic',
            'settings' => [
                'onboarded' => true,
                'facility_type' => 'clinic',
                'plan' => 'pro',
                'plan_expires_at' => now()->addMonth()->toIso8601String(),
            ],
        ]);

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

        $east = Branch::create([
            'owner_ref' => $owner->public_id,
            'organization_id' => $organization->id,
            'name' => 'East',
            'is_active' => true,
        ]);
        $west = Branch::create([
            'owner_ref' => $owner->public_id,
            'organization_id' => $organization->id,
            'name' => 'West',
            'is_active' => true,
        ]);

        Department::create([
            'owner_ref' => $owner->public_id,
            'branch_id' => $east->id,
            'name' => 'Dentistry',
            'type' => 'dental',
            'is_active' => true,
        ]);
        Department::create([
            'owner_ref' => $owner->public_id,
            'branch_id' => $west->id,
            'name' => 'Dentistry',
            'type' => 'dental',
            'is_active' => true,
        ]);

        Patient::create([
            'owner_ref' => $owner->public_id,
            'organization_id' => $organization->id,
            'branch_id' => $east->id,
            'patient_number' => 'P-SEL-1',
            'first_name' => 'Kofi',
            'last_name' => 'Mensah',
            'gender' => 'male',
            'date_of_birth' => '1990-01-01',
            'phone' => '0244111222',
        ]);

        $this->actingAs($owner)
            ->get(route('care.appointments.create'))
            ->assertOk()
            ->assertSee('Dentistry — East')
            ->assertSee('Dentistry — West')
            ->assertSee('Search name, number, phone')
            ->assertSee('Kofi Mensah (P-SEL-1)');
    }

    public function test_department_label_for_select_includes_branch(): void
    {
        $branch = new Branch(['name' => 'Main']);
        $department = new Department(['name' => 'Cardiology']);
        $department->setRelation('branch', $branch);

        $this->assertSame('Cardiology — Main', $department->labelForSelect());
    }
}
