import SwiftUI

extension WidgetVariant {
    /// Design-time preview dimensions (used for layout in the Widgets library and on cards).
    var previewCardSize: CGSize {
        if layoutMode == .interactiveMap {
            switch size {
            case .small:
                return CGSize(width: 280, height: 320)
            case .medium:
                return CGSize(width: 400, height: 320)
            case .large:
                return CGSize(width: 440, height: 420)
            }
        }
        if let override = layoutMode.widgetPreviewCardSize() {
            return override
        }
        switch size {
        case .small:
            return CGSize(width: 190, height: 190)
        case .medium:
            return CGSize(width: 340, height: 190)
        case .large:
            return CGSize(width: 340, height: 360)
        }
    }
}

struct WidgetPreviewCard: View {
    let app: LauncherApp
    let variant: WidgetVariant
    /// Uniform scale so multiple previews fit the library panel without widening the window.
    var layoutScale: CGFloat = 1

    private var previewSize: CGSize { variant.previewCardSize }

    var body: some View {
        let s = max(0.01, layoutScale)
        DesktopWidgetView(app: app, variant: variant, isPreview: true)
            .frame(width: previewSize.width, height: previewSize.height)
            .scaleEffect(s)
            .frame(width: previewSize.width * s, height: previewSize.height * s)
            .clipShape(RoundedRectangle(cornerRadius: 18, style: .continuous))
            .overlay(
                RoundedRectangle(cornerRadius: 18, style: .continuous)
                    .stroke(Color.white.opacity(0.10), lineWidth: 1)
            )
    }
}

#if DEBUG
#Preview {
    ZStack {
        Color.black
        WidgetPreviewCard(
            app: LauncherApp.sampleApps[1],
            variant: WidgetTemplates.variants(for: LauncherApp.sampleApps[1])[0]
        )
            .frame(width: 520, height: 360)
            .padding()
    }
}
#endif

