# Copyright (c) 2022, Frappe and Contributors
# See license.txt
from __future__ import annotations

from typing import TYPE_CHECKING
from unittest.mock import MagicMock, patch

import jingrow
from frappe.tests.utils import FrappeTestCase

from press.press.doctype.cluster.test_cluster import create_test_cluster
from press.press.doctype.virtual_machine_image.virtual_machine_image import (
	VirtualMachineImage,
)

if TYPE_CHECKING:
	from press.press.doctype.cluster.cluster import Cluster


@patch.object(VirtualMachineImage, "client", new=MagicMock())
@patch.object(VirtualMachineImage, "after_insert", new=MagicMock())
def create_test_virtual_machine_image(
	ip: str | None = None, cluster: Cluster | None = None, series: str = "m", platform: str = "x86_64"
) -> VirtualMachineImage:
	"""Create test Virtual Machine Image pg"""
	if not ip:
		ip = frappe.mock("ipv4")
	if not cluster:
		cluster = create_test_cluster()
	from press.press.doctype.virtual_machine.test_virtual_machine import (
		create_test_virtual_machine,
	)

	vm = create_test_virtual_machine(cluster=cluster, series=series, platform=platform)

	return frappe.get_pg(
		{
			"doctype": "Virtual Machine Image",
			"virtual_machine": vm.name,
			"region": vm.region,
			"status": "Available",
			"image_id": "ami-1234567890",
			"mariadb_root_password": "password",
			"platform": platform,
		}
	).insert(ignore_if_duplicate=True)


class TestVirtualMachineImage(FrappeTestCase):
	pass
