import SwiftUI
import AppKit

// MARK: - Custom Model Card View
struct CustomModelCardView: View {
    let model: CustomCloudModel
    let isCurrent: Bool
    var setDefaultAction: () -> Void
    var deleteAction: () -> Void
    var editAction: (CustomCloudModel) -> Void
    
    var body: some View {
        VStack(alignment: .leading, spacing: 0) {
            // Main card content
            HStack(alignment: .top, spacing: ProatSpacing.m) {
                VStack(alignment: .leading, spacing: ProatSpacing.xs - 2) {
                    headerSection
                    metadataSection
                    descriptionSection
                }
                .frame(maxWidth: .infinity, alignment: .leading)

                actionSection
            }
            .padding(ProatSpacing.m)
        }
        .background(ProatModelCardBackground(isSelected: isCurrent))
    }

    private var headerSection: some View {
        HStack(alignment: .firstTextBaseline) {
            Text(model.displayName)
                .font(ProatFont.sectie)
                .foregroundColor(.proatText1)

            Spacer()
        }
    }

    private var metadataSection: some View {
        HStack(spacing: ProatSpacing.s) {
            // Provider
            Label("Eigen provider", systemImage: "cloud")
                .font(ProatFont.klein)
                .foregroundColor(.proatText2)
                .lineLimit(1)

            // Language
            Label(model.language, systemImage: "globe")
                .font(ProatFont.klein)
                .foregroundColor(.proatText2)
                .lineLimit(1)

            // OpenAI Compatible
            Label("OpenAI-compatibel", systemImage: "checkmark.seal")
                .font(ProatFont.klein)
                .foregroundColor(.proatText2)
                .lineLimit(1)
        }
        .lineLimit(1)
    }

    private var descriptionSection: some View {
        Text(model.description)
            .font(ProatFont.klein)
            .foregroundColor(.proatText3)
            .lineLimit(2)
            .fixedSize(horizontal: false, vertical: true)
            .padding(.top, ProatSpacing.xs / 2)
    }

    private var actionSection: some View {
        HStack(spacing: ProatSpacing.xs) {
            if isCurrent {
                Text("Standaardmodel")
                    .font(.system(size: 12, weight: .medium))
                    .foregroundColor(.proatTeal)
            } else {
                Button(action: setDefaultAction) {
                    Text("Als standaard instellen")
                        .font(.system(size: 12))
                }
                .buttonStyle(.bordered)
                .controlSize(.small)
            }

            Menu {
                Button {
                    editAction(model)
                } label: {
                    Label("Model bewerken", systemImage: "pencil")
                }

                Button(role: .destructive) {
                    deleteAction()
                } label: {
                    Label("Model verwijderen", systemImage: "trash")
                }
            } label: {
                Image(systemName: "ellipsis.circle")
                    .font(.system(size: 14))
                    .foregroundColor(.proatText2)
            }
            .menuStyle(.borderlessButton)
            .menuIndicator(.hidden)
            .frame(width: 20, height: 20)
        }
    }
}
