//
//  SessionViews.swift
//  CasinoKit
//
//  Reusable UI components for session management.
//  Works with any game that uses the session system.
//

import SwiftUI

// MARK: - End Session Button

/// A button that triggers ending the current session.
public struct EndSessionButton: View {
    let action: () -> Void
    
    public init(action: @escaping () -> Void) {
        self.action = action
    }
    
    public var body: some View {
        Button {
            action()
        } label: {
            HStack(spacing: CasinoDesign.Spacing.small) {
                Image(systemName: "flag.checkered")
                Text(String(localized: "End Session", bundle: .module))
            }
            .font(.system(size: CasinoDesign.BaseFontSize.body, weight: .semibold))
            .foregroundStyle(.white)
            .padding(.horizontal, CasinoDesign.Spacing.large)
            .padding(.vertical, CasinoDesign.Spacing.medium)
            .background(
                Capsule()
                    .fill(Color.orange.opacity(CasinoDesign.Opacity.strong))
            )
        }
    }
}

// MARK: - End Session Confirmation

/// Confirmation dialog for ending a session.
public struct EndSessionConfirmation: View {
    let sessionDuration: TimeInterval
    let netResult: Int
    let onConfirm: () -> Void
    let onCancel: () -> Void
    
    public init(
        sessionDuration: TimeInterval,
        netResult: Int,
        onConfirm: @escaping () -> Void,
        onCancel: @escaping () -> Void
    ) {
        self.sessionDuration = sessionDuration
        self.netResult = netResult
        self.onConfirm = onConfirm
        self.onCancel = onCancel
    }
    
    public var body: some View {
        VStack(spacing: CasinoDesign.Spacing.large) {
            // Header
            VStack(spacing: CasinoDesign.Spacing.small) {
                Image(systemName: "flag.checkered.circle.fill")
                    .font(.system(size: CasinoDesign.BaseFontSize.xxLarge * 2))
                    .foregroundStyle(.orange)
                
                Text(String(localized: "End Session?", bundle: .module))
                    .font(.system(size: CasinoDesign.BaseFontSize.title, weight: .bold))
                    .foregroundStyle(.white)
            }
            
            // Session summary
            VStack(spacing: CasinoDesign.Spacing.medium) {
                HStack {
                    Text(String(localized: "Duration", bundle: .module))
                        .foregroundStyle(.white.opacity(CasinoDesign.Opacity.medium))
                    Spacer()
                    Text(SessionFormatter.formatDuration(sessionDuration))
                        .foregroundStyle(.white)
                        .bold()
                }
                
                HStack {
                    Text(String(localized: "Net Result", bundle: .module))
                        .foregroundStyle(.white.opacity(CasinoDesign.Opacity.medium))
                    Spacer()
                    Text(SessionFormatter.formatMoney(netResult))
                        .foregroundStyle(netResult >= 0 ? .green : .red)
                        .bold()
                }
            }
            .font(.system(size: CasinoDesign.BaseFontSize.body))
            .padding()
            .background(
                RoundedRectangle(cornerRadius: CasinoDesign.CornerRadius.medium)
                    .fill(Color.black.opacity(CasinoDesign.Opacity.light))
            )
            
            // Info text
            Text(String(localized: "Your session will be saved to history and a new session will start.", bundle: .module))
                .font(.system(size: CasinoDesign.BaseFontSize.small))
                .foregroundStyle(.white.opacity(CasinoDesign.Opacity.medium))
                .multilineTextAlignment(.center)
            
            // Buttons
            HStack(spacing: CasinoDesign.Spacing.medium) {
                Button {
                    onCancel()
                } label: {
                    Text(String(localized: "Cancel", bundle: .module))
                        .font(.system(size: CasinoDesign.BaseFontSize.body, weight: .medium))
                        .foregroundStyle(.white)
                        .frame(maxWidth: .infinity)
                        .padding(.vertical, CasinoDesign.Spacing.medium)
                        .background(
                            RoundedRectangle(cornerRadius: CasinoDesign.CornerRadius.medium)
                                .fill(Color.gray.opacity(CasinoDesign.Opacity.medium))
                        )
                }
                
                Button {
                    onConfirm()
                } label: {
                    Text(String(localized: "End Session", bundle: .module))
                        .font(.system(size: CasinoDesign.BaseFontSize.body, weight: .bold))
                        .foregroundStyle(.white)
                        .frame(maxWidth: .infinity)
                        .padding(.vertical, CasinoDesign.Spacing.medium)
                        .background(
                            RoundedRectangle(cornerRadius: CasinoDesign.CornerRadius.medium)
                                .fill(Color.orange)
                        )
                }
            }
        }
        .padding(CasinoDesign.Spacing.xLarge)
        .background(
            RoundedRectangle(cornerRadius: CasinoDesign.CornerRadius.xLarge)
                .fill(Color.Sheet.background)
        )
        .padding(CasinoDesign.Spacing.xLarge)
    }
}

