<?php

use App\Filament\Pages\Documents;
use App\Models\User;
use Illuminate\Support\Facades\Http;
use Livewire\Livewire;

beforeEach(function () {
    config([
        'logistics.base_url' => 'http://test-server.local',
        'logistics.api_key' => 'test-api-key',
        'logistics.folder' => 'testfolder',
        'logistics.timeout' => 30,
        'logistics.connect_timeout' => 10,
        'logistics.retry.times' => 1,
        'logistics.retry.sleep_ms' => 0,
    ]);

    $this->actingAs(User::factory()->create());
});

it('defaults to read mode', function () {
    Http::fake(['*' => Http::response(['data' => [], 'metadata' => [], 'error' => null])]);

    Livewire::test(Documents::class)
        ->assertSet('mode', 'read');
});

it('can switch between read and write modes', function () {
    Http::fake(['*' => Http::response(['data' => [], 'metadata' => [], 'error' => null])]);

    Livewire::test(Documents::class)
        ->set('mode', 'write')
        ->assertSet('mode', 'write')
        ->set('mode', 'read')
        ->assertSet('mode', 'read');
});

it('searches documents via document_list', function () {
    Http::fake([
        '*/document_list' => Http::response([
            'data' => [['jnl' => 'VEN', 'number' => '1']],
            'metadata' => ['rowcount' => 1, 'issuccess' => true],
            'error' => null,
        ]),
    ]);

    Livewire::test(Documents::class)
        ->set('select', 'jnl,number')
        ->set('thirdId', 'CUST001')
        ->call('searchDocuments')
        ->assertSet('hasSearchedDocs', true)
        ->assertSet('data', [['jnl' => 'VEN', 'number' => '1']]);
});

it('sends results parameter to document_list', function () {
    Http::fake([
        '*/document_list' => Http::response([
            'data' => [['jnl' => 'VEN', 'number' => '1']],
            'metadata' => ['rowcount' => 1, 'issuccess' => true],
            'error' => null,
        ]),
    ]);

    Livewire::test(Documents::class)
        ->set('select', 'jnl,number')
        ->set('results', '20')
        ->call('searchDocuments');

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

        return str_contains($request->url(), 'document_list')
            && $body['results'] === '20';
    });
});

it('omits results parameter when empty', function () {
    Http::fake([
        '*/document_list' => Http::response([
            'data' => [['jnl' => 'VEN', 'number' => '1']],
            'metadata' => ['rowcount' => 1, 'issuccess' => true],
            'error' => null,
        ]),
    ]);

    Livewire::test(Documents::class)
        ->set('select', 'jnl,number')
        ->set('results', '')
        ->call('searchDocuments');

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

        return str_contains($request->url(), 'document_list')
            && ! array_key_exists('results', $body);
    });
});

it('gets document detail', function () {
    Http::fake([
        '*/document_detail' => Http::response([
            'data' => ['jnl' => 'VEN', 'number' => '1', 'thirdid' => 'CUST001'],
            'metadata' => ['rowcount' => 1, 'issuccess' => true],
            'error' => null,
        ]),
    ]);

    Livewire::test(Documents::class)
        ->set('detailJnl', 'VEN')
        ->set('detailNumber', '1')
        ->call('getDocumentDetail')
        ->assertSet('detailData', ['jnl' => 'VEN', 'number' => '1', 'thirdid' => 'CUST001']);
});

it('gets status list for a journal', function () {
    Http::fake([
        '*/Document_GetStatusList' => Http::response([
            'data' => [['status' => 'DRAFT'], ['status' => 'VALID']],
            'metadata' => ['rowcount' => 2, 'issuccess' => true],
            'error' => null,
        ]),
    ]);

    Livewire::test(Documents::class)
        ->set('statusJnl', 'VEN')
        ->call('getStatusList')
        ->assertSet('statusData', [['status' => 'DRAFT'], ['status' => 'VALID']]);
});

it('gets unit price and vat', function () {
    Http::fake([
        '*/Document_GetUnitPriceAndVat' => Http::response([
            'data' => ['price' => '10.00', 'vat' => '21'],
            'metadata' => ['rowcount' => 1, 'issuccess' => true],
            'error' => null,
        ]),
    ]);

    Livewire::test(Documents::class)
        ->set('priceArtId', 'ART001')
        ->set('priceQty', '2')
        ->set('priceJnl', 'VEN')
        ->set('priceThirdId', 'CUST001')
        ->set('priceDate', '2026-02-20')
        ->call('getUnitPriceAndVat')
        ->assertSet('priceData', ['price' => '10.00', 'vat' => '21']);
});

it('gets due date', function () {
    Http::fake([
        '*/Document_GetDueDate' => Http::response([
            'data' => ['duedate' => '2026-03-22'],
            'metadata' => ['rowcount' => 1, 'issuccess' => true],
            'error' => null,
        ]),
    ]);

    Livewire::test(Documents::class)
        ->set('payDelay', '30J')
        ->set('dueDateInput', '2026-02-20')
        ->call('getDueDate')
        ->assertSet('dueDateData', ['duedate' => '2026-03-22']);
});

it('gets attach list thumbnail', function () {
    Http::fake([
        '*/Document_GetAttachListThumbnail' => Http::response([
            'data' => [['filename' => 'photo.jpg']],
            'metadata' => ['rowcount' => 1, 'issuccess' => true],
            'error' => null,
        ]),
    ]);

    Livewire::test(Documents::class)
        ->set('attachJnl', 'VEN')
        ->set('attachNumber', '2026/0001')
        ->call('getAttachListThumbnail')
        ->assertSet('attachData', [['filename' => 'photo.jpg']]);
});

