//
//  BettingZoneView.swift
//  Blackjack
//
//  The betting area where players place their bets.
//

import SwiftUI
import CasinoKit

struct BettingZoneView: View {
    let betAmount: Int
    let minBet: Int
    let maxBet: Int
    let onTap: () -> Void
    
    @ScaledMetric(relativeTo: .headline) private var labelFontSize: CGFloat = Design.Size.handLabelFontSize
    
    private var isAtMax: Bool {
        betAmount >= maxBet
    }
    
    var body: some View {
        Button(action: onTap) {
            ZStack {
                // Background
                RoundedRectangle(cornerRadius: Design.CornerRadius.large)
                    .fill(Color.BettingZone.main)
                    .overlay(
                        RoundedRectangle(cornerRadius: Design.CornerRadius.large)
                            .strokeBorder(Color.BettingZone.mainBorder, lineWidth: Design.LineWidth.medium)
                    )
                
                // Content
                if betAmount > 0 {
                    // Show chip with amount (scaled)
                    ChipOnTableView(amount: betAmount, showMax: isAtMax, size: Design.Size.bettingChipSize)
                } else {
                    // Empty state
                    VStack(spacing: Design.Spacing.small) {
                        Text(String(localized: "TAP TO BET"))
                            .font(.system(size: labelFontSize, weight: .bold))
                            .foregroundStyle(.white.opacity(Design.Opacity.medium))
                        
                        HStack(spacing: Design.Spacing.medium) {
                            Text(String(localized: "Min: $\(minBet)"))
                                .font(.system(size: Design.Size.handNumberFontSize, weight: .medium))
                                .foregroundStyle(.white.opacity(Design.Opacity.light))
                            
                            Text(String(localized: "Max: $\(maxBet.formatted())"))
                                .font(.system(size: Design.Size.handNumberFontSize, weight: .medium))
                                .foregroundStyle(.white.opacity(Design.Opacity.light))
                        }
                    }
                }
            }
            .frame(maxWidth: .infinity)
            .frame(height: Design.Size.bettingZoneHeightScaled)
        }
        .buttonStyle(.plain)
        .accessibilityLabel(betAmount > 0 ? "$\(betAmount) bet" + (isAtMax ? ", maximum" : "") : "Place bet")
        .accessibilityHint("Double tap to add chips")
    }
}

// MARK: - Previews

#Preview("Empty") {
    ZStack {
        Color.Table.felt.ignoresSafeArea()
        BettingZoneView(
            betAmount: 0,
            minBet: 10,
            maxBet: 1000,
            onTap: {}
        )
        .padding()
    }
}

#Preview("With Bet") {
    ZStack {
        Color.Table.felt.ignoresSafeArea()
        BettingZoneView(
            betAmount: 250,
            minBet: 10,
            maxBet: 1000,
            onTap: {}
        )
        .padding()
    }
}

#Preview("Max Bet") {
    ZStack {
        Color.Table.felt.ignoresSafeArea()
        BettingZoneView(
            betAmount: 1000,
            minBet: 10,
            maxBet: 1000,
            onTap: {}
        )
        .padding()
    }
}

