<?php

use App\Exceptions\LogisticsApiException;
use App\Services\LogisticsService;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;

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,
    ]);
});

it('sends the X-API-KEY header on every request', function () {
    Http::fake([
        '*' => Http::response(['data' => [], 'metadata' => ['rowcount' => 0, 'issuccess' => true], 'error' => null]),
    ]);

    $service = app(LogisticsService::class);
    $service->tablesList();

    Http::assertSent(function ($request) {
        return $request->hasHeader('X-API-KEY', 'test-api-key');
    });
});

it('builds the correct URL for tables_list', function () {
    Http::fake([
        '*' => Http::response(['data' => ['art', 'cust'], 'metadata' => ['rowcount' => 2, 'issuccess' => true], 'error' => null]),
    ]);

    $service = app(LogisticsService::class);
    $result = $service->tablesList();

    Http::assertSent(function ($request) {
        return str_contains($request->url(), 'http://test-server.local/testfolder/tables_list');
    });

    expect($result['data'])->toBe(['art', 'cust']);
});

it('builds the correct URL for column_list with table name', function () {
    Http::fake([
        '*' => Http::response(['data' => [['name' => 'artid']], 'metadata' => ['rowcount' => 1, 'issuccess' => true], 'error' => null]),
    ]);

    $service = app(LogisticsService::class);
    $service->columnList('art');

    Http::assertSent(function ($request) {
        return str_contains($request->url(), 'testfolder/column_list/art');
    });
});

it('sends correct parameters for art_list', function () {
    Http::fake([
        '*' => Http::response(['data' => [], 'metadata' => ['rowcount' => 0, 'issuccess' => true], 'error' => null]),
    ]);

    $service = app(LogisticsService::class);
    $service->artList(['select' => 'artid,name1', 'search' => 'test']);

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

        return $body['select'] === 'artid,name1' && $body['search'] === 'test';
    });
});

it('sends ARTID for art_getstk', function () {
    Http::fake([
        '*' => Http::response(['data' => ['stock' => 42], 'metadata' => ['rowcount' => 1, 'issuccess' => true], 'error' => null]),
    ]);

    $service = app(LogisticsService::class);
    $result = $service->artGetStock('ART001');

    Http::assertSent(function ($request) {
        return $request->data()['ARTID'] === 'ART001';
    });

    expect($result['data']['stock'])->toBe(42);
});

it('sends correct parameters for document_detail', function () {
    Http::fake([
        '*' => Http::response(['data' => ['jnl' => 'VEN', 'number' => '1'], 'metadata' => ['rowcount' => 1, 'issuccess' => true], 'error' => null]),
    ]);

    $service = app(LogisticsService::class);
    $service->documentDetail('VEN', '1');

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

        return $body['jnl'] === 'VEN' && $body['number'] === '1';
    });
});

it('sends correct parameters for third_list', function () {
    Http::fake([
        '*' => Http::response(['data' => [], 'metadata' => ['rowcount' => 0, 'issuccess' => true], 'error' => null]),
    ]);

    $service = app(LogisticsService::class);
    $service->thirdList(['select' => 'custid', 'search' => 'test']);

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

        return $body['select'] === 'custid' && $body['search'] === 'test';
    });
});

it('returns fallback data when API returns empty response', function () {
    Http::fake([
        '*' => Http::response(null),
    ]);

    $service = app(LogisticsService::class);
    $result = $service->tablesList();

    expect($result)->toHaveKey('error')
        ->and($result['metadata']['issuccess'])->toBeFalse();
});

it('throws LogisticsApiException on connection timeout', function () {
    Http::fake(fn () => throw new ConnectionException('Connection timed out'));

    $service = app(LogisticsService::class);
    $service->tablesList();
})->throws(LogisticsApiException::class, "L'API Logistics est injoignable");

it('throws LogisticsApiException on general request failure', function () {
    Http::fake(fn () => throw new \RuntimeException('Server error'));

    $service = app(LogisticsService::class);
    $service->tablesList();
})->throws(LogisticsApiException::class, "La requete vers l'API Logistics a echoue");

it('logs failed requests to api_request_logs as valid JSON', function () {
    Http::fake(fn () => throw new ConnectionException('Connection timed out'));

    $service = app(LogisticsService::class);

    try {
        $service->tablesList();
    } catch (LogisticsApiException) {
        // expected
    }

    $this->assertDatabaseHas('api_request_logs', [
        'endpoint' => 'tables_list',
        'response_status' => 0,
    ]);

    $log = DB::table('api_request_logs')->where('endpoint', 'tables_list')->first();
    $decoded = json_decode($log->response_data, true);

    expect($decoded)->toBeArray()
        ->and($decoded)->toHaveKey('error');
});

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

    $service = app(LogisticsService::class);
    $service->documentGetPdf('VEN', '2026/0001', 'DEFAULT');

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

        return str_contains($request->url(), 'Document_GetPDF')
            && $body['JNL'] === 'VEN'
            && $body['NUMBER'] === '2026/0001'
            && $body['LAYOUT'] === 'DEFAULT';
    });
});

