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

import json
from typing import TYPE_CHECKING

import jingrow
from frappe.model.document import Document

if TYPE_CHECKING:
	from press.press.doctype.bench.bench import Bench


class BenchShell(Document):
	# 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.types import DF

		as_root: DF.Check
		bench: DF.Link | None
		command: DF.Code | None
		directory: DF.Data | None
		duration: DF.Float
		output: DF.Code | None
		returncode: DF.Int
		save_output: DF.Check
		subdir: DF.Data | None
		traceback: DF.Code | None
	# end: auto-generated types

	def run_command(self):
		frappe.only_for("System Manager")

		bench: "Bench" = frappe.get_pg("Bench", self.bench)
		try:
			result = bench.docker_execute(self.command, self.subdir, self.save_output, as_root=self.as_root)
		except Exception:
			self.save_output = False
			self.output = None
			self.traceback = frappe.get_traceback()
			return

		self.output = result.get("output")
		self.directory = result.get("directory")
		self.traceback = result.get("traceback")
		self.returncode = result.get("returncode")
		self.duration = result.get("duration")
		frappe.db.commit()


@jingrow.whitelist()
def run_command(pg):
	bench_shell: "BenchShell" = frappe.get_pg(json.loads(pg))
	bench_shell.run_command()
	return bench_shell.as_dict()
