import SwiftUI
import Bedrock

struct HabitRowModel: Identifiable {
    let id: UUID
    let title: String
    let symbolName: String
    let isCompleted: Bool
    let action: () -> Void
}

struct TodayRitualSectionView: View {
    let focusTitle: String
    let focusTheme: String
    let dayLabel: String
    let completionSummary: String
    let progress: Double
    let habitRows: [HabitRowModel]
    var iconName: String = "sparkles"
    var timeOfDay: TimeOfDay = .anytime

    var body: some View {
        VStack(alignment: .leading, spacing: Bedrock.Design.Spacing.large) {
            focusCard

            VStack(alignment: .leading, spacing: Bedrock.Design.Spacing.large) {
                HStack {
                    SectionHeaderView(
                        title: String(localized: "Habits"),
                        subtitle: String(localized: "Tap to check in")
                    )

                    Spacer()

                    // Time of day indicator
                    SymbolIcon(timeOfDay.symbolName, size: .row, color: AppTextColors.tertiary)
                        .accessibilityLabel(timeOfDay.displayName)
                }

                habitsList
            }
            .padding(.horizontal, Bedrock.Design.Spacing.large)
            .padding(.bottom, Bedrock.Design.Spacing.large)
        }
        .background(AppSurface.card)
        .clipShape(.rect(cornerRadius: Design.CornerRadius.large))
        .overlay(
            RoundedRectangle(cornerRadius: Design.CornerRadius.large)
                .stroke(AppBorder.subtle.opacity(Design.Opacity.light), lineWidth: 1)
        )
        .shadow(
            color: AppBorder.subtle.opacity(Design.Opacity.light),
            radius: AppMetrics.Shadow.radiusSmall,
            x: AppMetrics.Shadow.xOffsetNone,
            y: AppMetrics.Shadow.yOffsetSmall
        )
    }

    private var focusCard: some View {
        RitualFocusCardView(
            title: focusTitle,
            theme: focusTheme,
            dayLabel: dayLabel,
            completionSummary: completionSummary,
            progress: progress,
            iconName: iconName
        )
    }

    private var habitsList: some View {
        VStack(spacing: Bedrock.Design.Spacing.medium) {
            ForEach(habitRows) { habit in
                TodayHabitRowView(
                    title: habit.title,
                    symbolName: habit.symbolName,
                    isCompleted: habit.isCompleted,
                    horizontalPadding: 0,
                    action: habit.action
                )
            }
        }
    }
}

#Preview {
    TodayRitualSectionView(
        focusTitle: "Morning Flow",
        focusTheme: "Light and steady",
        dayLabel: "Day 3 of 28",
        completionSummary: "2 of 3 habits complete",
        progress: 0.66,
        habitRows: [
            HabitRowModel(id: UUID(), title: "Hydrate", symbolName: "drop.fill", isCompleted: true, action: {}),
            HabitRowModel(id: UUID(), title: "Move", symbolName: "figure.walk", isCompleted: false, action: {})
        ],
        iconName: "sunrise.fill",
        timeOfDay: .morning
    )
}
