import SwiftUI

// MARK: - Standard Card Modifier

struct StandardCardModifier: ViewModifier {
    var backgroundColor: Color = Color.appBackgroundSecondary
    var cornerRadius: CGFloat = AppRadius.lg
    var padding: CGFloat = AppSpacing.md
    var shadow: AppShadow.Shadow = AppShadow.md

    func body(content: Content) -> some View {
        content
            .padding(padding)
            .background(backgroundColor)
            .cornerRadius(cornerRadius)
            .shadow(
                color: shadow.color,
                radius: shadow.radius,
                x: shadow.x,
                y: shadow.y
            )
    }
}

// MARK: - Compact Card Modifier (smaller padding)

struct CompactCardModifier: ViewModifier {
    var backgroundColor: Color = Color.appBackgroundSecondary
    var cornerRadius: CGFloat = AppRadius.md
    var shadow: AppShadow.Shadow = AppShadow.sm

    func body(content: Content) -> some View {
        content
            .padding(AppSpacing.sm)
            .background(backgroundColor)
            .cornerRadius(cornerRadius)
            .shadow(
                color: shadow.color,
                radius: shadow.radius,
                x: shadow.x,
                y: shadow.y
            )
    }
}

// MARK: - Organic Card Modifier (matches existing design system)

struct OrganicCardModifier: ViewModifier {
    var accentColor: Color = Color.appPrimary
    var showBlob: Bool = true
    var blobVariation: Int = 0
    var padding: CGFloat = OrganicSpacing.cozy
    var shadowIntensity: NaturalShadow.ShadowIntensity = .medium

    func body(content: Content) -> some View {
        content
            .padding(padding)
            .background(
                OrganicCardBackground(
                    accentColor: accentColor,
                    showBlob: showBlob,
                    blobVariation: blobVariation
                )
            )
            .clipShape(RoundedRectangle(cornerRadius: 24, style: .continuous))
            .naturalShadow(shadowIntensity)
    }
}

// MARK: - View Extensions for Cards

extension View {
    /// Applies standard card styling with default values
    func standardCard(
        backgroundColor: Color = Color.appBackgroundSecondary,
        cornerRadius: CGFloat = AppRadius.lg,
        padding: CGFloat = AppSpacing.md,
        shadow: AppShadow.Shadow = AppShadow.md
    ) -> some View {
        modifier(StandardCardModifier(
            backgroundColor: backgroundColor,
            cornerRadius: cornerRadius,
            padding: padding,
            shadow: shadow
        ))
    }

    /// Applies compact card styling with smaller padding
    func compactCard(
        backgroundColor: Color = Color.appBackgroundSecondary,
        cornerRadius: CGFloat = AppRadius.md,
        shadow: AppShadow.Shadow = AppShadow.sm
    ) -> some View {
        modifier(CompactCardModifier(
            backgroundColor: backgroundColor,
            cornerRadius: cornerRadius,
            shadow: shadow
        ))
    }

    /// Applies organic card styling (use for main content cards)
    func organicCardStyle(
        accentColor: Color = Color.appPrimary,
        showBlob: Bool = true,
        blobVariation: Int = 0,
        padding: CGFloat = OrganicSpacing.cozy,
        shadowIntensity: NaturalShadow.ShadowIntensity = .medium
    ) -> some View {
        modifier(OrganicCardModifier(
            accentColor: accentColor,
            showBlob: showBlob,
            blobVariation: blobVariation,
            padding: padding,
            shadowIntensity: shadowIntensity
        ))
    }
}

// MARK: - List Row Card Style

struct ListRowCardStyle: ViewModifier {
    func body(content: Content) -> some View {
        content
            .padding(AppSpacing.md)
            .background(Color.appBackgroundSecondary)
            .cornerRadius(AppRadius.md)
            .shadow(color: Color.black.opacity(0.05), radius: 2, x: 0, y: 1)
    }
}

extension View {
    /// Applies card styling for list rows
    func listRowCard() -> some View {
        modifier(ListRowCardStyle())
    }
}

// MARK: - Organic Card Button Style

struct OrganicCardButtonStyle: ButtonStyle {
    func makeBody(configuration: Configuration) -> some View {
        configuration.label
            .scaleEffect(configuration.isPressed ? 0.98 : 1.0)
            .opacity(configuration.isPressed ? 0.9 : 1.0)
            .animation(.easeOut(duration: 0.15), value: configuration.isPressed)
    }
}

// MARK: - Metadata Pill Styles

struct MetadataPillStyle: ViewModifier {
    var backgroundColor: Color = Color.appBackgroundPrimary
    var foregroundColor: Color = Color.appTextSecondary
    var borderColor: Color = Color.appTextSecondary

    func body(content: Content) -> some View {
        content
            .padding(.horizontal, 12)
            .padding(.vertical, 6)
            .background(
                Capsule()
                    .fill(backgroundColor.opacity(0.6))
                    .overlay(
                        Capsule()
                            .stroke(borderColor.opacity(0.15), lineWidth: 1)
                    )
            )
    }
}

extension View {
    /// Applies metadata pill styling (for tags, badges, status indicators)
    func metadataPill(
        backgroundColor: Color = Color.appBackgroundPrimary,
        foregroundColor: Color = Color.appTextSecondary,
        borderColor: Color = Color.appTextSecondary
    ) -> some View {
        modifier(MetadataPillStyle(
            backgroundColor: backgroundColor,
            foregroundColor: foregroundColor,
            borderColor: borderColor
        ))
    }
}
