// Copyright (c) 2026, Your Company and contributors
// For license information, please see license.txt

frappe.ui.form.on("testapi", {
	refresh: function (frm) {
		// Button to start full ASAN Sign login flow
		frm.add_custom_button(
			__("Start ASAN Login"),
			function () {
				if (!frm.doc.user_id) {
					frappe.msgprint(__("Please fill ASAN ID field"));
					return;
				}
				if (!frm.doc.phone) {
					frappe.msgprint(__("Please fill Phone field"));
					return;
				}

				frm.set_value("status", "Waiting for confirmation...");
				frm.set_value("verification_code", "");
				frm.set_value("response_data", "");

				// Step 1: Start login (this is a long-polling request)
				// It will wait until user confirms in ASAN Sign app
				frappe.call({
					method: "invoice_az.invoice_az.doctype.testapi.testapi.start_asan_sign_login",
					args: {
						user_id: frm.doc.user_id,
						phone_number: frm.doc.phone,
					},
					timeout: 130000, // 130 seconds timeout
					callback: function (r) {
						if (r.message && r.message.success) {
							frm.set_value("status", "Success - Token received");
							frm.set_value(
								"response_data",
								JSON.stringify(r.message.response_data, null, 2)
							);
							frm.save();
							frappe.show_alert({
								message: __("Login successful! Token received."),
								indicator: "green",
							});
						} else {
							frm.set_value("status", "Failed");
							frm.set_value(
								"response_data",
								r.message.response_data || r.message.message
							);
							frappe.msgprint({
								title: __("Login Failed"),
								indicator: "red",
								message: r.message
									? r.message.message
									: __("Failed to complete ASAN Sign login"),
							});
						}
					},
					error: function () {
						frm.set_value("status", "Error/Timeout");
					},
				});

				// Step 2: Get verification code immediately after starting login
				// Small delay to ensure POST request is sent first
				setTimeout(function () {
					frappe.call({
						method: "invoice_az.invoice_az.doctype.testapi.testapi.get_verification_code",
						args: { user_id: frm.doc.user_id },
						callback: function (r) {
							if (r.message && r.message.success) {
								frm.set_value("verification_code", r.message.code);
								frappe.show_alert({
									message: __(
										"Enter this code in ASAN Sign: {0}",
										[r.message.code]
									),
									indicator: "blue",
								});
								// Show dialog with verification code
								frappe.msgprint({
									title: __("Verification Code"),
									indicator: "blue",
									message: __(
										"<h1 style='text-align:center; font-size: 48px;'>{0}</h1><p>Enter this code in your ASAN Sign mobile app to confirm login.</p>",
										[r.message.code]
									),
								});
							} else {
								frappe.show_alert({
									message: __("Could not get verification code"),
									indicator: "red",
								});
							}
						},
					});
				}, 500);
			},
			__("MyGovID")
		);

		// Separate button to just get verification code (for testing)
		frm.add_custom_button(
			__("Get Code Only"),
			function () {
				if (!frm.doc.user_id) {
					frappe.msgprint(__("Please fill ASAN ID field"));
					return;
				}

				frappe.call({
					method: "invoice_az.invoice_az.doctype.testapi.testapi.get_verification_code",
					args: { user_id: frm.doc.user_id },
					freeze: true,
					freeze_message: __("Getting verification code..."),
					callback: function (r) {
						if (r.message && r.message.success) {
							frm.set_value("verification_code", r.message.code);
							frm.set_value("status", "Code received");
							frm.set_value(
								"response_data",
								JSON.stringify(r.message.response_data, null, 2)
							);
							frappe.show_alert({
								message: __("Verification code: {0}", [r.message.code]),
								indicator: "green",
							});
						} else {
							frm.set_value("status", "Failed");
							frappe.msgprint({
								title: __("Error"),
								indicator: "red",
								message: r.message
									? r.message.message
									: __("Failed to get verification code"),
							});
						}
					},
				});
			},
			__("MyGovID")
		);
	},
});
