//
//  ResultBannerView.swift
//  Baccarat
//
//  Animated result banner showing the winner and itemized bet results.
//  Uses the shared ResultBannerView from CasinoKit.
//

import SwiftUI
import CasinoKit

/// An animated banner showing the round result with bet breakdown.
/// This is a wrapper around CasinoKit's shared ResultBannerView.
struct ResultBannerView: View {
    let result: GameResult
    let totalWinnings: Int
    let betResults: [BetResult]
    var playerHadPair: Bool = false
    var bankerHadPair: Bool = false
    let currentBalance: Int
    let minBet: Int
    let onNewRound: () -> Void
    let onGameOver: () -> Void
    
    // MARK: - Computed Properties
    
    private var winningBets: [BetResult] {
        betResults.filter { $0.isWin }
    }
    
    private var losingBets: [BetResult] {
        betResults.filter { $0.isLoss }
    }
    
    private var pushBets: [BetResult] {
        betResults.filter { $0.isPush }
    }
    
    var body: some View {
        CasinoResultBannerView(
            resultText: result.displayText,
            resultColor: result.color,
            totalWinnings: totalWinnings,
            currentBalance: currentBalance,
            minBet: minBet,
            breakdownContent: {
                // Pair indicators
                if playerHadPair || bankerHadPair {
                    HStack(spacing: Design.Spacing.large) {
                        if playerHadPair {
                            PairBadge(label: "P PAIR", color: .blue)
                        }
                        if bankerHadPair {
                            PairBadge(label: "B PAIR", color: .red)
                        }
                    }
                }
                
                // Bet breakdown
                if !betResults.isEmpty {
                    BetBreakdownView(
                        winningBets: winningBets,
                        losingBets: losingBets,
                        pushBets: pushBets
                    )
                }
            },
            onNewRound: onNewRound,
            onPlayAgain: onGameOver
        )
    }
}

// MARK: - Bet Breakdown View

private struct BetBreakdownView: View {
    let winningBets: [BetResult]
    let losingBets: [BetResult]
    let pushBets: [BetResult]
    
    @ScaledMetric(relativeTo: .body) private var fontSize: CGFloat = Design.BaseFontSize.medium
    
    var body: some View {
        ResultBreakdownCard {
            // Winning bets
            ForEach(winningBets) { bet in
                BetResultRow(bet: bet, fontSize: fontSize)
            }
            
            // Push bets
            ForEach(pushBets) { bet in
                BetResultRow(bet: bet, fontSize: fontSize)
            }
            
            // Losing bets
            ForEach(losingBets) { bet in
                BetResultRow(bet: bet, fontSize: fontSize)
            }
        }
    }
}

// MARK: - Bet Result Row

private struct BetResultRow: View {
    let bet: BetResult
    let fontSize: CGFloat
    
    private var statusColor: Color {
        if bet.isWin { return .green }
        if bet.isLoss { return .red }
        return .yellow  // Push
    }
    
    private var statusIcon: String {
        if bet.isWin { return "checkmark.circle.fill" }
        if bet.isLoss { return "xmark.circle.fill" }
        return "arrow.left.arrow.right.circle.fill"  // Push
    }
    
    private var payoutText: String {
        if bet.isWin { return "+\(bet.payout)" }
        if bet.isLoss { return "\(bet.payout)" }
        return "PUSH"
    }
    
    var body: some View {
        HStack(spacing: Design.Spacing.small) {
            // Status icon
            Image(systemName: statusIcon)
                .font(.system(size: fontSize))
                .foregroundStyle(statusColor)
            
            // Bet name
            Text(bet.displayName)
                .font(.system(size: fontSize, weight: .medium))
                .foregroundStyle(.white.opacity(Design.Opacity.heavy))
            
            Spacer()
            
            // Payout
            Text(payoutText)
                .font(.system(size: fontSize, weight: .bold, design: .rounded))
                .foregroundStyle(statusColor)
        }
    }
}

// MARK: - Pair Badge

private struct PairBadge: View {
    let label: String
    let color: Color
    
    var body: some View {
        Text(label)
            .font(.system(size: Design.BaseFontSize.callout - Design.Spacing.xxSmall, weight: .bold))
            .foregroundStyle(.white)
            .padding(.horizontal, Design.Spacing.small)
            .padding(.vertical, Design.Spacing.xxSmall)
            .background(
                Capsule()
                    .fill(color)
            )
    }
}

// MARK: - Previews

#Preview("Win") {
    ZStack {
        Color.Table.preview
            .ignoresSafeArea()
        
        ResultBannerView(
            result: .playerWins,
            totalWinnings: 1500,
            betResults: [
                BetResult(type: .player, amount: 1000, payout: 1000),
                BetResult(type: .playerPair, amount: 100, payout: 1100),
                BetResult(type: .dragonBonusPlayer, amount: 100, payout: 200),
                BetResult(type: .tie, amount: 500, payout: -500)
            ],
            playerHadPair: true,
            bankerHadPair: false,
            currentBalance: 5000,
            minBet: 10,
            onNewRound: {},
            onGameOver: {}
        )
    }
}

#Preview("Game Over") {
    ZStack {
        Color.Table.preview
            .ignoresSafeArea()
        
        ResultBannerView(
            result: .bankerWins,
            totalWinnings: -1000,
            betResults: [
                BetResult(type: .player, amount: 1000, payout: -1000)
            ],
            playerHadPair: false,
            bankerHadPair: false,
            currentBalance: 0,
            minBet: 10,
            onNewRound: {},
            onGameOver: {}
        )
    }
}
