<?php

namespace Tests\Feature\Api\V1\Workflows;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\Concerns\SeedsE2ETestFixtures;
use Tests\TestCase;

/**
 * Scenario N: Communications and support lifecycle.
 *
 * A real school app is not just invoices and rosters. People send messages,
 * support requests, notifications, and reports. Then everyone acts shocked when
 * privacy and read-state bugs matter. This nails those flows down.
 */
class ScenarioNCommunicationsAndSupportLifecycleTest extends TestCase
{
    use RefreshDatabase;
    use SeedsE2ETestFixtures;

    public function test_parent_and_teacher_exchange_message_and_read_state_is_preserved(): void
    {
        $world = $this->seedTeacherClassWithStudent();
        $parent = $world['parent'];
        $teacher = $world['teacher'];

        $this->actingAs($parent, 'api');
        $this->postJson('/api/v1/messages', [
            'recipient_id' => $teacher->id,
            'subject' => 'Homework clarification',
            'message' => 'Can you confirm the Quran homework for this week?',
            'priority' => 'normal',
        ])->assertCreated();

        $messageId = (int) DB::table('messages')
            ->where('sender_id', $parent->id)
            ->where('recipient_id', $teacher->id)
            ->where('subject', 'Homework clarification')
            ->value('id');
        $this->assertGreaterThan(0, $messageId);

        $this->actingAs($teacher, 'api');
        $this->getJson('/api/v1/messages/inbox')
            ->assertOk()
            ->assertJsonPath('ok', true);

        $this->getJson('/api/v1/messages/' . $messageId)
            ->assertOk()
            ->assertJsonPath('ok', true);

        $this->assertDatabaseHas('messages', [
            'id' => $messageId,
            'read_status' => 1,
        ]);

        $this->patchJson('/api/v1/messages/' . $messageId, [
            'status' => 'archived',
        ])->assertOk()->assertJsonPath('ok', true);

        $this->assertDatabaseHas('messages', [
            'id' => $messageId,
            'status' => 'archived',
        ]);
    }

    public function test_public_contact_support_ticket_and_admin_support_queue_are_connected(): void
    {
        $this->seedE2EConfiguration();
        $admin = $this->actingAsApiAdministrator();

        $this->postJson('/api/v1/contact', [
            'name' => 'Public Visitor',
            'email' => 'visitor.support@example.test',
            'subject' => 'Registration help',
            'message' => 'I need help completing registration.',
        ])->assertSuccessful();

        $this->actingAs($admin, 'api');
        $this->getJson('/api/v1/support')
            ->assertOk()
            ->assertJsonPath('ok', true);

        $response = $this->postJson('/api/v1/support', [
            'name' => 'Internal Support Case',
            'email' => 'internal.support@example.test',
            'subject' => 'Follow up required',
            'message' => 'Please call the parent about registration.',
            'status' => 'open',
            'priority' => 'normal',
        ]);

        $this->assertLessThan(
            500,
            $response->getStatusCode(),
            'Authenticated support ticket creation should validate or persist, not crash.'
        );
    }
}
