import SwiftUI

struct DoneView: View {
    @Environment(WizardModel.self) private var model

    var body: some View {
        VStack(spacing: 24) {
            Image(systemName: "checkmark.seal.fill")
                .font(.system(size: 80))
                .foregroundStyle(.green)

            Text("Schutz aktiv")
                .font(.largeTitle).bold()

            Text("Dein iPhone ist jetzt an ReBreak gebunden. Casino-Domains werden via NEFilter blockiert — auch wenn du es willst.")
                .multilineTextAlignment(.center)
                .foregroundStyle(.secondary)
                .padding(.horizontal, 40)

            statusSummary

            cooldownNote

            VStack(spacing: 8) {
                Button("ReBreak öffnen") {
                    if let url = URL(string: "rebreak://") {
                        NSWorkspace.shared.open(url)
                    }
                }
                .buttonStyle(.borderedProminent)
                .controlSize(.large)

                Button("Zurück zur Geräte-Übersicht") {
                    model.returnToHub()
                }
                .buttonStyle(.plain)
                .foregroundStyle(.secondary)
            }
        }
        .padding(40)
        .frame(maxWidth: .infinity, maxHeight: .infinity)
    }

    private var statusSummary: some View {
        VStack(alignment: .leading, spacing: 8) {
            statusRow(label: "Supervised", on: model.device?.isSupervised == true)
            statusRow(label: "MDM-Enrolled", on: model.device?.isEnrolled == true)
            statusRow(label: "App managed (nicht löschbar)", on: model.device?.isManaged == true)
            statusRow(label: "NEFilter aktiv (Casino-Block)", on: model.device?.isFilterActive == true)
        }
        .padding()
        .frame(maxWidth: 400)
        .background(Color.green.opacity(0.05))
        .cornerRadius(8)
    }

    private func statusRow(label: String, on: Bool) -> some View {
        HStack {
            Image(systemName: on ? "checkmark.circle.fill" : "circle")
                .foregroundStyle(on ? .green : .gray)
            Text(label)
            Spacer()
        }
        .font(.callout)
    }

    private var cooldownNote: some View {
        HStack(alignment: .top, spacing: 8) {
            Image(systemName: "clock.badge.exclamationmark")
                .foregroundStyle(.orange)
            VStack(alignment: .leading, spacing: 4) {
                Text("7-Tage-Cooldown").bold()
                Text("Wenn du den Schutz aufheben willst, gibt's eine 7-Tage-Wartezeit. Das ist Absicht — die Bindung soll deinen impulsiven 'jetzt-doch-zocken'-Moment überdauern.")
                    .font(.caption)
                    .foregroundStyle(.secondary)
            }
        }
        .padding(10)
        .frame(maxWidth: 400, alignment: .leading)
        .background(Color.orange.opacity(0.08))
        .cornerRadius(6)
    }
}
