//
//  SearchWidgetLiveActivity.swift
//  SearchWidget
//
//  Created by Johann Villegas on 9/04/25.
//

import ActivityKit
import WidgetKit
import SwiftUI

struct SearchWidgetAttributes: ActivityAttributes {
    public typealias LiveDeliveryData = ContentState
    
    public struct ContentState: Codable, Hashable {
        var title: String
        var body: String
        var searchTerm: String
        var totalResults: Int
        var timestamp: Int64
    }
    
    var id = UUID()
}

struct SearchWidgetLiveActivity: Widget {
    var body: some WidgetConfiguration {
        ActivityConfiguration(for: SearchWidgetAttributes.self) { context in
            // Lock Screen/Banner UI
            VStack {
                HStack {
                    Text(context.state.title)
                        .font(.headline)
                    Spacer()
                    Text("\(context.state.totalResults)")
                        .font(.caption)
                }
                Text(context.state.body)
                    .font(.subheadline)
                if !context.state.searchTerm.isEmpty {
                    Text("Search: \(context.state.searchTerm)")
                        .font(.caption)
                }
            }
            .padding()
        } dynamicIsland: { context in
            // Dynamic Island UI
            DynamicIsland {
                // Expanded UI
                DynamicIslandExpandedRegion(.leading) {
                    Text(context.state.title)
                        .font(.headline)
                }
                
                DynamicIslandExpandedRegion(.trailing) {
                    Text("\(context.state.totalResults)")
                        .font(.caption)
                }
                
                DynamicIslandExpandedRegion(.bottom) {
                    Text(context.state.body)
                        .font(.subheadline)
                    if !context.state.searchTerm.isEmpty {
                        Text("Search: \(context.state.searchTerm)")
                            .font(.caption)
                    }
                }
            } compactLeading: {
                // Compact Leading
                Text("\(context.state.totalResults)")
            } compactTrailing: {
                // Compact Trailing
                Image(systemName: "magnifyingglass")
            } minimal: {
                // Minimal UI
                Image(systemName: "magnifyingglass")
            }
        }
    }
}
