<?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 Illuminate\Support\Facades\Http;
use Tests\TestCase;

class CareSuiteMessagingTest extends TestCase
{
    use RefreshDatabase;

    protected User $user;

    protected Organization $organization;

    protected Branch $branch;

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

        $this->user = User::create([
            'public_id' => 'test-suite-user-001',
            'name' => 'Suite User',
            'email' => 'suite@example.com',
        ]);

        $this->organization = Organization::create([
            'owner_ref' => $this->user->public_id,
            'name' => 'Suite Clinic',
            'slug' => 'suite-clinic',
            'timezone' => 'UTC',
            'settings' => ['onboarded' => true],
        ]);

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

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

        Department::create([
            'owner_ref' => $this->user->public_id,
            'branch_id' => $this->branch->id,
            'name' => 'General Outpatient',
            'type' => 'outpatient',
            'is_active' => true,
        ]);
    }

    public function test_patient_email_uses_suite_channel_when_platform_configured(): void
    {
        config([
            'smtp.platform_api_key' => 'care-smtp-key',
            'smtp.platform_api_url' => 'https://platform.test/api/smtp',
        ]);

        Http::fake([
            'platform.test/api/smtp/messages/send' => Http::response([
                'success' => true,
                'path' => 'suite',
                'message_id' => 'suite-1',
            ]),
        ]);

        $patient = Patient::create([
            'uuid' => (string) \Illuminate\Support\Str::uuid(),
            'owner_ref' => $this->user->public_id,
            'organization_id' => $this->organization->id,
            'branch_id' => $this->branch->id,
            'patient_number' => 'LC-SUITE-001',
            'first_name' => 'Ama',
            'last_name' => 'Suite',
            'email' => 'ama.suite@example.com',
        ]);

        $this->actingAs($this->user)
            ->post(route('care.patients.email', $patient), [
                'subject' => 'Hello',
                'body' => 'Suite email body',
            ])
            ->assertRedirect()
            ->assertSessionHas('success');

        Http::assertSent(function ($request) {
            return str_contains($request->url(), '/messages/send')
                && ($request['channel'] ?? null) === 'suite'
                && ($request['user'] ?? null) === $this->user->public_id
                && ($request['from_name'] ?? null) === 'Suite Clinic';
        });
    }

    public function test_patient_sms_uses_suite_channel_when_platform_configured(): void
    {
        config([
            'sms.platform_api_key' => 'care-sms-key',
            'sms.platform_api_url' => 'https://platform.test/api',
        ]);

        Http::fake([
            'platform.test/api/sms/messages/send' => Http::response([
                'success' => true,
                'path' => 'suite',
            ]),
        ]);

        $patient = Patient::create([
            'uuid' => (string) \Illuminate\Support\Str::uuid(),
            'owner_ref' => $this->user->public_id,
            'organization_id' => $this->organization->id,
            'branch_id' => $this->branch->id,
            'patient_number' => 'LC-SUITE-002',
            'first_name' => 'Kofi',
            'last_name' => 'Suite',
            'phone' => '0244000111',
        ]);

        $this->actingAs($this->user)
            ->post(route('care.patients.sms', $patient), [
                'message' => 'Suite SMS body',
            ])
            ->assertRedirect()
            ->assertSessionHas('success');

        Http::assertSent(function ($request) {
            $data = $request->data();

            return str_contains($request->url(), '/sms/messages/send')
                && ($data['channel'] ?? null) === 'suite'
                && ! array_key_exists('sms_service_id', $data);
        });
    }
}
