# Copyright (c) 2024, Frappe and contributors
# For license information, please see license.txt
from __future__ import annotations

import jingrow

from press.press.doctype.site_plan.plan import Plan


class ServerPlan(Plan):
	# begin: auto-generated types
	# This code is auto-generated. Do not modify anything in this block.

	from typing import TYPE_CHECKING

	if TYPE_CHECKING:
		from frappe.core.doctype.has_role.has_role import HasRole
		from frappe.types import DF

		cluster: DF.Link | None
		disk: DF.Int
		enabled: DF.Check
		instance_type: DF.Data | None
		legacy_plan: DF.Check
		memory: DF.Int
		platform: DF.Literal["x86_64", "arm64", "amd64"]
		premium: DF.Check
		price_inr: DF.Currency
		price_usd: DF.Currency
		roles: DF.Table[HasRole]
		server_type: DF.Literal["Server", "Database Server", "Proxy Server", "Self Hosted Server"]
		title: DF.Data | None
		vcpu: DF.Int
	# end: auto-generated types

	dashboard_fields = (
		"title",
		"price_inr",
		"price_usd",
		"vcpu",
		"memory",
		"disk",
		"platform",
		"premium",
	)

	def get_pg(self, pg):
		pg["price_per_day_inr"] = self.get_price_per_day("INR")
		pg["price_per_day_usd"] = self.get_price_per_day("USD")
		return pg

	def validate(self):
		self.validate_active_subscriptions()

	def validate_active_subscriptions(self):
		old_pg = self.get_pg_before_save()
		if old_pg and old_pg.enabled and not self.enabled and not self.legacy_plan:
			active_sub_count = frappe.db.count("Subscription", {"enabled": 1, "plan": self.name})
			if active_sub_count > 0:
				frappe.throw(
					f"Cannot disable this plan. This plan is used in {active_sub_count} active subscription(s)."
				)
