import QtQuick
import QtQuick.Controls
import QtQuick.Dialogs
import QtQuick.Layouts
import ISC

// ===== First-Launch Wizard =====
// Shown once, on first launch (no settings.json yet). Two skippable steps:
// load a license, then pick a save folder. Either step can be skipped —
// review-mode features work without a license, and save dir already has a
// sensible default, so this is guidance, not a gate.
Popup {
    id: root
    modal: true
    dim: true
    closePolicy: Popup.NoAutoClose
    width: 520
    height: 320
    anchors.centerIn: Overlay.overlay

    property int step: 0

    signal finished()

    background: Rectangle {
        radius: Theme.radiusMd
        color: Theme.cardBackground
        border.color: Theme.cardBorder
        border.width: 1
    }

    FileDialog {
        id: licenseFileDialog
        title: "Load License"
        nameFilters: ["License files (*.bin)"]
        onAccepted: {
            licenseModel.loadLicenseFile(selectedFile.toString())
            root.step = 1
        }
    }

    FolderDialog {
        id: saveDirDialog
        title: "Choose a folder to save data"
        onAccepted: {
            var dir = decodeURIComponent(String(selectedFolder).replace(/^file:\/\//, ""))
            deviceController.setSaveDataDir(dir)
            file_browser.setCurrentDirectory(dir)
            root.finished()
        }
    }

    ColumnLayout {
        anchors.fill: parent
        anchors.margins: 28
        spacing: 20

        Label {
            text: "Welcome to ISenseCloud"
            font.pixelSize: Theme.font2xl
            font.bold: true
            color: Theme.headingColor
        }

        // ── Step 1: License ──
        ColumnLayout {
            visible: root.step === 0
            Layout.fillWidth: true
            Layout.fillHeight: true
            spacing: 12

            Label {
                text: "Load your wafer license to enable live device actions\n(Detect, Read, Erase, Debug). You can also do this later\nfrom About."
                font.pixelSize: Theme.fontMd
                color: Theme.bodyColor
                Layout.fillWidth: true
                wrapMode: Text.WordWrap
            }
            Item { Layout.fillHeight: true }
        }

        // ── Step 2: Save Directory ──
        ColumnLayout {
            visible: root.step === 1
            Layout.fillWidth: true
            Layout.fillHeight: true
            spacing: 12

            Label {
                text: "Choose where wafer read data gets saved. If you skip\nthis, it defaults to a \"csv\" folder inside your app data\ndirectory — changeable anytime from Status."
                font.pixelSize: Theme.fontMd
                color: Theme.bodyColor
                Layout.fillWidth: true
                wrapMode: Text.WordWrap
            }
            Item { Layout.fillHeight: true }
        }

        // ── Buttons ──
        RowLayout {
            Layout.fillWidth: true
            spacing: 12

            Button {
                text: "Skip"
                Layout.preferredWidth: 100
                Layout.preferredHeight: 36
                onClicked: root.step === 0 ? (root.step = 1) : root.finished()

                background: Rectangle {
                    radius: Theme.radiusSm
                    color: parent.hovered ? Theme.buttonNeutralHover : Theme.buttonNeutralBackground
                    border.color: Theme.fieldBorder
                    border.width: 1
                }
                contentItem: Label {
                    text: parent.text
                    color: Theme.buttonNeutralText
                    font.pixelSize: Theme.fontMd
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                }
            }

            Item { Layout.fillWidth: true }

            Button {
                text: root.step === 0 ? "Load License" : "Choose Folder"
                Layout.preferredWidth: 160
                Layout.preferredHeight: 36
                onClicked: root.step === 0 ? licenseFileDialog.open() : saveDirDialog.open()

                background: Rectangle {
                    radius: Theme.radiusSm
                    color: Theme.primaryAccent
                }
                contentItem: Label {
                    text: parent.text
                    color: "#FFFFFF"
                    font.pixelSize: Theme.fontMd
                    font.bold: true
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                }
            }
        }
    }
}
