<?php

namespace Tests\Feature;

use App\Http\Middleware\EnsurePlatformSession;
use App\Models\Member;
use App\Models\Organization;
use App\Models\User;
use App\Services\Care\CarePricingService;
use App\Services\Care\SpecialtyModuleService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class CareServicePricingSettingsTest extends TestCase
{
    use RefreshDatabase;

    protected User $owner;

    protected Organization $organization;

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

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

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

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

    public function test_gp_pricing_page_saves_org_overrides(): void
    {
        $this->actingAs($this->owner)
            ->get(route('care.settings.gp-pricing'))
            ->assertOk()
            ->assertSee('GP consultation')
            ->assertSee('Patient card');

        $this->actingAs($this->owner)
            ->put(route('care.settings.gp-pricing.update'), [
                'services' => [
                    ['code' => 'gp.consultation', 'amount' => 75.50],
                    ['code' => 'gp.follow_up', 'amount' => 40],
                    ['code' => 'gp.patient_card', 'amount' => 25],
                    ['code' => 'gp.vitals', 'amount' => 10],
                    ['code' => 'gp.procedure', 'amount' => 50],
                    ['code' => 'gp.home_visit', 'amount' => 150],
                ],
            ])
            ->assertRedirect(route('care.settings.gp-pricing'));

        $this->organization->refresh();
        $pricing = app(CarePricingService::class);
        $this->assertSame(7550, $pricing->consultationFee($this->organization));
        $this->assertSame(2500, $pricing->patientCardFee($this->organization));
    }

    public function test_specialty_module_settings_update_service_prices(): void
    {
        app(SpecialtyModuleService::class)->activate(
            $this->organization,
            $this->owner->public_id,
            'dentistry',
        );

        $this->actingAs($this->owner)
            ->get(route('care.settings.modules.show', 'dentistry'))
            ->assertOk()
            ->assertSee('Dental consultation')
            ->assertSee('Service prices');

        $this->actingAs($this->owner)
            ->put(route('care.settings.modules.services.update', 'dentistry'), [
                'services' => [
                    ['code' => 'den.consult', 'amount' => 80, 'label' => 'Dental consultation'],
                    ['code' => 'den.fill', 'amount' => 150, 'label' => 'Filling'],
                ],
            ])
            ->assertRedirect(route('care.settings.modules.show', 'dentistry'));

        $this->organization->refresh();
        $services = collect(app(\App\Services\Care\SpecialtyShellService::class)
            ->provisionedServices($this->organization, 'dentistry'))
            ->keyBy('code');

        $this->assertSame(8000, (int) $services['den.consult']['amount_minor']);
        $this->assertSame(15000, (int) $services['den.fill']['amount_minor']);
    }

    public function test_admin_dashboard_links_modules_to_settings(): void
    {
        app(SpecialtyModuleService::class)->activate(
            $this->organization,
            $this->owner->public_id,
            'dentistry',
        );

        $this->actingAs($this->owner)
            ->get(route('care.dashboard'))
            ->assertOk()
            ->assertSee(route('care.settings.modules.show', 'dentistry', false))
            ->assertSee('Settings & pricing');
    }
}
