"""Builder for snapshot and storage actions section."""

from __future__ import annotations

from PyQt6.QtWidgets import QGroupBox, QHBoxLayout, QLabel, QLineEdit, QPushButton, QSpinBox, QVBoxLayout


def build_data_actions_group(owner) -> QGroupBox:
    """Create snapshot save controls section."""
    group = QGroupBox("Data Actions")
    layout = QVBoxLayout(group)
    layout.setSpacing(8)

    save_row = QHBoxLayout()
    save_row.setSpacing(8)

    save_button = QPushButton("Save Numpy Snapshot")
    save_button.clicked.connect(owner._save_snapshot)
    save_vna_json_button = QPushButton("Save VNA History JSON")
    save_vna_json_button.clicked.connect(owner._save_vna_history_json)
    remove_last_button = QPushButton("Remove Last Runtime Measurement")
    remove_last_button.clicked.connect(owner._remove_last_runtime_history)
    clear_history_button = QPushButton("Clear ALL Runtime History")
    clear_history_button.clicked.connect(owner._clear_all_runtime_history)
    owner._save_count = QSpinBox()
    owner._save_count.setMinimum(1)
    owner._save_count.setMaximum(10_000)
    owner._save_count.setValue(10)
    owner._vna_json_input_index = QSpinBox()
    owner._vna_json_input_index.setMinimum(0)
    owner._vna_json_input_index.setMaximum(65_535)
    owner._vna_json_input_index.setValue(0)
    owner._vna_json_output_index = QSpinBox()
    owner._vna_json_output_index.setMinimum(0)
    owner._vna_json_output_index.setMaximum(65_535)
    owner._vna_json_output_index.setValue(0)

    save_row.addWidget(save_button)
    save_row.addWidget(save_vna_json_button)
    save_row.addWidget(remove_last_button)
    save_row.addWidget(clear_history_button)
    save_row.addWidget(QLabel("Last N"))
    save_row.addWidget(owner._save_count)
    save_row.addStretch(1)
    layout.addLayout(save_row)

    json_row = QHBoxLayout()
    json_row.setSpacing(8)
    json_row.addWidget(QLabel("JSON input"))
    json_row.addWidget(owner._vna_json_input_index)
    json_row.addWidget(QLabel("output"))
    json_row.addWidget(owner._vna_json_output_index)
    json_row.addStretch(1)
    layout.addLayout(json_row)

    path_row = QHBoxLayout()
    path_row.setSpacing(8)
    owner._save_path_input = QLineEdit(str(owner._project_root / "python_app/data/snapshots"))
    browse_button = QPushButton("Browse")
    browse_button.clicked.connect(owner._browse_save_path)
    path_row.addWidget(QLabel("Path"))
    path_row.addWidget(owner._save_path_input, stretch=1)
    path_row.addWidget(browse_button)
    layout.addLayout(path_row)

    name_row = QHBoxLayout()
    name_row.setSpacing(8)
    owner._save_name_input = QLineEdit("snapshot_manual")
    name_row.addWidget(QLabel("Name"))
    name_row.addWidget(owner._save_name_input, stretch=1)
    layout.addLayout(name_row)
    return group
