"""
workflow_restart.py — Restart IOM workflow.
Sends a GracefulRestart to a selected IOM via either a direct network
Redfish connection or a serial console curl command.
"""

import time

from redfish import _redfish_restart_iom
from workflow_serial import (
    detect_serial_device,
    open_serial_connection,
    _login_serial_console,
    _serial_redfish_request,
)
from ui import (
    _c, C,
    banner, rule,
    info, ok, warn, error,
    prompt, prompt_ip, prompt_password, prompt_yn,
)


def _iom_prompt() -> str:
    """Ask which IOM to target and return 'IOM1' or 'IOM2'."""
    print()
    print("  Which IOM would you like to restart?")
    print(f"    {_c(C.BOLD, '1')}  IOM1")
    print(f"    {_c(C.BOLD, '2')}  IOM2")
    print()
    while True:
        choice = prompt("Select [1/2]")
        if choice in ("1", "2"):
            break
        warn("Please enter 1 or 2.")
    return "IOM1" if choice == "1" else "IOM2"


# ── Network restart ────────────────────────────────────────────────────────────

def _restart_via_network():
    banner()
    rule("Restart IOM -- Network Connection")

    print()
    password = prompt_password()
    print()
    ip  = prompt_ip("  IOM IP address (either IOM1 or IOM2)")
    iom = _iom_prompt()

    print()
    if not prompt_yn(f"Restart {iom} at {ip}?", default=False):
        info("Restart cancelled.")
        print()
        prompt("Press Enter to return to main menu")
        return

    info(f"Sending restart command to {iom}...")
    ok_flag, data = _redfish_restart_iom(password, ip, iom)
    if ok_flag:
        ok(f"{iom} restart initiated.")
        info("The IOM will be temporarily unreachable while rebooting.")
    else:
        error(f"Restart failed: {data}")

    print()
    prompt("Press Enter to return to main menu")


# ── Serial restart ─────────────────────────────────────────────────────────────

def _restart_via_serial():
    banner()
    rule("Restart IOM -- Serial Connection")

    device = detect_serial_device()
    if not device:
        error("Could not detect a serial device. Returning to main menu.")
        time.sleep(2)
        return

    ser = open_serial_connection(device)
    if not ser:
        error("Could not open serial port. Returning to main menu.")
        time.sleep(2)
        return

    print()
    logged_in, password = _login_serial_console(ser)
    if not logged_in:
        error("Could not log in to IOM console. Returning to main menu.")
        if ser.is_open:
            ser.close()
        time.sleep(2)
        return

    iom = _iom_prompt()

    print()
    if not prompt_yn(f"Restart {iom} via serial?", default=False):
        info("Restart cancelled.")
        if ser.is_open:
            ser.close()
        print()
        prompt("Press Enter to return to main menu")
        return

    info(f"Sending restart command to {iom}...")
    ok_flag, data = _serial_redfish_request(
        ser, password, "POST",
        f"/redfish/v1/Managers/{iom}/Actions/Manager.Reset",
        {"ResetType": "GracefulRestart"},
    )
    if ok_flag:
        ok(f"{iom} restart initiated.")
        info("The IOM will be temporarily unreachable while rebooting.")
    else:
        error(f"Restart failed: {data}")

    if ser.is_open:
        ser.close()
        ok(f"Serial port {device} closed.")

    print()
    prompt("Press Enter to return to main menu")


# ── Top-level entry point ──────────────────────────────────────────────────────

def restart_iom_workflow():
    banner()
    rule("Restart IOM")

    print("  How do you want to connect to the IOM?")
    print()
    print(f"    {_c(C.BOLD, '1')}  Network connection")
    print(f"    {_c(C.BOLD, '2')}  Serial connection")
    print(f"    {_c(C.BOLD, '3')}  Cancel")
    print()

    while True:
        choice = prompt("Select [1/2/3]")
        if choice in ("1", "2", "3"):
            break
        warn("Please enter 1, 2, or 3.")

    if choice == "1":
        _restart_via_network()
    elif choice == "2":
        _restart_via_serial()