it('sends correct parameters for custom_geninv_updatestock', function () {
    Http::fake([
        '*' => Http::response(['data' => null, 'metadata' => ['rowcount' => 0, 'issuccess' => false], 'error' => 'Unknown STKID']),
    ]);

    $service = app(LogisticsService::class);
    $params = ['ARTID' => 'ART001', 'STKID' => 'STK1', 'QTY' => '10', 'TOCHECK' => '5', 'TOCHECKDETAIL' => 'test', 'MODE' => '1'];
    $service->customGeninvUpdatestock($params);

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

        return str_contains($request->url(), 'custom_geninv_updatestock')
            && $body['ARTID'] === 'ART001'
            && $body['STKID'] === 'STK1'
            && $body['QTY'] === '10';
    });
});

it('sends correct parameters for document_add', function () {
    Http::fake([
        '*' => Http::response(['data' => ['number' => '2026/0001'], 'metadata' => ['rowcount' => 1, 'issuccess' => true], 'error' => null]),
    ]);

    $service = app(LogisticsService::class);
    $params = [
        'ThirdId' => 'CUST001',
        'Date' => '2026-02-20',
        'Artid' => ['ART001', 'ART002'],
        'Qty' => ['2', '5'],
        'Saleprice' => ['10.00', '25.50'],
        'JNL' => 'VEN',
    ];
    $service->documentAdd($params);

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

        return str_contains($request->url(), 'document_add')
            && $body['ThirdId'] === 'CUST001'
            && $body['JNL'] === 'VEN'
            && $body['Artid'] === ['ART001', 'ART002'];
    });
});

it('sends correct parameters for document_mod', function () {
    Http::fake([
        '*' => Http::response(['data' => ['updated' => true], 'metadata' => ['rowcount' => 1, 'issuccess' => true], 'error' => null]),
    ]);

    $service = app(LogisticsService::class);
    $params = [
        'number' => '2026/0001',
        'JNL' => 'VEN',
        'Thirdid' => 'CUST002',
    ];
    $service->documentMod($params);

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

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

it('sends correct parameters for art_add', function () {
    Http::fake([
        '*' => Http::response(['data' => ['artid' => 'ART001'], 'metadata' => ['rowcount' => 1, 'issuccess' => true], 'error' => null]),
    ]);

    $service = app(LogisticsService::class);
    $params = ['ARTID' => 'ART001', 'NAME1' => 'Test Article', 'SALEPRICE' => '49.99'];
    $service->artAdd($params);

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

        return str_contains($request->url(), 'art_add')
            && $body['ARTID'] === 'ART001'
            && $body['NAME1'] === 'Test Article'
            && $body['SALEPRICE'] === '49.99';
    });
});

it('sends correct parameters for art_mod', function () {
    Http::fake([
        '*' => Http::response(['data' => ['artid' => 'ART001'], 'metadata' => ['rowcount' => 1, 'issuccess' => true], 'error' => null]),
    ]);

    $service = app(LogisticsService::class);
    $params = ['ARTID' => 'ART001', 'NAME1' => 'Updated Article'];
    $service->artMod($params);

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

        return str_contains($request->url(), 'art_mod')
            && $body['ARTID'] === 'ART001'
            && $body['NAME1'] === 'Updated Article';
    });
});

it('sends correct parameters for third_add', function () {
    Http::fake([
        '*' => Http::response(['data' => ['thirdid' => '_@00036051'], 'metadata' => ['rowcount' => 1, 'issuccess' => true], 'error' => null]),
    ]);

    $service = app(LogisticsService::class);
    $params = ['NAME' => 'Test Company', 'VAT' => 'BE0123456789', 'EMAIL' => 'test@example.com'];
    $service->thirdAdd($params);

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

        return str_contains($request->url(), 'third_add')
            && $body['NAME'] === 'Test Company'
            && $body['VAT'] === 'BE0123456789';
    });
});

it('sends correct parameters for third_mod', function () {
    Http::fake([
        '*' => Http::response(['data' => ['thirdid' => 'CUST001'], 'metadata' => ['rowcount' => 1, 'issuccess' => true], 'error' => null]),
    ]);

    $service = app(LogisticsService::class);
    $params = ['CUSTID' => 'CUST001', 'NAME' => 'Updated Company'];
    $service->thirdMod($params);

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

        return str_contains($request->url(), 'third_mod')
            && $body['CUSTID'] === 'CUST001'
            && $body['NAME'] === 'Updated Company';
    });
});

it('includes endpoint info in LogisticsApiException', function () {
    Http::fake(fn () => throw new ConnectionException('Connection timed out'));

    $service = app(LogisticsService::class);

    try {
        $service->artList(['search' => 'test']);
    } catch (LogisticsApiException $e) {
        expect($e->endpoint)->toBe('art_list')
            ->and($e->params)->toBe(['search' => 'test']);

        return;
    }

    $this->fail('Expected LogisticsApiException was not thrown');
});
