import SwiftUI

struct StatusBadge: View {
    let status: String

    var body: some View {
        HStack(spacing: 5) {
            Image(systemName: statusIcon)
                .font(.system(size: 10, weight: .bold))

            Text(formatStatus(status))
                .font(.system(size: 11, weight: .semibold, design: .rounded))
        }
        .padding(.horizontal, 10)
        .padding(.vertical, 5)
        .foregroundColor(statusColor)
        .background(
            Capsule()
                .fill(statusColor.opacity(0.12))
                .overlay(
                    Capsule()
                        .stroke(statusColor.opacity(0.2), lineWidth: 1)
                )
        )
        .accessibilityElement(children: .combine)
        .accessibilityLabel(A11y.Task.statusBadge(status: formatStatus(status)))
    }

    private func formatStatus(_ status: String) -> String {
        switch status {
        case "in_progress": return "In Progress"
        case "cancelled": return "Cancelled"
        default: return status.capitalized
        }
    }

    private var statusIcon: String {
        switch status {
        case "completed": return "checkmark.circle.fill"
        case "in_progress": return "play.circle.fill"
        case "pending": return "clock.fill"
        case "cancelled": return "xmark.circle.fill"
        default: return "circle.fill"
        }
    }

    private var statusColor: Color {
        switch status {
        case "completed": return Color.appPrimary
        case "in_progress": return Color.appAccent
        case "pending": return Color.appSecondary
        case "cancelled": return Color.appError
        default: return Color.appTextSecondary
        }
    }
}

#Preview {
    VStack(spacing: 12) {
        StatusBadge(status: "pending")
        StatusBadge(status: "in_progress")
        StatusBadge(status: "completed")
        StatusBadge(status: "cancelled")
    }
    .padding()
    .background(WarmGradientBackground())
}
