<?php

declare(strict_types=1);

use App\Models\Backend\Transaction;
use App\Models\Backend\TransactionOutput;

test('can connect to backend schema', function () {
    $count = Transaction::count();

    expect($count)->toBeGreaterThanOrEqual(0);
});

test('can read transactions from backend schema', function () {
    $transaction = Transaction::first();

    if ($transaction) {
        expect($transaction)->toBeInstanceOf(Transaction::class);
        expect($transaction->corporate_entity)->toBeString();
        expect($transaction->corporate_counterparty)->toBeString();
        expect($transaction->tx_amount)->not->toBeNull();
    } else {
        expect(true)->toBeTrue(); // No transactions yet
    }
});

test('can read transaction outputs', function () {
    $output = TransactionOutput::first();

    if ($output) {
        expect($output)->toBeInstanceOf(TransactionOutput::class);
        expect($output->transaction_id)->toBeInt();
        expect($output->output_key)->toBeString();
        expect($output->content)->toBeArray();
    } else {
        expect(true)->toBeTrue(); // No outputs yet
    }
});

test('transaction has outputs relationship', function () {
    $transaction = Transaction::with('outputs')->first();

    if ($transaction) {
        expect($transaction->outputs)->toBeInstanceOf(\Illuminate\Database\Eloquent\Collection::class);
    } else {
        expect(true)->toBeTrue(); // No transactions yet
    }
});

test('can get specific output by key', function () {
    $transaction = Transaction::with('outputs')->first();

    if ($transaction && $transaction->outputs->isNotEmpty()) {
        $firstKey = $transaction->outputs->first()->output_key;
        $output = $transaction->getOutput($firstKey);

        expect($output)->toBeArray();
    } else {
        expect(true)->toBeTrue(); // No transactions with outputs yet
    }
});

test('can get company info from transaction', function () {
    $transaction = Transaction::with('outputs')->first();

    if ($transaction) {
        $companyInfo = $transaction->getCompanyInfo();

        // Kann null sein wenn kein company_info output existiert
        expect($companyInfo)->toBeIn([null, 'array']);
    } else {
        expect(true)->toBeTrue();
    }
});

test('can get risk assessment from transaction', function () {
    $transaction = Transaction::with('outputs')->first();

    if ($transaction) {
        $risk = $transaction->getRiskAssessment();

        // Kann null sein wenn kein risk_assessment output existiert
        expect($risk)->toBeIn([null, 'array']);

        if ($risk) {
            expect($risk)->toHaveKey('score');
        }
    } else {
        expect(true)->toBeTrue();
    }
});

test('can check if transaction requires review', function () {
    $transaction = Transaction::with('outputs')->first();

    if ($transaction) {
        $requiresReview = $transaction->requiresReview();

        expect($requiresReview)->toBeBool();
    } else {
        expect(true)->toBeTrue();
    }
});

test('transaction output keys constants exist', function () {
    expect(TransactionOutput::KEY_COMPANY_INFO)->toBe('company_info');
    expect(TransactionOutput::KEY_RISK_ASSESSMENT)->toBe('risk_assessment');
    expect(TransactionOutput::KEY_SANCTIONS)->toBe('sanctions');
    expect(TransactionOutput::KEY_PEP)->toBe('pep');
});

test('can get all available output keys', function () {
    $keys = TransactionOutput::availableKeys();

    expect($keys)->toBeArray();
    expect($keys)->toContain('company_info');
    expect($keys)->toContain('risk_assessment');
});

test('can get label for output key', function () {
    $label = TransactionOutput::getKeyLabel('company_info');

    expect($label)->toBe('Company Information');

    $label = TransactionOutput::getKeyLabel('risk_assessment');

    expect($label)->toBe('Risk Assessment');
});
