//
//  SidebarPowerbanksSectionView.swift
//  USB Meter
//

import SwiftUI

struct SidebarPowerbanksSectionView: View {
    let title: String
    let powerbanks: [PowerbankSummary]
    let emptyStateText: String
    let tint: Color
    let isExpanded: Bool
    let onToggle: () -> Void
    let onAdd: () -> Void

    var body: some View {
        Section(header: headerView) {
            if isExpanded {
                ForEach(powerbanks) { powerbank in
                    NavigationLink(destination: PowerbankDetailView(powerbankID: powerbank.id)) {
                        PowerbankSidebarCardView(powerbank: powerbank)
                    }
                    .buttonStyle(.plain)
                    .transition(.opacity.combined(with: .move(edge: .top)))
                }

                if powerbanks.isEmpty {
                    Text(emptyStateText)
                        .font(.caption)
                        .foregroundColor(.secondary)
                        .padding(.vertical, 6)
                        .transition(.opacity)
                }
            }
        }
    }

    private var headerView: some View {
        HStack(alignment: .firstTextBaseline, spacing: 10) {
            Button(action: onToggle) {
                HStack(alignment: .firstTextBaseline, spacing: 4) {
                    Image(systemName: "chevron.right")
                        .font(.caption.weight(.semibold))
                        .foregroundColor(.secondary)
                        .rotationEffect(.degrees(isExpanded ? 90 : 0))
                        .animation(.easeInOut(duration: 0.22), value: isExpanded)
                    Text(title)
                        .font(.headline)
                }
            }
            .buttonStyle(.plain)
            Spacer()
            Button(action: onAdd) {
                Image(systemName: "plus.circle.fill")
                    .font(.body.weight(.semibold))
                    .foregroundColor(tint)
            }
            .buttonStyle(.plain)
            Text("\(powerbanks.count)")
                .font(.caption.weight(.bold))
                .padding(.horizontal, 10)
                .padding(.vertical, 6)
                .meterCard(tint: tint, fillOpacity: 0.18, strokeOpacity: 0.24, cornerRadius: 999)
        }
    }
}
