import SwiftUI

struct CustomHeader: View {
    @State private var selectedModelVersion = "v4.5"
    let onModelChange: ((String) -> Void)?
    let title: String
    
    init(title: String = "Custom", onModelChange: ((String) -> Void)? = nil) {
        self.title = title
        self.onModelChange = onModelChange
    }
    
    var body: some View {
        HStack {
            // Left Section - Custom Title and Credits
            leftSection
            
            Spacer()
            
            // Right Section - Model Version Button
            rightSection
        }
        .padding(.leading, 24)
        .padding(.trailing, 16)
        .padding(.top, 24)
    }
    
    private var leftSection: some View {
        // Title and Credits (no back button)
        VStack(alignment: .leading, spacing: 2) {
            // Custom Title
            Text(title)
                .font(Constants.Typography.mediumRegular)
                .foregroundColor(.white)
                .tracking(0.36)
            
            // Credits Display
            CreditsDisplay()
        }
    }
    
    private var rightSection: some View {
        modelVersionButton
    }
    
    private var modelVersionButton: some View {
        Menu {
            Button {
                selectedModelVersion = "v4.5"
                onModelChange?("v4.5")
            } label: {
                HStack {
                    Text("v4.5")
                        .foregroundColor(Constants.Colors.Foreground.primary)
                    Spacer()
                    if selectedModelVersion == "v4.5" {
                        Image(systemName: "checkmark")
                            .foregroundColor(Constants.Colors.Accent.brand)
                            .font(.system(size: 14, weight: .medium))
                    }
                }
            }
            
            Button {
                selectedModelVersion = "v4.0"
                onModelChange?("v4.0")
            } label: {
                HStack {
                    Text("v4.0")
                        .foregroundColor(Constants.Colors.Foreground.primary)
                    Spacer()
                    if selectedModelVersion == "v4.0" {
                        Image(systemName: "checkmark")
                            .foregroundColor(Constants.Colors.Accent.brand)
                            .font(.system(size: 14, weight: .medium))
                    }
                }
            }
            
            Button {
                selectedModelVersion = "v3.5"
                onModelChange?("v3.5")
            } label: {
                HStack {
                    Text("v3.5")
                        .foregroundColor(Constants.Colors.Foreground.primary)
                    Spacer()
                    if selectedModelVersion == "v3.5" {
                        Image(systemName: "checkmark")
                            .foregroundColor(Constants.Colors.Accent.brand)
                            .font(.system(size: 14, weight: .medium))
                    }
                }
            }
        } label: {
            HStack(spacing: 4) {
                Text(selectedModelVersion)
                    .font(Constants.Typography.xSmallTitle)
                    .foregroundColor(Constants.Colors.Foreground.primary)
                    .tracking(0.24)
                
                Image("Icon/triangle-down")
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .frame(width: 16, height: 16)
                    .foregroundColor(Constants.Colors.Foreground.primary)
            }
            .frame(height: 40)
            .padding(.horizontal, 16)
            .background(
                RoundedRectangle(cornerRadius: 100)
                    .stroke(Color.white.opacity(0.1), lineWidth: 1)
            )
        }
        .buttonStyle(PlainButtonStyle())
    }
}

#Preview {
    CustomHeader { modelVersion in
        print("Model changed to: \(modelVersion)")
    }
    .background(Constants.Colors.Background.primary)
}
