import SwiftUI

// MARK: - Theme

extension Color {
    // Primary palette
    static let closerPrimary = Color(hex: "B98AF4")
    static let closerSecondary = Color(hex: "E7A2D1")
    static let closerBackground = Color(hex: "FFFBFE")
    static let closerSurface = Color(hex: "F5F0FF")
    static let closerOnPrimary = Color.white
    static let closerText = Color(hex: "1C1B1F")
    static let closerTextSecondary = Color(hex: "49454F")
    static let closerDivider = Color(hex: "E6E0E9")
    
    // Semantic
    static let closerSuccess = Color(hex: "4CAF50")
    static let closerWarning = Color(hex: "FF9800")
    static let closerDanger = Color(hex: "F44336")
    static let closerGold = Color(hex: "FFD700")
    
    // Category colors
    static let categoryCommunication = Color(hex: "B98AF4")
    static let categoryIntimacy = Color(hex: "E7A2D1")
    static let categoryFun = Color(hex: "FFB74D")
    static let categoryGoals = Color(hex: "81C784")
    static let categoryAdventure = Color(hex: "64B5F6")
    
    // Streak
    static let streakActive = Color(hex: "FF6B6B")
    static let streakInactive = Color(hex: "E0E0E0")
    
    init(hex: String) {
        let hex = hex.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
        var int: UInt64 = 0
        Scanner(string: hex).scanHexInt64(&int)
        let a, r, g, b: UInt64
        switch hex.count {
        case 6:
            (a, r, g, b) = (255, (int >> 16) & 0xFF, (int >> 8) & 0xFF, int & 0xFF)
        case 8:
            (a, r, g, b) = ((int >> 24) & 0xFF, (int >> 16) & 0xFF, (int >> 8) & 0xFF, int & 0xFF)
        default:
            (a, r, g, b) = (255, 0, 0, 0)
        }
        self.init(
            .sRGB,
            red: Double(r) / 255,
            green: Double(g) / 255,
            blue: Double(b) / 255,
            opacity: Double(a) / 255
        )
    }
}

// MARK: - Typography

enum CloserFont {
    static let largeTitle = Font.system(size: 34, weight: .bold, design: .default)
    static let title1 = Font.system(size: 28, weight: .bold)
    static let title2 = Font.system(size: 22, weight: .semibold)
    static let title3 = Font.system(size: 20, weight: .semibold)
    static let headline = Font.system(size: 17, weight: .semibold)
    static let body = Font.system(size: 17, weight: .regular)
    static let callout = Font.system(size: 16, weight: .regular)
    static let subheadline = Font.system(size: 15, weight: .regular)
    static let footnote = Font.system(size: 13, weight: .regular)
    static let caption = Font.system(size: 12, weight: .regular)
    static let caption2 = Font.system(size: 11, weight: .regular)
}

// MARK: - Spacing

enum CloserSpacing {
    static let xs: CGFloat = 4
    static let sm: CGFloat = 8
    static let md: CGFloat = 12
    static let lg: CGFloat = 16
    static let xl: CGFloat = 24
    static let xxl: CGFloat = 32
    static let xxxl: CGFloat = 48
}

// MARK: - Corner Radius

enum CloserRadius {
    static let small: CGFloat = 8
    static let medium: CGFloat = 12
    static let large: CGFloat = 16
    static let xlarge: CGFloat = 24
    static let full: CGFloat = 999
}

// MARK: - Shadow

extension View {
    func closerShadow(level: ShadowLevel = .medium) -> some View {
        switch level {
        case .small:
            return self.shadow(color: .black.opacity(0.1), radius: 4, x: 0, y: 2)
        case .medium:
            return self.shadow(color: .black.opacity(0.12), radius: 8, x: 0, y: 4)
        case .large:
            return self.shadow(color: .black.opacity(0.15), radius: 16, x: 0, y: 8)
        }
    }
}

enum ShadowLevel {
    case small, medium, large
}

// MARK: - Button Styles

struct PrimaryButtonStyle: ButtonStyle {
    let isDisabled: Bool
    
    init(isDisabled: Bool = false) {
        self.isDisabled = isDisabled
    }
    
    func makeBody(configuration: Configuration) -> some View {
        configuration.label
            .font(CloserFont.headline)
            .foregroundColor(.closerOnPrimary)
            .frame(maxWidth: .infinity)
            .padding(.vertical, CloserSpacing.lg)
            .background(isDisabled ? Color.closerPrimary.opacity(0.4) : Color.closerPrimary)
            .cornerRadius(CloserRadius.large)
            .opacity(configuration.isPressed ? 0.9 : 1.0)
    }
}

struct SecondaryButtonStyle: ButtonStyle {
    func makeBody(configuration: Configuration) -> some View {
        configuration.label
            .font(CloserFont.headline)
            .foregroundColor(.closerPrimary)
            .frame(maxWidth: .infinity)
            .padding(.vertical, CloserSpacing.lg)
            .background(Color.closerSurface)
            .cornerRadius(CloserRadius.large)
            .overlay(
                RoundedRectangle(cornerRadius: CloserRadius.large)
                    .stroke(Color.closerPrimary, lineWidth: 1.5)
            )
            .opacity(configuration.isPressed ? 0.8 : 1.0)
    }
}

// MARK: - View Modifiers

struct CloserCardModifier: ViewModifier {
    func body(content: Content) -> some View {
        content
            .background(Color.closerSurface)
            .cornerRadius(CloserRadius.large)
            .closerShadow(level: .small)
    }
}

extension View {
    func closerCard() -> some View {
        modifier(CloserCardModifier())
    }
    
    func closerPadding() -> some View {
        padding(.horizontal, CloserSpacing.xl)
    }
    
    func closerSectionTitle() -> some View {
        font(CloserFont.title3)
            .foregroundColor(.closerText)
            .padding(.bottom, CloserSpacing.sm)
    }
}
