//
//  MusadaqLiveActivityBundle.swift
//  MusadaqLiveActivity
//
//  Created by Hamza Aleghwairyeen on 07/05/2026.
//

import WidgetKit
import SwiftUI
import ActivityKit

// ─── 1. Data Model ───────────────────────────────────
struct InvoiceBatchAttributes: ActivityAttributes {
    public struct ContentState: Codable, Hashable {
        var current: Int
        var total: Int
        var isDone: Bool
    }
    var companyName: String
}

// ─── 2. Bundle ───────────────────────────────────────
@main
struct MusadaqLiveActivityBundle: WidgetBundle {
    var body: some Widget {
        InvoiceBatchLiveActivity()
    }
}

// ─── 3. Widget ───────────────────────────────────────
struct InvoiceBatchLiveActivity: Widget {
    var body: some WidgetConfiguration {
        ActivityConfiguration(for: InvoiceBatchAttributes.self) { context in
            // Lock Screen UI
            ZStack {
                Color(red: 0.043, green: 0.098, blue: 0.161) // #0B1929
                HStack(spacing: 12) {
                    Image(systemName: context.state.isDone
                          ? "checkmark.doc.fill" : "arrow.up.doc.fill")
                        .foregroundColor(Color(red: 0.831, green: 0.659, blue: 0.263)) // #D4A843
                        .font(.title2)
                    VStack(alignment: .leading, spacing: 4) {
                        Text(context.state.isDone ? "✅ تم الرفع بنجاح" : "مُصادَق — جارٍ الرفع...")
                            .font(.caption.bold())
                            .foregroundColor(.white)
                        ProgressView(
                            value: Double(context.state.current),
                            total: Double(context.state.total)
                        )
                        .tint(Color(red: 0.831, green: 0.659, blue: 0.263))
                        Text("\(context.state.current) / \(context.state.total) فاتورة — \(context.attributes.companyName)")
                            .font(.caption2)
                            .foregroundColor(.gray)
                    }
                }
                .padding()
            }
        } dynamicIsland: { context in
            DynamicIsland {
                DynamicIslandExpandedRegion(.leading) {
                    Image(systemName: "arrow.up.doc.fill")
                        .foregroundColor(Color(red: 0.831, green: 0.659, blue: 0.263))
                }
                DynamicIslandExpandedRegion(.trailing) {
                    Text("\(context.state.current)/\(context.state.total)")
                        .font(.caption.bold()).foregroundColor(.white)
                }
                DynamicIslandExpandedRegion(.bottom) {
                    ProgressView(value: Double(context.state.current),
                                 total: Double(context.state.total))
                        .tint(Color(red: 0.831, green: 0.659, blue: 0.263))
                }
            } compactLeading: {
                Image(systemName: "arrow.up.doc.fill")
                    .foregroundColor(Color(red: 0.831, green: 0.659, blue: 0.263))
                    .font(.caption)
            } compactTrailing: {
                Text("\(context.state.current)/\(context.state.total)")
                    .font(.caption2.bold()).foregroundColor(.white)
            } minimal: {
                Image(systemName: "arrow.up.doc.fill")
                    .foregroundColor(Color(red: 0.831, green: 0.659, blue: 0.263))
            }
        }
    }
}