// MARK: - Session Summary Row

/// A compact row showing session summary - works with any game.
public struct SessionSummaryRow: View {
    let styleDisplayName: String
    let duration: TimeInterval
    let roundsPlayed: Int
    let netResult: Int
    let startTime: Date
    let isActive: Bool
    let endReason: SessionEndReason?
    
    public init(
        styleDisplayName: String,
        duration: TimeInterval,
        roundsPlayed: Int,
        netResult: Int,
        startTime: Date,
        isActive: Bool,
        endReason: SessionEndReason?
    ) {
        self.styleDisplayName = styleDisplayName
        self.duration = duration
        self.roundsPlayed = roundsPlayed
        self.netResult = netResult
        self.startTime = startTime
        self.isActive = isActive
        self.endReason = endReason
    }
    
    private var resultColor: Color {
        if netResult > 0 { return .green }
        if netResult < 0 { return .red }
        return .gray
    }
    
    private var resultIcon: String {
        if netResult > 0 { return "arrow.up.circle.fill" }
        if netResult < 0 { return "arrow.down.circle.fill" }
        return "equal.circle.fill"
    }
    
    public var body: some View {
        HStack(spacing: CasinoDesign.Spacing.medium) {
            // Result indicator
            Image(systemName: resultIcon)
                .font(.system(size: CasinoDesign.BaseFontSize.xLarge))
                .foregroundStyle(resultColor)
            
            // Session info
            VStack(alignment: .leading, spacing: CasinoDesign.Spacing.xxSmall) {
                HStack {
                    Text(styleDisplayName)
                        .font(.system(size: CasinoDesign.BaseFontSize.body, weight: .semibold))
                        .foregroundStyle(.white)
                    
                    if isActive {
                        Text(String(localized: "ACTIVE", bundle: .module))
                            .font(.system(size: CasinoDesign.BaseFontSize.xxSmall, weight: .bold))
                            .foregroundStyle(.green)
                            .padding(.horizontal, CasinoDesign.Spacing.xSmall)
                            .padding(.vertical, CasinoDesign.Spacing.xxSmall)
                            .background(
                                Capsule()
                                    .fill(Color.green.opacity(CasinoDesign.Opacity.hint))
                            )
                    }
                }
                
                HStack(spacing: CasinoDesign.Spacing.medium) {
                    Label(
                        SessionFormatter.formatDuration(duration),
                        systemImage: "clock"
                    )
                    
                    Label(
                        "\(roundsPlayed)",
                        systemImage: "suit.spade.fill"
                    )
                }
                .font(.system(size: CasinoDesign.BaseFontSize.small))
                .foregroundStyle(.white.opacity(CasinoDesign.Opacity.medium))
                
                Text(SessionFormatter.formatSessionDate(startTime))
                    .font(.system(size: CasinoDesign.BaseFontSize.xxSmall))
                    .foregroundStyle(.white.opacity(CasinoDesign.Opacity.light))
            }
            
            Spacer()
            
            // Net result
            VStack(alignment: .trailing, spacing: CasinoDesign.Spacing.xxSmall) {
                Text(SessionFormatter.formatMoney(netResult))
                    .font(.system(size: CasinoDesign.BaseFontSize.large, weight: .bold, design: .rounded))
                    .foregroundStyle(resultColor)
                
                if let reason = endReason {
                    Text(reason == .brokeOut 
                         ? String(localized: "Bust", bundle: .module) 
                         : String(localized: "Ended", bundle: .module))
                        .font(.system(size: CasinoDesign.BaseFontSize.xxSmall))
                        .foregroundStyle(.white.opacity(CasinoDesign.Opacity.light))
                }
            }
        }
        .padding(CasinoDesign.Spacing.medium)
        .background(
            RoundedRectangle(cornerRadius: CasinoDesign.CornerRadius.medium)
                .fill(Color.white.opacity(CasinoDesign.Opacity.subtle))
        )
    }
}

