import QtQuick
import QtQuick.Controls
import QtQuick.Controls.impl
import QtQuick.Layouts
import ISC
import ISC.Tabs.components

// ── Status tab: Hardware Actions + Log Actions ──────────
ColumnLayout {
    spacing: Theme.sideRailSpacing

    Rectangle {
        Layout.fillWidth: true
        Layout.preferredHeight: hwActionsCol.implicitHeight + 28
        radius: Theme.sidePanelRadius
        border.color: Theme.sideBorder
        border.width: 1
        color: Theme.sidePanelBackground

        ColumnLayout {
            id: hwActionsCol
            anchors.fill: parent
            anchors.margins: 14
            spacing: 6

            Label {
                text: "HARDWARE ACTIONS"
                color: Theme.sideMutedText
                font.family: Theme.uiFontFamily
                font.pixelSize: Theme.fontSm
                font.letterSpacing: 1.5
                font.bold: true
            }

            RailActionButton {
                label: "DETECT WAFER"
                iconSource: "../icons/detect.svg"
                Layout.fillWidth: true
                enabled: !deviceController.operationInProgress
                onClicked: root._doDetect()
            }

            RailActionButton {
                label: "READ MEMORY"
                iconSource: "../icons/read.svg"
                Layout.fillWidth: true
                enabled: deviceController.waferDetected && root.waferLicensed && !deviceController.operationInProgress
                tooltipText: (deviceController.waferDetected && !root.waferLicensed)
                    ? "Wafer not licensed — load its license .bin (About dialog)" : ""
                onClicked: deviceController.readMemoryAsync()
            }

            RailActionButton {
                label: "ERASE MEMORY"
                iconSource: "../icons/erase.svg"
                Layout.fillWidth: true
                destructive: true
                enabled: deviceController.waferDetected && root.waferLicensed && !deviceController.operationInProgress
                tooltipText: (deviceController.waferDetected && !root.waferLicensed)
                    ? "Wafer not licensed — load its license .bin (About dialog)" : ""
                onClicked: deviceController.eraseMemory(
                    deviceController.selectedPort)
            }

            RailActionButton {
                label: "READ DEBUG"
                iconSource: "../icons/read.svg"
                Layout.fillWidth: true
                // One-shot toggle: "on" while the debug read is in flight,
                // resets automatically once deviceController flips status.
                toggled: deviceController.connectionStatus === "Reading debug..."
                enabled: deviceController.waferDetected && root.waferLicensed && !deviceController.operationInProgress
                tooltipText: (deviceController.waferDetected && !root.waferLicensed)
                    ? "Wafer not licensed — load its license .bin (About dialog)" : ""
                onClicked: deviceController.readDebug(deviceController.selectedPort)
            }
        }
    }

    Rectangle {
        Layout.fillWidth: true
        Layout.preferredHeight: logActionsCol.implicitHeight + 28
        radius: Theme.sidePanelRadius
        border.color: Theme.sideBorder
        border.width: 1
        color: Theme.sidePanelBackground

        ColumnLayout {
            id: logActionsCol
            anchors.fill: parent
            anchors.margins: 14
            spacing: 6

            Label {
                text: "LOG ACTIONS"
                color: Theme.sideMutedText
                font.family: Theme.uiFontFamily
                font.pixelSize: Theme.fontSm
                font.letterSpacing: 1.5
                font.bold: true
            }

            RailActionButton {
                label: "REFRESH SESSION"
                iconSource: "../icons/refresh.svg"
                Layout.fillWidth: true
                enabled: !deviceController.operationInProgress
                onClicked: deviceController.clearSession()
            }

            RailActionButton {
                label: "CLEAR LOG"
                iconSource: "../icons/x.svg"
                Layout.fillWidth: true
                onClicked: deviceController.clearActivityLog()
            }
        }
    }

    Item { Layout.fillHeight: true }
}
