import SwiftUI

struct MeterDetailView: View {
    let meterSummary: AppData.MeterSummary

    var body: some View {
        ScrollView {
            VStack(spacing: 18) {
                headerCard
                statusCard
                identifiersCard
            }
            .padding()
        }
        .background(
            LinearGradient(
                colors: [meterSummary.tint.opacity(0.18), Color.clear],
                startPoint: .topLeading,
                endPoint: .bottomTrailing
            )
            .ignoresSafeArea()
        )
        .navigationTitle(meterSummary.displayName)
    }

    private var headerCard: some View {
        VStack(alignment: .leading, spacing: 8) {
            Text(meterSummary.displayName)
                .font(.title2.weight(.semibold))
            Text(meterSummary.modelSummary)
                .font(.subheadline)
                .foregroundColor(.secondary)
            if let advertisedName = meterSummary.advertisedName {
                Text("Advertised as " + advertisedName)
                    .font(.caption2)
                    .foregroundColor(.secondary)
            }
        }
        .frame(maxWidth: .infinity, alignment: .leading)
        .padding(18)
        .meterCard(tint: meterSummary.tint, fillOpacity: 0.22, strokeOpacity: 0.28, cornerRadius: 20)
    }

    private var statusCard: some View {
        VStack(alignment: .leading, spacing: 10) {
            Text("Status")
                .font(.headline)
            HStack(spacing: 8) {
                Circle()
                    .fill(meterSummary.tint)
                    .frame(width: 10, height: 10)
                Text("Offline")
                    .font(.caption.weight(.semibold))
                    .foregroundColor(.secondary)
            }
            Text("The meter is not currently connected. Bring it within Bluetooth range or wake it up to open live diagnostics.")
                .font(.caption)
                .foregroundColor(.secondary)
        }
        .frame(maxWidth: .infinity, alignment: .leading)
        .padding(18)
        .meterCard(tint: meterSummary.tint, fillOpacity: 0.16, strokeOpacity: 0.22, cornerRadius: 18)
    }

    private var identifiersCard: some View {
        VStack(alignment: .leading, spacing: 10) {
            Text("Identifiers")
                .font(.headline)
            infoRow(label: "MAC Address", value: meterSummary.macAddress)
            if let advertisedName = meterSummary.advertisedName {
                infoRow(label: "Advertised as", value: advertisedName)
            }
        }
        .frame(maxWidth: .infinity, alignment: .leading)
        .padding(18)
        .meterCard(tint: .secondary, fillOpacity: 0.12, strokeOpacity: 0.18, cornerRadius: 18)
    }

    private func infoRow(label: String, value: String) -> some View {
        HStack {
            Text(label)
            Spacer()
            Text(value)
                .foregroundColor(.secondary)
                .font(.caption)
        }
    }
}

struct MeterDetailView_Previews: PreviewProvider {
    static var previews: some View {
        MeterDetailView(
            meterSummary: AppData.MeterSummary(
                macAddress: "AA:BB:CC:DD:EE:FF",
                displayName: "Desk Meter",
                modelSummary: "UM25C",
                advertisedName: "UM25C-123",
                lastSeen: Date(),
                lastConnected: Date().addingTimeInterval(-3600),
                meter: nil
            )
        )
    }
}
