import SwiftUI

struct LicenseManagementView: View {
    @StateObject private var licenseViewModel = LicenseViewModel()
    let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "Onbekend"

    var body: some View {
        ScrollView {
            VStack(spacing: 32) {
                heroSection
                statusSection
                maintenanceSection
            }
            .padding(32)
            .frame(maxWidth: 760)
            .frame(maxWidth: .infinity)
        }
        .background(Color(NSColor.controlBackgroundColor))
    }

    private var heroSection: some View {
        VStack(spacing: 18) {
            AppIconView()

            HStack(alignment: .lastTextBaseline, spacing: 8) {
                Text("Proat")
                    .font(.system(size: 32, weight: .bold))

                Text("v\(appVersion)")
                    .font(.subheadline)
                    .foregroundStyle(.secondary)
                    .padding(.bottom, 4)
            }

            Text("Persoonlijke macOS-dicteerapp zonder licentie- of verkooplaag.")
                .font(.title3)
                .foregroundStyle(.secondary)
                .multilineTextAlignment(.center)
        }
        .padding(.top, 28)
    }

    private var statusSection: some View {
        VStack(alignment: .leading, spacing: 18) {
            HStack(spacing: 12) {
                Image(systemName: "checkmark.circle.fill")
                    .font(.system(size: 24))
                    .foregroundStyle(.green)

                VStack(alignment: .leading, spacing: 4) {
                    Text("Vrij beschikbaar")
                        .font(.headline)
                    Text("Alle lokale functies zijn beschikbaar zonder proefperiode, licentiesleutel of externe aankoopportal.")
                        .font(.subheadline)
                        .foregroundStyle(.secondary)
                }

                Spacer()
            }

            if let message = licenseViewModel.validationMessage {
                Divider()
                Text(message)
                    .font(.callout)
                    .foregroundStyle(licenseViewModel.validationSuccess ? .green : .secondary)
            }
        }
        .padding(24)
        .background(CardBackground(isSelected: false))
        .shadow(color: .black.opacity(0.05), radius: 10)
    }

    private var maintenanceSection: some View {
        VStack(alignment: .leading, spacing: 16) {
            Text("Onderhoud")
                .font(.headline)

            Text("Heb je nog oude licentiegegevens in je keychain staan, dan kun je die lokaal opruimen.")
                .font(.subheadline)
                .foregroundStyle(.secondary)

            Button(role: .destructive, action: {
                licenseViewModel.removeLicense()
            }) {
                Label("Oude licentiegegevens verwijderen", systemImage: "trash")
                    .frame(maxWidth: .infinity)
                    .padding(.vertical, 8)
            }
            .buttonStyle(.bordered)
        }
        .padding(24)
        .background(CardBackground(isSelected: false))
        .shadow(color: .black.opacity(0.05), radius: 10)
    }
}
