//
//  AppUpdateView.swift
//  punchnet
//
//  Created by 安礼成 on 2026/3/23.
//
import SwiftUI

struct AppUpdateView: View {
    let info: AppUpgradeInfo
    var dismissAction: () -> Void
    
    var body: some View {
        VStack(spacing: 0) {
            // 顶部 Header
            VStack(spacing: 12) {
                Image(systemName: "arrow.up.rocket.fill")
                    .font(.system(size: 40))
                    .foregroundStyle(.blue.gradient)
                
                Text("新版本已就绪")
                    .font(.title3.bold())
                
                Text("版本号: \(info.latestVersion)")
                    .font(.caption)
                    .foregroundColor(.secondary)
            }
            .padding(.top, 30)
            .padding(.bottom, 20)
            .frame(maxWidth: .infinity)
            .background(Color.blue.opacity(0.05))

            // 更新日志
            VStack(alignment: .leading, spacing: 12) {
                Text("更新说明")
                    .font(.subheadline.bold())
                
                ScrollView {
                    Text(info.releaseNotes)
                        .font(.subheadline)
                        .foregroundColor(.secondary)
                        .lineSpacing(4)
                        .frame(maxWidth: .infinity, alignment: .leading)
                }
                .frame(maxHeight: 120)
                
                // 操作按钮
                HStack(spacing: 12) {
                    if !info.forceUpdate {
                        Button("稍后") {
                            dismissAction()
                        }
                        .buttonStyle(.plain)
                        .frame(width: 80)
                    }
                    
                    Button {
                        if let url = URL(string: info.downloadUrl) {
                            NSWorkspace.shared.open(url)
                        }
                    } label: {
                        Text(info.forceUpdate ? "立即更新" : "下载并安装")
                            .fontWeight(.bold)
                            .frame(maxWidth: .infinity)
                    }
                    .buttonStyle(.borderedProminent)
                    .controlSize(.large)
                }
                .padding(.top, 10)
            }
            .padding(25)
        }
        .frame(width: 360)
        .background(VisualEffectView(material: .underWindowBackground, blendingMode: .behindWindow))
    }
    
}