it('calls Document_GetPDF endpoint', function () {
    Http::fake([
        '*/Document_GetPDF' => Http::response([
            'data' => null,
            'metadata' => ['rowcount' => 0, 'issuccess' => false],
            'error' => 'Layout not found',
        ]),
    ]);

    $component = Livewire::test(Documents::class)
        ->set('pdfJnl', 'VEN')
        ->set('pdfNumber', '2026/0001')
        ->set('pdfLayout', 'DEFAULT')
        ->call('getPdf');

    expect($component->get('errorMessage'))->toContain('Layout not found');
});

it('adds a document via document_add', function () {
    Http::fake([
        '*/document_add' => Http::response([
            'data' => ['number' => '2026/0002'],
            'metadata' => ['rowcount' => 1, 'issuccess' => true],
            'error' => null,
        ]),
    ]);

    Livewire::test(Documents::class)
        ->set('mode', 'write')
        ->set('addThirdId', 'CUST001')
        ->set('addDate', '2026-02-20')
        ->set('addArtIds', 'ART001,ART002')
        ->set('addQty', '2,5')
        ->set('addSalePrice', '10.00,25.50')
        ->set('addJnl', 'VEN')
        ->call('addDocument')
        ->assertSet('addResult', ['number' => '2026/0002']);

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

        return str_contains($request->url(), 'document_add')
            && $body['Artid'] === ['ART001', 'ART002']
            && $body['Qty'] === ['2', '5']
            && $body['Saleprice'] === ['10.00', '25.50'];
    });
});

it('modifies a document via document_mod', function () {
    Http::fake([
        '*/document_mod' => Http::response([
            'data' => ['updated' => true],
            'metadata' => ['rowcount' => 1, 'issuccess' => true],
            'error' => null,
        ]),
    ]);

    Livewire::test(Documents::class)
        ->set('mode', 'write')
        ->set('modNumber', '2026/0001')
        ->set('modJnl', 'VEN')
        ->set('modArtIds', 'ART001,ART003')
        ->set('modQty', '3,1')
        ->set('modSalePrice', '10.00,50.00')
        ->call('modDocument')
        ->assertSet('modResult', ['updated' => true]);

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

        return str_contains($request->url(), 'document_mod')
            && $body['number'] === '2026/0001'
            && $body['JNL'] === 'VEN';
    });
});

it('shows validation error when statusJnl is empty', function () {
    Http::fake();

    Livewire::test(Documents::class)
        ->call('getStatusList')
        ->assertSet('hasStatus', false)
        ->assertSet('errorMessage', 'Le champ code journal (jnl) est obligatoire.');

    Http::assertNothingSent();
});

it('shows validation error when document_detail fields are empty', function () {
    Http::fake();

    Livewire::test(Documents::class)
        ->call('getDocumentDetail')
        ->assertSet('hasDetail', false)
        ->assertSet('errorMessage', 'Les champs code journal (jnl) et numero de document (number) sont obligatoires.');

    Http::assertNothingSent();
});

it('shows validation error when getUnitPriceAndVat fields are empty', function () {
    Http::fake();

    Livewire::test(Documents::class)
        ->call('getUnitPriceAndVat')
        ->assertSet('hasPrice', false)
        ->assertSet('errorMessage', 'Tous les champs sont obligatoires : ARTID, QTY (format string), JNL, THIRDID et DATE.');

    Http::assertNothingSent();
});

it('shows validation error when getDueDate fields are empty', function () {
    Http::fake();

    Livewire::test(Documents::class)
        ->call('getDueDate')
        ->assertSet('hasDueDate', false)
        ->assertSet('errorMessage', 'Les champs delai de paiement (paydelay) et date de depart (date) sont obligatoires.');

    Http::assertNothingSent();
});

it('shows validation error when getAttachListThumbnail fields are empty', function () {
    Http::fake();

    Livewire::test(Documents::class)
        ->call('getAttachListThumbnail')
        ->assertSet('hasAttach', false)
        ->assertSet('errorMessage', 'Les champs code journal (JNL) et numero de document (NUMBER) sont obligatoires.');

    Http::assertNothingSent();
});

it('shows validation error when getPdf fields are empty', function () {
    Http::fake();

    Livewire::test(Documents::class)
        ->call('getPdf')
        ->assertSet('hasPdf', false)
        ->assertSet('errorMessage', 'Les champs code journal (JNL), numero de document (NUMBER) et mise en page (LAYOUT) sont obligatoires.');

    Http::assertNothingSent();
});

it('shows validation error when addDocument required fields are empty', function () {
    Http::fake();

    Livewire::test(Documents::class)
        ->set('mode', 'write')
        ->call('addDocument')
        ->assertSet('hasAdded', false)
        ->assertSet('errorMessage', 'Les champs ThirdId, Date, Artid, Qty, Saleprice et JNL sont obligatoires.');

    Http::assertNothingSent();
});

it('shows validation error when modDocument required fields are empty', function () {
    Http::fake();

    Livewire::test(Documents::class)
        ->set('mode', 'write')
        ->call('modDocument')
        ->assertSet('hasModified', false)
        ->assertSet('errorMessage', 'Les champs numero de document (number) et code journal (JNL) sont obligatoires.');

    Http::assertNothingSent();
});

it('displays error message on API failure', function () {
    Http::fake([
        '*/document_list' => Http::response([
            'data' => null,
            'metadata' => ['rowcount' => 0, 'issuccess' => false],
            'error' => 'Invalid API key',
        ]),
    ]);

    $component = Livewire::test(Documents::class)
        ->call('searchDocuments');

    expect($component->get('errorMessage'))->toContain('Invalid API key');
});
