<?php

namespace App\Models\Backend;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class TransactionOutput extends Model
{
    protected $connection = 'backend';

    protected $table = 'transaction_outputs';

    public $timestamps = false;

    protected $fillable = [
        'transaction_id',
        'prompt_id',
        'output_key',
        'content',
        'run_id',
    ];

    protected $casts = [
        'content' => 'array',
    ];

    /**
     * Transaction zu der dieser Output gehört
     */
    public function transaction(): BelongsTo
    {
        return $this->belongsTo(Transaction::class, 'transaction_id');
    }

    /**
     * Output-Key Konstanten für Type Safety
     */
    public const KEY_COMPANY_INFO = 'company_info';

    public const KEY_RISK_ASSESSMENT = 'risk_assessment';

    public const KEY_SANCTIONS = 'sanctions';

    public const KEY_PEP = 'pep';

    public const KEY_REGISTRY = 'registry';

    public const KEY_GLEIF = 'gleif';

    public const KEY_INSOLVENCY = 'insolvency';

    public const KEY_BUNDESANZEIGER = 'bundesanzeiger';

    public const KEY_RSS = 'rss';

    public const KEY_EU_SANCTIONS = 'eu_sanctions';

    public const KEY_HANDELSREGISTER = 'handelsregister';

    public const KEY_GENESIS = 'genesis';

    public const KEY_GOVDATA = 'govdata';

    /**
     * Alle verfügbaren Output-Keys
     */
    public static function availableKeys(): array
    {
        return [
            self::KEY_COMPANY_INFO,
            self::KEY_RISK_ASSESSMENT,
            self::KEY_SANCTIONS,
            self::KEY_PEP,
            self::KEY_REGISTRY,
            self::KEY_GLEIF,
            self::KEY_INSOLVENCY,
            self::KEY_BUNDESANZEIGER,
            self::KEY_RSS,
            self::KEY_EU_SANCTIONS,
            self::KEY_HANDELSREGISTER,
            self::KEY_GENESIS,
            self::KEY_GOVDATA,
        ];
    }

    /**
     * Get human-readable label for output key
     */
    public static function getKeyLabel(string $key): string
    {
        return match ($key) {
            self::KEY_COMPANY_INFO => 'Company Information',
            self::KEY_RISK_ASSESSMENT => 'Risk Assessment',
            self::KEY_SANCTIONS => 'Sanctions Check',
            self::KEY_PEP => 'PEP Check',
            self::KEY_REGISTRY => 'Registry Data',
            self::KEY_GLEIF => 'GLEIF Data',
            self::KEY_INSOLVENCY => 'Insolvency Check',
            self::KEY_BUNDESANZEIGER => 'Bundesanzeiger',
            self::KEY_RSS => 'RSS Alerts',
            self::KEY_EU_SANCTIONS => 'EU Sanctions',
            self::KEY_HANDELSREGISTER => 'Handelsregister',
            self::KEY_GENESIS => 'Genesis Data',
            self::KEY_GOVDATA => 'GovData',
            default => ucfirst(str_replace('_', ' ', $key)),
        };
    }
}
