//
//  ShiftActivityWidgetLiveActivity.swift
//  ShiftActivityWidget
//
//  Created by Ryan VanVuren on 12/29/25.
//

import ActivityKit
import WidgetKit
import SwiftUI

struct ShiftActivityWidgetLiveActivity: Widget {
    var body: some WidgetConfiguration {
        ActivityConfiguration(for: ShiftActivityAttributes.self) { context in
            // Lock screen/banner UI
            LockScreenView(context: context)
        } dynamicIsland: { context in
            DynamicIsland {
                // Expanded region
                DynamicIslandExpandedRegion(.leading) {
                    VStack(alignment: .leading) {
                        Text(context.attributes.position)
                            .font(.caption)
                            .foregroundColor(.secondary)
                        Text(context.state.elapsedFormatted)
                            .font(.title2)
                            .fontWeight(.bold)
                    }
                }

                DynamicIslandExpandedRegion(.trailing) {
                    VStack(alignment: .trailing) {
                        Text("Shift")
                            .font(.caption)
                            .foregroundColor(.secondary)
                        Text(context.state.remainingFormatted)
                            .font(.title3)
                            .fontWeight(.medium)
                    }
                }

                DynamicIslandExpandedRegion(.bottom) {
                    VStack(spacing: 8) {
                        // Progress bar
                        ProgressView(value: context.state.progress)
                            .progressViewStyle(.linear)
                            .tint(.purple)

                        HStack {
                            Text(context.attributes.shiftStart)
                                .font(.caption2)
                                .foregroundColor(.secondary)
                            Spacer()
                            Text(context.attributes.storeName)
                                .font(.caption2)
                                .foregroundColor(.purple)
                            Spacer()
                            Text(context.attributes.shiftEnd)
                                .font(.caption2)
                                .foregroundColor(.secondary)
                        }
                    }
                    .padding(.horizontal, 4)
                }
            } compactLeading: {
                // Compact leading - clock icon with elapsed time
                HStack(spacing: 4) {
                    Image(systemName: "clock.fill")
                        .foregroundColor(.purple)
                    Text(context.state.elapsedFormatted)
                        .font(.caption)
                        .fontWeight(.medium)
                }
            } compactTrailing: {
                // Compact trailing - progress indicator
                Text("\(Int(context.state.progress * 100))%")
                    .font(.caption)
                    .foregroundColor(.purple)
                    .fontWeight(.medium)
            } minimal: {
                // Minimal - just the clock icon
                Image(systemName: "clock.fill")
                    .foregroundColor(.purple)
            }
        }
    }
}

// MARK: - Lock Screen View

struct LockScreenView: View {
    let context: ActivityViewContext<ShiftActivityAttributes>

    var body: some View {
        HStack(spacing: 12) {
            // Left side - elapsed time
            VStack(alignment: .leading, spacing: 4) {
                Text("ON SHIFT")
                    .font(.caption2)
                    .fontWeight(.semibold)
                    .foregroundColor(.purple)

                Text(context.state.elapsedFormatted)
                    .font(.title)
                    .fontWeight(.bold)

                Text(context.attributes.position)
                    .font(.caption)
                    .foregroundColor(.secondary)
            }

            Spacer()

            // Right side - shift info and progress
            VStack(alignment: .trailing, spacing: 4) {
                Text(context.attributes.storeName)
                    .font(.caption)
                    .foregroundColor(.secondary)

                Text(context.state.remainingFormatted)
                    .font(.subheadline)
                    .fontWeight(.medium)

                // Mini progress bar
                ProgressView(value: context.state.progress)
                    .progressViewStyle(.linear)
                    .tint(.purple)
                    .frame(width: 80)
            }
        }
        .padding()
        .background(Color(.systemBackground))
    }
}

// MARK: - Previews

extension ShiftActivityAttributes {
    fileprivate static var preview: ShiftActivityAttributes {
        ShiftActivityAttributes(
            shiftStart: "9:00 AM",
            shiftEnd: "5:00 PM",
            position: "Cashier",
            storeName: "Ottawa Store",
            totalHours: 8.0
        )
    }
}

extension ShiftActivityAttributes.ContentState {
    fileprivate static var working: ShiftActivityAttributes.ContentState {
        ShiftActivityAttributes.ContentState(
            elapsedMinutes: 150,
            progress: 0.31,
            elapsedFormatted: "2h 30m",
            remainingFormatted: "5h 30m left"
        )
    }

    fileprivate static var almostDone: ShiftActivityAttributes.ContentState {
        ShiftActivityAttributes.ContentState(
            elapsedMinutes: 450,
            progress: 0.94,
            elapsedFormatted: "7h 30m",
            remainingFormatted: "30m left"
        )
    }
}

#Preview("Notification", as: .content, using: ShiftActivityAttributes.preview) {
    ShiftActivityWidgetLiveActivity()
} contentStates: {
    ShiftActivityAttributes.ContentState.working
    ShiftActivityAttributes.ContentState.almostDone
}
