<?php

namespace Tests\Feature;

use App\Enums\UserKycStatus;
use App\Models\User;
use App\Models\UserKycProfile;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;

class AdminKycDocumentDownloadTest extends TestCase
{
    use RefreshDatabase;

    /**
     * @return array{0: User, 1: string}
     */
    private function investorWithFrontDocument(): array
    {
        $investor = User::factory()->create([
            'kyc_status' => UserKycStatus::PendingReview,
        ]);

        $path = sprintf('users/%d/test/front.pdf', $investor->id);
        Storage::disk('kyc')->put($path, 'fake-payload');

        UserKycProfile::query()->create([
            'user_id' => $investor->id,
            'legal_first_name' => 'Ada',
            'legal_last_name' => 'Investor',
            'date_of_birth' => '1991-06-01',
            'document_type' => 'national_id',
            'document_number' => 'ID-991',
            'document_issuing_country' => 'EC',
            'country_of_residence' => 'EC',
            'address_line1' => 'Av. Ejemplo 1',
            'address_line2' => null,
            'city' => 'Quito',
            'region_state' => 'Pichincha',
            'postal_code' => '170101',
            'occupation' => null,
            'document_front_path' => $path,
            'document_back_path' => null,
            'selfie_document_path' => null,
        ]);

        return [$investor, $path];
    }

    public function test_guest_is_redirected_to_login(): void
    {
        Storage::fake('kyc');

        [$investor] = $this->investorWithFrontDocument();

        $this->get(route('admin.kyc.document-download', [
            'user' => $investor,
            'document' => 'front',
        ]))->assertRedirect(route('login'));
    }

    public function test_non_admin_forbidden(): void
    {
        Storage::fake('kyc');

        [$investor] = $this->investorWithFrontDocument();

        $other = User::factory()->create();

        $response = $this->actingAs($other)->get(route('admin.kyc.document-download', [
            'user' => $investor,
            'document' => 'front',
        ]));

        $response->assertForbidden();
    }

    public function test_admin_downloads_front_file(): void
    {
        Storage::fake('kyc');

        [$investor] = $this->investorWithFrontDocument();
        $admin = User::factory()->admin()->create();

        $response = $this->actingAs($admin)->get(route('admin.kyc.document-download', [
            'user' => $investor,
            'document' => 'front',
        ]));

        $response->assertOk();
        $response->assertDownload('kyc-usuario-'.$investor->id.'-frontal.pdf');
        $this->assertSame('fake-payload', $response->streamedContent());
    }

    public function test_download_target_must_not_be_admin(): void
    {
        Storage::fake('kyc');

        $targetAdmin = User::factory()->admin()->create();
        $admin = User::factory()->admin()->create();

        $response = $this->actingAs($admin)->get(route('admin.kyc.document-download', [
            'user' => $targetAdmin,
            'document' => 'front',
        ]));

        $response->assertNotFound();
    }

    public function test_missing_optional_back_returns_not_found(): void
    {
        Storage::fake('kyc');

        [$investor] = $this->investorWithFrontDocument();

        $admin = User::factory()->admin()->create();

        $response = $this->actingAs($admin)->get(route('admin.kyc.document-download', [
            'user' => $investor,
            'document' => 'back',
        ]));

        $response->assertNotFound();
    }
}
