import SwiftUI

/// Updates screen — desktop + Android parity.
/// Shows app version info, up-to-date status, release notes, and a "Check for Updates" button.
struct UpdatesView: View {
    @Environment(\.dismiss) private var dismiss

    var body: some View {
        ScrollView(showsIndicators: false) {
            VStack(spacing: 0) {
                statusCard
                versionCard
                helpText
                checkButton
            }
            .padding(.horizontal, 16)
            .padding(.top, 16)
            .padding(.bottom, 100)
        }
        .background(RosettaColors.Adaptive.background)
        .scrollContentBackground(.hidden)
        .navigationBarTitleDisplayMode(.inline)
        .navigationBarBackButtonHidden(true)
        .enableSwipeBack()
        .toolbar { toolbarContent }
        .toolbarBackground(.hidden, for: .navigationBar)
    }

    // MARK: - Toolbar

    @ToolbarContentBuilder
    private var toolbarContent: some ToolbarContent {
        ToolbarItem(placement: .navigationBarLeading) {
            Button {
                dismiss()
            } label: {
                Image(systemName: "chevron.left")
                    .font(.system(size: 16, weight: .semibold))
                    .foregroundStyle(RosettaColors.Adaptive.text)
                    .frame(width: 44, height: 44)
            }
            .buttonStyle(.plain)
            .glassCircle()
        }

        // No title — per user request
    }

    // MARK: - Status Card

    private var statusCard: some View {
        SettingsCard {
            HStack(spacing: 10) {
                Image(systemName: "checkmark.circle.fill")
                    .font(.system(size: 22))
                    .foregroundStyle(RosettaColors.success)

                VStack(alignment: .leading, spacing: 2) {
                    Text("App is up to date")
                        .font(.system(size: 15, weight: .medium))
                        .foregroundStyle(RosettaColors.success)

                    Text("You're using the latest version")
                        .font(.system(size: 13))
                        .foregroundStyle(RosettaColors.Adaptive.textSecondary)
                }

                Spacer()
            }
            .padding(16)
        }
    }

    // MARK: - Version Card

    private var versionCard: some View {
        SettingsCard {
            VStack(spacing: 0) {
                versionRow(title: "Application Version", value: ReleaseNotes.appVersion)

                Divider()
                    .background(RosettaColors.Adaptive.divider)
                    .padding(.horizontal, 16)

                versionRow(title: "Build Number", value: ReleaseNotes.buildNumber)
            }
        }
        .padding(.top, 16)
    }

    private func versionRow(title: String, value: String) -> some View {
        HStack {
            Text(title)
                .font(.system(size: 15))
                .foregroundStyle(RosettaColors.Adaptive.text)

            Spacer()

            Text(value)
                .font(.system(size: 15, weight: .medium))
                .foregroundStyle(RosettaColors.Adaptive.textSecondary)
        }
        .padding(.horizontal, 16)
        .frame(height: 48)
    }

    // MARK: - Help Text

    private var helpText: some View {
        Text("We recommend always keeping the app up to date to improve visual effects and have the latest features.")
            .font(.system(size: 13))
            .foregroundStyle(RosettaColors.Adaptive.textSecondary)
            .frame(maxWidth: .infinity, alignment: .leading)
            .padding(.horizontal, 16)
            .padding(.top, 12)
            .padding(.bottom, 8)
    }

    // MARK: - Check Button

    private var checkButton: some View {
        Button {} label: {
            Text("Check for Updates")
                .font(.system(size: 17, weight: .medium))
                .foregroundStyle(.white)
                .frame(maxWidth: .infinity)
                .frame(height: 48)
        }
        .buttonStyle(RosettaPrimaryButtonStyle())
        .padding(.top, 16)
    }

    // MARK: - Release Notes

    /// Desktop parity: shows release notes for each version.
    /// Desktop sends these as system messages from the "updates" account;
    /// iOS displays them inline on the Updates settings page.
    private var releaseNotesSection: some View {
        VStack(alignment: .leading, spacing: 16) {
            Text("What's New")
                .font(.system(size: 20, weight: .bold))
                .foregroundStyle(RosettaColors.Adaptive.text)
                .padding(.horizontal, 4)

            ForEach(ReleaseNotes.entries) { entry in
                releaseNoteCard(entry)
            }
        }
        .padding(.top, 28)
    }

    private func releaseNoteCard(_ entry: ReleaseNotes.Entry) -> some View {
        SettingsCard {
            VStack(alignment: .leading, spacing: 12) {
                HStack(spacing: 8) {
                    Image(systemName: "sparkles")
                        .font(.system(size: 14, weight: .semibold))
                        .foregroundStyle(RosettaColors.primaryBlue)

                    Text("Version \(entry.version)")
                        .font(.system(size: 15, weight: .semibold))
                        .foregroundStyle(RosettaColors.Adaptive.text)
                }

                VStack(alignment: .leading, spacing: 6) {
                    ForEach(entry.changes, id: \.self) { change in
                        HStack(alignment: .top, spacing: 8) {
                            Text("\u{2022}")
                                .font(.system(size: 15))
                                .foregroundStyle(RosettaColors.Adaptive.textSecondary)

                            Text(change)
                                .font(.system(size: 14))
                                .foregroundStyle(RosettaColors.Adaptive.textSecondary)
                                .fixedSize(horizontal: false, vertical: true)
                        }
                    }
                }
            }
            .padding(16)
        }
    }
}
