import SwiftUI

struct CustomAlert: View {
    let title: String
    let message: String?
    let primaryButtonTitle: String
    let secondaryButtonTitle: String?
    let onPrimaryAction: () -> Void
    let onSecondaryAction: (() -> Void)?
    let onDismiss: () -> Void
    
    init(
        title: String,
        message: String? = nil,
        primaryButtonTitle: String = "OK",
        secondaryButtonTitle: String? = nil,
        onPrimaryAction: @escaping () -> Void = {},
        onSecondaryAction: (() -> Void)? = nil,
        onDismiss: @escaping () -> Void = {}
    ) {
        self.title = title
        self.message = message
        self.primaryButtonTitle = primaryButtonTitle
        self.secondaryButtonTitle = secondaryButtonTitle
        self.onPrimaryAction = onPrimaryAction
        self.onSecondaryAction = onSecondaryAction
        self.onDismiss = onDismiss
    }
    
    var body: some View {
        ZStack {
            // Background overlay
            Constants.Colors.Background.darkOverlay
                .ignoresSafeArea()
                .onTapGesture {
                    onDismiss()
                }
            
            // Alert container
            VStack(spacing: 32) {
                // Title and message section
                VStack(spacing: 8) {
                    Text(title)
                        .font(Constants.Typography.largeTitle)
                        .foregroundColor(Constants.Colors.Foreground.primary)
                        .tracking(0.36)
                        .multilineTextAlignment(.center)
                        .frame(maxWidth: .infinity)
                    
                    if let message = message {
                        Text(message)
                            .font(Constants.Typography.mediumRegular)
                            .foregroundColor(Constants.Colors.Foreground.secondary)
                            .tracking(0.32)
                            .multilineTextAlignment(.center)
                            .frame(maxWidth: .infinity)
                    }
                }
                
                // Buttons section
                if let secondaryTitle = secondaryButtonTitle {
                    HStack(spacing: 8) {
                        LargeButton.secondary(secondaryTitle, action: {
                            onSecondaryAction?()
                            onDismiss()
                        })
                        LargeButton(title: primaryButtonTitle, variant: .primary, action: {
                            onPrimaryAction()
                            onDismiss()
                        })
                    }
                } else {
                    LargeButton(title: primaryButtonTitle, variant: .primary, action: {
                        onPrimaryAction()
                        onDismiss()
                    })
                }
            }
            .padding(32)
            .background(Constants.Colors.Background.secondary)
            .clipShape(RoundedRectangle(cornerRadius: 24))
            .frame(maxWidth: .infinity)
            .padding(.horizontal, 32)
        }
    }
}

// MARK: - Alert Modifier
extension View {
    func customAlert(
        isPresented: Binding<Bool>,
        title: String,
        message: String? = nil,
        primaryButtonTitle: String = "OK",
        secondaryButtonTitle: String? = nil,
        onPrimaryAction: @escaping () -> Void = {},
        onSecondaryAction: (() -> Void)? = nil
    ) -> some View {
        self.overlay(
            Group {
                if isPresented.wrappedValue {
                    CustomAlert(
                        title: title,
                        message: message,
                        primaryButtonTitle: primaryButtonTitle,
                        secondaryButtonTitle: secondaryButtonTitle,
                        onPrimaryAction: onPrimaryAction,
                        onSecondaryAction: onSecondaryAction,
                        onDismiss: {
                            withAnimation(.easeInOut(duration: 0.2)) {
                                isPresented.wrappedValue = false
                            }
                        }
                    )
                    .transition(.opacity.combined(with: .scale(scale: 0.95)))
                    .animation(.easeInOut(duration: 0.2), value: isPresented.wrappedValue)
                }
            }
        )
    }
}

// MARK: - Preview
#Preview {
    VStack(spacing: 20) {
        Text("Background Content")
            .font(Constants.Typography.largeTitle)
            .foregroundColor(Constants.Colors.Foreground.primary)
    }
    .frame(maxWidth: .infinity, maxHeight: .infinity)
    .background(Constants.Colors.Background.primary)
    .overlay(
        CustomAlert(
            title: "Title",
            message: "Some body text goes here",
            primaryButtonTitle: "Confirm",
            secondaryButtonTitle: "Cancel",
            onPrimaryAction: {
                print("Delete confirmed")
            },
            onSecondaryAction: {
                print("Delete cancelled")
            },
            onDismiss: {
                print("Alert dismissed")
            }
        )
    )
}