// MARK: - Current Session Header

/// A header showing the current active session with end button.
public struct CurrentSessionHeader: View {
    let duration: TimeInterval
    let roundsPlayed: Int
    let netResult: Int
    let onEndSession: () -> Void
    
    public init(
        duration: TimeInterval,
        roundsPlayed: Int,
        netResult: Int,
        onEndSession: @escaping () -> Void
    ) {
        self.duration = duration
        self.roundsPlayed = roundsPlayed
        self.netResult = netResult
        self.onEndSession = onEndSession
    }
    
    private var resultColor: Color {
        if netResult > 0 { return .green }
        if netResult < 0 { return .red }
        return .white
    }
    
    public var body: some View {
        VStack(spacing: CasinoDesign.Spacing.medium) {
            // Header row
            HStack {
                Label(String(localized: "Current Session", bundle: .module), systemImage: "play.circle.fill")
                    .font(.system(size: CasinoDesign.BaseFontSize.body, weight: .semibold))
                    .foregroundStyle(.green)
                
                Spacer()
                
                EndSessionButton(action: onEndSession)
            }
            
            Divider()
                .background(Color.white.opacity(CasinoDesign.Opacity.hint))
            
            // Stats row
            HStack(spacing: CasinoDesign.Spacing.large) {
                StatColumn(
                    value: SessionFormatter.formatDuration(duration),
                    label: String(localized: "Duration", bundle: .module)
                )
                
                StatColumn(
                    value: "\(roundsPlayed)",
                    label: String(localized: "Hands", bundle: .module)
                )
                
                StatColumn(
                    value: SessionFormatter.formatMoney(netResult),
                    label: String(localized: "Net", bundle: .module),
                    valueColor: resultColor
                )
            }
        }
        .padding(CasinoDesign.Spacing.medium)
        .background(
            RoundedRectangle(cornerRadius: CasinoDesign.CornerRadius.large)
                .fill(Color.green.opacity(CasinoDesign.Opacity.subtle))
                .overlay(
                    RoundedRectangle(cornerRadius: CasinoDesign.CornerRadius.large)
                        .stroke(Color.green.opacity(CasinoDesign.Opacity.light), lineWidth: CasinoDesign.LineWidth.thin)
                )
        )
    }
}

// MARK: - Stat Column
// StatColumn is now provided by StatisticsComponents.swift

// MARK: - Game Stats Display Row

/// A row for displaying a single stat item.
public struct GameStatRow: View {
    let item: StatDisplayItem
    
    public init(item: StatDisplayItem) {
        self.item = item
    }
    
    public var body: some View {
        HStack {
            // Icon
            ZStack {
                Circle()
                    .fill(item.iconColor)
                    .frame(width: CasinoDesign.Spacing.xLarge + CasinoDesign.Spacing.small, 
                           height: CasinoDesign.Spacing.xLarge + CasinoDesign.Spacing.small)
                
                Image(systemName: item.icon)
                    .font(.system(size: CasinoDesign.BaseFontSize.small, weight: .bold))
                    .foregroundStyle(.white)
            }
            
            Text(item.label)
                .font(.system(size: CasinoDesign.BaseFontSize.body))
                .foregroundStyle(.white.opacity(CasinoDesign.Opacity.strong))
            
            Spacer()
            
            Text(item.value)
                .font(.system(size: CasinoDesign.BaseFontSize.body, weight: .semibold, design: .rounded))
                .foregroundStyle(item.valueColor)
        }
    }
}

// MARK: - Preview

#Preview("End Session Confirmation") {
    ZStack {
        Color.black.ignoresSafeArea()
        
        EndSessionConfirmation(
            sessionDuration: 3600 + 45 * 60,
            netResult: -250,
            onConfirm: {},
            onCancel: {}
        )
    }
}

#Preview("Current Session Header") {
    ZStack {
        Color.Sheet.background.ignoresSafeArea()
        
        CurrentSessionHeader(
            duration: 1850,
            roundsPlayed: 12,
            netResult: 350,
            onEndSession: {}
        )
        .padding()
    }
}
