-- Tabla para transacciones KamiPay PIX
CREATE TABLE IF NOT EXISTS kamipay_transactions (
    id INT AUTO_INCREMENT PRIMARY KEY,
    operation_id VARCHAR(255) NOT NULL UNIQUE,
    character_name VARCHAR(50) NOT NULL,
    amount_jc INT NOT NULL,
    amount_brl DECIMAL(10,2) NOT NULL,
    amount_usdt DECIMAL(10,6) NOT NULL,
    rate DECIMAL(10,6) NOT NULL,
    external_reference VARCHAR(255) NOT NULL,
    emv_code TEXT NOT NULL,
    status ENUM('pending', 'processing', 'completed', 'failed', 'expired') DEFAULT 'pending',
    txid VARCHAR(255) NULL,
    bank_txid VARCHAR(255) NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    expires_at TIMESTAMP NOT NULL,
    processed_at TIMESTAMP NULL,
    
    INDEX idx_operation_id (operation_id),
    INDEX idx_character_name (character_name),
    INDEX idx_status (status),
    INDEX idx_created_at (created_at),
    INDEX idx_expires_at (expires_at)
);

-- Tabla para logs de webhooks KamiPay
CREATE TABLE IF NOT EXISTS kamipay_webhook_logs (
    id INT AUTO_INCREMENT PRIMARY KEY,
    operation_id VARCHAR(255) NOT NULL,
    webhook_data TEXT NOT NULL,
    status VARCHAR(50) NOT NULL,
    processed BOOLEAN DEFAULT FALSE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    
    INDEX idx_operation_id (operation_id),
    INDEX idx_status (status),
    INDEX idx_processed (processed),
    INDEX idx_created_at (created_at)
);

-- Insertar datos iniciales si es necesario
-- Nota: Las credenciales reales de KamiPay deben ser configuradas en configs.php
