/**
 * @file gas-tokenization.ts
 * @notice GAS Network integration for tokenized assets (Sub-Volume A)
 */

import { gasSettlementService } from './gas/gas-settlement.service';
import { GasSettlementRequest, GasSettlementResult } from './gas/gas-settlement.service';
import { TokenRegistry } from '../../../smom-dbis-138/contracts/tokenization/TokenRegistry';

export interface TokenizedAssetGasSettlementRequest {
    tokenAddress: string;
    fabricTokenId: string;
    sourceBankId: string;
    destinationBankId: string;
    amount: string;
    currencyCode: string;
    networkType?: 'classical' | 'cbdc' | 'commodity' | 'security' | 'quantum' | 'multiversal';
}

export class GasTokenizationIntegration {
    private tokenRegistry: TokenRegistry;

    constructor(tokenRegistry: TokenRegistry) {
        this.tokenRegistry = tokenRegistry;
    }

    /**
     * Execute atomic settlement for tokenized asset via GAS network
     */
    async executeTokenizedAssetSettlement(
        request: TokenizedAssetGasSettlementRequest
    ): Promise<GasSettlementResult> {
        // Get token metadata
        const tokenMetadata = await this.tokenRegistry.getToken(request.tokenAddress);

        // Prepare GAS settlement request
        const gasRequest: GasSettlementRequest = {
            sourceBankId: request.sourceBankId,
            destinationBankId: request.destinationBankId,
            amount: parseFloat(request.amount),
            currencyCode: request.currencyCode,
            assetType: 'tokenized',
            networkType: request.networkType || 'classical',
            assetDetails: {
                tokenAddress: request.tokenAddress,
                fabricTokenId: request.fabricTokenId,
                underlyingAsset: tokenMetadata.underlyingAsset,
                backingReserve: tokenMetadata.backingReserve
            },
            optimizeFor: 'cost'
        };

        // Execute GAS settlement
        const result = await gasSettlementService.executeGasSettlement(gasRequest);

        return result;
    }

    /**
     * Verify finality for tokenized asset settlement
     */
    async verifyTokenizedAssetFinality(
        gasSettlementId: string,
        tokenAddress: string
    ): Promise<{ final: boolean; finalityProof?: string }> {
        // Verify GAS settlement finality
        const isFinal = await gasSettlementService.isSettlementAtomicAndVerified(gasSettlementId);

        if (!isFinal) {
            return { final: false };
        }

        // Get finality proof
        const settlement = await gasSettlementService.getGasSettlement(gasSettlementId);
        
        return {
            final: true,
            finalityProof: settlement.finalityProof
        };
    }

    /**
     * Get tokenized asset transfers via GAS
     */
    async getTokenizedAssetTransfers(
        tokenAddress: string,
        bankId?: string
    ): Promise<GasSettlementResult[]> {
        // Get all GAS settlements for tokenized assets
        const settlements = bankId
            ? await gasSettlementService.getGasSettlementsForBank(bankId)
            : [];

        // Filter for tokenized asset settlements
        const tokenizedSettlements = settlements.filter(
            (s: any) => s.assetDetails?.tokenAddress === tokenAddress
        );

        return tokenizedSettlements;
    }
}
