<?php

namespace Tests\Feature\Api;

use App\Models\User;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;

class ValidationTest extends TestCase
{
    public function test_absence_report_validates_required_fields(): void
    {
        $user = User::first();
        if (!$user) {
            $this->markTestSkipped('No users in database');
        }

        Sanctum::actingAs($user, ['mobile:*']);

        $response = $this->postJson('/api/v1/absences/report', []);

        $response->assertUnprocessable()
            ->assertJsonValidationErrors(['participant_uuid', 'session_id', 'reason']);
    }

    public function test_message_send_validates_required_fields(): void
    {
        $user = User::first();
        if (!$user) {
            $this->markTestSkipped('No users in database');
        }

        Sanctum::actingAs($user, ['mobile:*']);

        $response = $this->postJson('/api/v1/messages/send', []);

        $response->assertUnprocessable()
            ->assertJsonValidationErrors(['subject', 'body']);
    }

    public function test_service_request_validates_type(): void
    {
        $user = User::first();
        if (!$user) {
            $this->markTestSkipped('No users in database');
        }

        Sanctum::actingAs($user, ['mobile:*']);

        $response = $this->postJson('/api/v1/service-requests', [
            'participant_uuid' => 'fake-uuid',
            'type' => 'invalid_type',
            'reason' => 'test',
        ]);

        $response->assertUnprocessable()
            ->assertJsonValidationErrors(['type']);
    }

    public function test_order_create_validates_required_fields(): void
    {
        $user = User::first();
        if (!$user) {
            $this->markTestSkipped('No users in database');
        }

        Sanctum::actingAs($user, ['mobile:*']);

        $response = $this->postJson('/api/v1/orders/create', []);

        $response->assertUnprocessable()
            ->assertJsonValidationErrors(['participant_uuid', 'product_id', 'payment_method']);
    }

    public function test_payment_initiate_validates_invoice_uuid(): void
    {
        $user = User::first();
        if (!$user) {
            $this->markTestSkipped('No users in database');
        }

        Sanctum::actingAs($user, ['mobile:*']);

        $response = $this->postJson('/api/v1/payments/initiate', []);

        $response->assertUnprocessable()
            ->assertJsonValidationErrors(['invoice_uuid']);
    }

    public function test_photo_upload_validates_file(): void
    {
        $user = User::first();
        if (!$user) {
            $this->markTestSkipped('No users in database');
        }

        Sanctum::actingAs($user, ['mobile:*']);

        $response = $this->postJson('/api/v1/profile/photo', []);

        $response->assertUnprocessable()
            ->assertJsonValidationErrors(['photo']);
    }

    public function test_profile_update_rejects_empty_payload(): void
    {
        $user = User::first();
        if (!$user) {
            $this->markTestSkipped('No users in database');
        }

        Sanctum::actingAs($user, ['mobile:*']);

        $response = $this->patchJson('/api/v1/profile', []);

        // Either 422 (no changes) or 404 (no person) — both are valid
        $this->assertContains($response->status(), [404, 422]);
    }
}
