from ..models.workflow import VmConfig
from .base import (
    AuthenticatedSession,
    Bridge,
    IsoImage,
    Node,
    Pool,
    ProxmoxService,
    Realm,
    Storage,
    VmCreationResult,
)


class FakeProxmoxService(ProxmoxService):
    mode = "fake"

    def __init__(self) -> None:
        self.created_vms: list[VmCreationResult] = []
        self.start_after_create_requests: list[bool] = []

    def check_connectivity(self) -> str:
        return "fake transport reachable"

    def check_api_base(self) -> str:
        return "fake-api-base"

    def load_realms(self) -> list[Realm]:
        return [
            Realm(name="pam", title="Linux PAM standard authentication", default=True),
            Realm(name="pve", title="Proxmox VE authentication server"),
        ]

    def login(self, username: str, password: str, realm: str) -> AuthenticatedSession:
        if not username or not password:
            raise ValueError("Username and password are required.")
        return AuthenticatedSession(username=f"{username}@{realm}", ticket="fake-ticket")

    def load_nodes(self) -> list[Node]:
        return [Node(name="fake-node-01", status="online")]

    def load_next_vmid(self) -> int:
        return 123

    def load_pools(self) -> list[Pool]:
        return [Pool(poolid="lab"), Pool(poolid="sandbox")]

    def load_existing_tags(self) -> list[str]:
        return ["codex-e2e", "linux", "test"]

    def load_bridges(self, node: str) -> list[Bridge]:
        return [Bridge(iface="vmbr9"), Bridge(iface="vmbr0")]

    def load_storages(self, node: str) -> list[Storage]:
        return [
            Storage(storage="cephfs", node=node, content=("iso", "backup")),
            Storage(storage="ceph-pool", node=node, content=("images",)),
        ]

    def load_isos(self, node: str, storage: str) -> list[IsoImage]:
        return [
            IsoImage(
                volid=f"{storage}:iso/nixos-minimal-24-11.1234abcd-x86_64-linux.iso",
                storage=storage,
                node=node,
            )
        ]

    def create_vm(self, config: VmConfig, start_after_create: bool = False) -> VmCreationResult:
        name = config.general.name
        if not name.startswith("codex-e2e-"):
            name = f"codex-e2e-{name}"
        self.start_after_create_requests.append(start_after_create)
        result = VmCreationResult(
            node=config.general.node,
            vmid=config.general.vmid,
            name=name,
            serial_console_configured=True,
            ha_configured=config.general.ha_enabled,
        )
        self.created_vms.append(result)
        return result
