import QtQuick
import QtQuick.Controls
import QtQuick.Controls.impl
import ISC

// ===== Rail Action Button =====
// Reusable pill-style button for rail action panels.
// Icon left, uppercase label, fixed height, hover/active backgrounds.
Rectangle {
    id: root

    property string label: ""
    property string iconSource: ""
    property bool destructive: false
    // Set true to show the button as "active" (e.g. a one-shot toggle
    // that's mid-operation), independent of hover/press state.
    property bool toggled: false
    // Shown on hover even while disabled, e.g. to explain *why* a greyed-out
    // action is unavailable. Empty string shows no tooltip.
    property string tooltipText: ""
    property color _textColor: !root.enabled ? Theme.sideMutedText
        : root.destructive ? Theme.statusErrorColor
        : Theme.headingColor

    signal clicked()

    implicitHeight: Theme.sideButtonHeight
    radius: 8
    color: root.toggled || (root.enabled && (mouseArea.containsMouse || mouseArea.pressed))
        ? Theme.sideActiveBackground : "transparent"
    border.width: 0

    Behavior on color {
        ColorAnimation { duration: 100 }
    }

    Row {
        anchors.fill: parent
        anchors.leftMargin: 12
        anchors.rightMargin: 12
        spacing: 10

        IconImage {
            anchors.verticalCenter: parent.verticalCenter
            width: 18
            height: 18
            source: root.iconSource
            sourceSize.width: 18
            sourceSize.height: 18
            color: root._textColor
            opacity: root.enabled ? 0.85 : 0.35
        }

        Label {
            anchors.verticalCenter: parent.verticalCenter
            text: root.label
            color: root._textColor
            font.family: Theme.uiFontFamily
            font.pixelSize: Theme.fontSm
            font.bold: true
            font.letterSpacing: 1.2
            opacity: root.enabled ? 1.0 : 0.4
            elide: Text.ElideRight
        }
    }

    MouseArea {
        id: mouseArea
        anchors.fill: parent
        // Hover stays live while disabled so tooltipText can explain why —
        // click is still gated on root.enabled below.
        hoverEnabled: true
        cursorShape: root.enabled ? Qt.PointingHandCursor : Qt.ArrowCursor
        onClicked: {
            if (root.enabled) root.clicked()
        }
    }

    AppToolTip {
        visible: root.tooltipText.length > 0 && mouseArea.containsMouse
        text: root.tooltipText
    }
}
