// Copyright (c) 2025, Frappe and contributors
// For license information, please see license.txt

frappe.ui.form.on('Server Snapshot Recovery', {
	refresh(frm) {
		[
			[
				'Provision Servers',
				'provision_servers',
				true,
				frm.pg.status === 'Draft',
			],
			[
				'Archive Servers',
				'archive_servers',
				true,
				(frm.pg.app_server || frm.pg.database_server) &&
					(!frm.pg.app_server_archived || !frm.pg.database_server_archived),
			],
			['Expire Backups', 'expire_backups', true, frm.pg.status === 'Restored'],
		].forEach(([label, method, confirm, condition]) => {
			if (typeof condition === 'undefined' || condition) {
				frm.add_custom_button(
					label,
					() => {
						if (confirm) {
							frappe.confirm(
								`Are you sure you want to ${label.toLowerCase()}?`,
								() =>
									frm.call(method).then((r) => {
										if (r.message) {
											frappe.msgprint(r.message);
										} else {
											frm.refresh();
										}
									}),
							);
						} else {
							frm.call(method).then((r) => {
								if (r.message) {
									frappe.msgprint(r.message);
								} else {
									frm.refresh();
								}
							});
						}
					},
					__('Actions'),
				);
			}
		});
	},
});
