"""Builder for pinned primary action controls."""

from __future__ import annotations

from PyQt6.QtWidgets import QGridLayout, QGroupBox, QPushButton, QSizePolicy


def _expanding_button(label: str) -> QPushButton:
    """Create horizontally expanding action button."""
    button = QPushButton(label)
    button.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed)
    return button


def build_primary_actions_group(owner) -> QGroupBox:
    """Create pinned action block combining pipeline and hardware actions."""
    group = QGroupBox("Actions")
    layout = QGridLayout(group)
    layout.setHorizontalSpacing(8)
    layout.setVerticalSpacing(8)

    start_button = _expanding_button("Start")
    start_button.clicked.connect(owner._start_run)
    layout.addWidget(start_button, 0, 0)

    single_button = _expanding_button("Single Capture")
    single_button.clicked.connect(owner._start_single_capture)
    layout.addWidget(single_button, 0, 1)

    stop_button = _expanding_button("Stop")
    stop_button.clicked.connect(owner._stop_run)
    layout.addWidget(stop_button, 0, 2)

    apply_radar_button = _expanding_button("Apply Radar")
    apply_radar_button.clicked.connect(owner._apply_radar_settings)
    layout.addWidget(apply_radar_button, 1, 0)

    load_config_button = _expanding_button("Load Config")
    load_config_button.clicked.connect(owner._load_config_from_dialog)
    layout.addWidget(load_config_button, 1, 1)

    save_config_button = _expanding_button("Save Config")
    save_config_button.clicked.connect(owner._save_current_config)
    layout.addWidget(save_config_button, 1, 2)

    preprocess_button = _expanding_button("Preprocessing")
    preprocess_button.clicked.connect(owner._open_preprocess_panel)
    layout.addWidget(preprocess_button, 2, 0)

    clear_history_button = _expanding_button("Clear Runtime History")
    clear_history_button.clicked.connect(owner._clear_all_runtime_history)
    layout.addWidget(clear_history_button, 2, 1, 1, 2)

    layout.setColumnStretch(0, 1)
    layout.setColumnStretch(1, 1)
    layout.setColumnStretch(2, 1)
    return group
