// Load employees from ƏMAS (e-social.gov.az) directly into ERPNext Employee
// Pattern: same as e-taxes import — button on list → API call → selection → create

frappe.ui.form.on('Employee', {
    refresh: function(frm) {
        // Add "View AMAS Contract" button if employee was imported from AMAS
        if (frm.doc.custom_amas_employee) {
            frm.add_custom_button(__('View AMAS Contract'), function() {
                frappe.set_route('Form', 'Amas Employees', frm.doc.custom_amas_employee);
            }, __('AMAS'));

            // Load and display AMAS data dynamically
            load_amas_data(frm);
        }
    }
});

// Load AMAS data from linked record and populate HTML fields
function load_amas_data(frm) {
    if (!frm.doc.custom_amas_employee) return;

    frappe.call({
        method: 'frappe.client.get',
        args: {
            doctype: 'Amas Employees',
            name: frm.doc.custom_amas_employee
        },
        callback: function(r) {
            if (r.message) {
                const amas_data = r.message;
                populate_amas_contract_tab(amas_data);
                populate_amas_position_tab(amas_data);
                populate_amas_salary_tab(amas_data);
                populate_amas_schedule_tab(amas_data);
                populate_amas_personal_tab(amas_data);
                populate_amas_documents_tab(amas_data);
                populate_amas_integration_tab(amas_data);
            }
        }
    });
}

// Helper function to create a field row
function field_row(label, value) {
    if (!value) return '';
    return `<tr><td style="font-weight: 600; width: 40%; padding: 8px;">${label}</td><td style="padding: 8px;">${frappe.utils.escape_html(value)}</td></tr>`;
}

// Populate Contract tab
function populate_amas_contract_tab(data) {
    let html = '<table class="table table-bordered" style="margin-top: 15px;">';
    html += '<thead><tr><th colspan="2" style="background-color: #f5f5f5;">Contract Information</th></tr></thead><tbody>';
    html += field_row('Document No', data.doc_no);
    html += field_row('Document Date', data.doc_date);
    html += field_row('Document Type', data.doc_type);
    html += field_row('Contract Type', data.contract_type);
    html += field_row('Contract Status', data.contract_status);
    html += field_row('Contract Number', data.contract_number);
    html += field_row('Is Signed', data.is_signed ? 'Yes' : 'No');
    html += field_row('Contract Validity', data.contract_validity);
    html += '</tbody></table>';

    html += '<table class="table table-bordered" style="margin-top: 15px;">';
    html += '<thead><tr><th colspan="2" style="background-color: #f5f5f5;">Contract Dates</th></tr></thead><tbody>';
    html += field_row('Begin Date', data.begin_date);
    html += field_row('End Date', data.end_date);
    html += field_row('Main Contract Begin Date', data.main_contract_begin_date);
    html += field_row('Contract Begin Date', data.contract_begin_date);
    html += field_row('Contract End Date', data.contract_end_date);
    html += '</tbody></table>';

    html += '<table class="table table-bordered" style="margin-top: 15px;">';
    html += '<thead><tr><th colspan="2" style="background-color: #f5f5f5;">Contract Details</th></tr></thead><tbody>';
    html += field_row('Previous Contract', data.prev_contract);
    html += field_row('Contract Reason', data.contract_reason);
    html += field_row('From Migration', data.from_migration ? 'Yes' : 'No');
    html += field_row('Staff OID', data.staff_oid);
    html += field_row('Structure Unit OID', data.structure_unit_oid);
    html += field_row('Entity OID', data.entity_oid);
    html += field_row('Entity Details OID', data.entity_details_oid);
    html += '</tbody></table>';

    $('#amas_contract_data').html(html);
}

// Populate Position tab
function populate_amas_position_tab(data) {
    let html = '<table class="table table-bordered" style="margin-top: 15px;">';
    html += '<thead><tr><th colspan="2" style="background-color: #f5f5f5;">Position</th></tr></thead><tbody>';
    html += field_row('Work Position', data.work_position);
    html += field_row('Position ID', data.position_id);
    html += field_row('Position Text', data.position_text);
    html += field_row('Activity Code', data.activity_code);
    html += field_row('Activity Name', data.activity_name);
    html += field_row('Staff Unit', data.staff_unit);
    html += field_row('Structure Path', data.structure_path);
    html += field_row('Workplace Sector', data.workplace_sector);
    html += '</tbody></table>';

    html += '<table class="table table-bordered" style="margin-top: 15px;">';
    html += '<thead><tr><th colspan="2" style="background-color: #f5f5f5;">Work Mode</th></tr></thead><tbody>';
    html += field_row('Workplace Type', data.workplace_type);
    html += field_row('Form Size', data.form_size);
    html += field_row('Flow', data.flow);
    html += field_row('Flow Alias', data.flow_alias);
    html += field_row('View Type', data.view_type);
    html += '</tbody></table>';

    $('#amas_position_data').html(html);
}

// Populate Salary tab
function populate_amas_salary_tab(data) {
    let html = '<table class="table table-bordered" style="margin-top: 15px;">';
    html += '<thead><tr><th colspan="2" style="background-color: #f5f5f5;">Base Salary</th></tr></thead><tbody>';
    html += field_row('Base Salary', data.base_salary);
    html += field_row('Monthly Salary', data.monthly_salary);
    html += field_row('Salary Add', data.salary_add);
    html += field_row('Salary Add HW', data.salary_add_hw);
    html += field_row('Currency Type', data.currency_type);
    html += field_row('Payment Type', data.payment_type);
    html += field_row('Payment First Day', data.payment_first_day);
    html += field_row('Bank OID', data.bank_oid);
    html += '</tbody></table>';

    html += '<table class="table table-bordered" style="margin-top: 15px;">';
    html += '<thead><tr><th colspan="2" style="background-color: #f5f5f5;">Additional Payments</th></tr></thead><tbody>';
    html += field_row('Award Amount', data.award_amount);
    html += field_row('Award Type', data.award_type);
    html += field_row('Overtime Amount', data.overtime_amount);
    html += field_row('Bad Condition Amount', data.bad_condition_amount);
    html += field_row('Bad Condition ID', data.bad_condition_id);
    html += field_row('Qualification Degree Salary Add', data.qualification_degree_salary_add);
    html += '</tbody></table>';

    $('#amas_salary_data').html(html);
}

// Populate Schedule tab
function populate_amas_schedule_tab(data) {
    let html = '<table class="table table-bordered" style="margin-top: 15px;">';
    html += '<thead><tr><th colspan="2" style="background-color: #f5f5f5;">Work Schedule</th></tr></thead><tbody>';
    html += field_row('Work Mode Type', data.work_mode_type);
    html += field_row('Is Full Time', data.is_full_time);
    html += field_row('Work Time', data.work_time);
    html += field_row('Working Time Cumulative', data.working_time_cumulative ? 'Yes' : 'No');
    html += field_row('First Shift Start', data.first_shift_start);
    html += field_row('First Shift End', data.first_shift_end);
    html += field_row('Second Shift Start', data.second_shift_start);
    html += field_row('Second Shift End', data.second_shift_end);
    html += field_row('Lunch Start', data.lunch_start);
    html += field_row('Lunch End', data.lunch_end);
    html += '</tbody></table>';

    html += '<table class="table table-bordered" style="margin-top: 15px;">';
    html += '<thead><tr><th colspan="2" style="background-color: #f5f5f5;">Vacation</th></tr></thead><tbody>';
    html += field_row('Vacation Days Count', data.vacation_days_count);
    html += field_row('Vacation General Duration', data.vacation_general_duration);
    html += field_row('Vacation Main Duration', data.vacation_main_duration);
    html += field_row('Vacation Add Duration 1', data.vacation_add_dur_one);
    html += field_row('Vacation Add Duration 2', data.vacation_add_dur_two);
    html += field_row('Vacation Add Duration 4', data.vacation_add_dur_four);
    html += field_row('Vacation Amount', data.vacation_amount);
    html += field_row('Profession Employee For Vacation', data.profession_employee_for_vacation);
    html += '</tbody></table>';

    $('#amas_schedule_data').html(html);
}

// Populate Personal tab
function populate_amas_personal_tab(data) {
    let html = '<table class="table table-bordered" style="margin-top: 15px;">';
    html += '<thead><tr><th colspan="2" style="background-color: #f5f5f5;">Personal Information</th></tr></thead><tbody>';
    html += field_row('FIN', data.identification_number);
    html += field_row('SSN', data.ssn);
    html += field_row('Father Name', data.father_name);
    html += field_row('Birthday', data.birthday);
    html += field_row('Gender', data.gender);
    html += field_row('Nationality', data.nationality);
    html += field_row('Blood Type', data.blood_type);
    html += field_row('Marital Status', data.marital_status);
    html += field_row('Military Status', data.military_status);
    html += '</tbody></table>';

    html += '<table class="table table-bordered" style="margin-top: 15px;">';
    html += '<thead><tr><th colspan="2" style="background-color: #f5f5f5;">Address</th></tr></thead><tbody>';
    html += field_row('Personal Address', data.personal_address);
    html += field_row('Personal City', data.personal_city);
    html += field_row('Personal District', data.personal_district);
    html += field_row('Work Address OID', data.work_address_oid);
    html += field_row('Work Address Street', data.work_address_street);
    html += field_row('Work Address District', data.work_address_district);
    html += '</tbody></table>';

    html += '<table class="table table-bordered" style="margin-top: 15px;">';
    html += '<thead><tr><th colspan="2" style="background-color: #f5f5f5;">Contact</th></tr></thead><tbody>';
    html += field_row('Contact Phone', data.contact_phone);
    html += field_row('Contact Email', data.contact_email);
    html += '</tbody></table>';

    html += '<table class="table table-bordered" style="margin-top: 15px;">';
    html += '<thead><tr><th colspan="2" style="background-color: #f5f5f5;">Education</th></tr></thead><tbody>';
    html += field_row('Education', data.education);
    html += field_row('Scientific Degree', data.scientific_degree);
    html += field_row('Speciality Degree', data.speciality_degree);
    html += field_row('State Speciality Degree', data.state_speciality_degree);
    html += '</tbody></table>';

    $('#amas_personal_data').html(html);
}

// Populate Documents tab
function populate_amas_documents_tab(data) {
    let html = '<table class="table table-bordered" style="margin-top: 15px;">';
    html += '<thead><tr><th colspan="2" style="background-color: #f5f5f5;">Document Information</th></tr></thead><tbody>';
    html += field_row('Document Number', data.document_number);
    html += field_row('Document Issue Date', data.document_issue_date);
    html += field_row('Document Expiry Date', data.document_expiry_date);
    html += field_row('Document Issued By', data.document_issued_by);
    html += '</tbody></table>';

    html += '<table class="table table-bordered" style="margin-top: 15px;">';
    html += '<thead><tr><th colspan="2" style="background-color: #f5f5f5;">Employer Information</th></tr></thead><tbody>';
    html += field_row('Employer PIN', data.employer_pin);
    html += field_row('Employer TIN', data.employer_tin);
    html += field_row('Chairman PIN', data.chairman_pin);
    html += '</tbody></table>';

    html += '<table class="table table-bordered" style="margin-top: 15px;">';
    html += '<thead><tr><th colspan="2" style="background-color: #f5f5f5;">Additional Metadata</th></tr></thead><tbody>';
    html += field_row('Document State', data.doc_state);
    html += field_row('Created Date', data.created_date);
    html += field_row('Labour Function', data.labour_function);
    html += field_row('Activity Name Detail', data.activity_name_detail);
    html += field_row('Protection Tools', data.protection_tools);
    html += field_row('Payment Other Conditions', data.payment_other_cond);
    html += field_row('Rule Text', data.rule_text);
    html += '</tbody></table>';

    html += '<table class="table table-bordered" style="margin-top: 15px;">';
    html += '<thead><tr><th colspan="2" style="background-color: #f5f5f5;">System Fields</th></tr></thead><tbody>';
    html += field_row('Job Start Date', data.emp_job_start_date);
    html += field_row('Sign Type', data.sign_type);
    html += field_row('Is Valid', data.is_valid);
    html += field_row('Duration State', data.dur_state);
    html += field_row('Operation', data.operation);
    html += field_row('After Execution Label', data.after_exec_label);
    html += field_row('Tree Path Name', data.tree_path_name);
    html += field_row('Document OID', data.doc_oid);
    html += '</tbody></table>';

    $('#amas_documents_data').html(html);
}

// Populate Integration tab
function populate_amas_integration_tab(data) {
    let html = '<table class="table table-bordered" style="margin-top: 15px;">';
    html += '<thead><tr><th colspan="2" style="background-color: #f5f5f5;">Integration Information</th></tr></thead><tbody>';
    html += field_row('AMAS Employee Record', data.name);
    html += field_row('Import Date', data.import_date);
    html += field_row('Organization Name', data.organization_name);
    html += field_row('Organization VOEN', data.organization_voen);
    html += field_row('Asan Login', data.asan_login);
    html += field_row('Employee', data.employee);
    html += '</tbody></table>';

    html += '<div style="margin-top: 15px; padding: 15px; background-color: #f8f9fa; border-radius: 4px;">';
    html += '<p style="margin-bottom: 10px;"><strong>Actions:</strong></p>';
    html += '<button class="btn btn-primary btn-sm" onclick="frappe.set_route(\'Form\', \'Amas Employees\', \'' + data.name + '\');">';
    html += '<i class="fa fa-external-link"></i> View Full AMAS Record</button>';
    html += '</div>';

    $('#amas_integration_data').html(html);
}

// List view — add "Load from ƏMAS" button
if (frappe.listview_settings['Employee']) {
    const orig_onload = frappe.listview_settings['Employee'].onload;
    frappe.listview_settings['Employee'].onload = function(listview) {
        if (orig_onload) orig_onload(listview);
        add_amas_load_button(listview);
    };
} else {
    frappe.listview_settings['Employee'] = {
        onload: function(listview) {
            add_amas_load_button(listview);
        }
    };
}

function add_amas_load_button(listview) {
    listview.page.add_inner_button(__('Load from ƏMAS'), function() {
        start_amas_employee_import(listview);
    });
}

// Step 1: Get default connected Asan Login → fetch employees from ƏMAS API
function start_amas_employee_import(listview) {
    frappe.call({
        method: 'invoice_az.amas_api.get_connected_asan_logins',
        freeze: true,
        freeze_message: __('Checking ƏMAS connection...'),
        callback: function(r) {
            if (!r.message || !r.message.success) {
                frappe.msgprint({
                    title: __('Error'),
                    indicator: 'red',
                    message: r.message ? r.message.message : __('Failed to check ƏMAS connections')
                });
                return;
            }

            const asan_logins = r.message.asan_logins || [];

            if (asan_logins.length === 0) {
                // No connected ƏMAS - start connection process automatically
                start_amas_connection_for_import(listview);
                return;
            }

            // Use the first (default) connected login
            fetch_employees_from_amas(listview, asan_logins[0].name, asan_logins[0].organization_name);
        }
    });
}

// Start ƏMAS connection process automatically for import
function start_amas_connection_for_import(listview) {
    // Get first available Asan Login
    frappe.call({
        method: 'frappe.client.get_list',
        args: {
            doctype: 'Asan Login',
            fields: ['name', 'phone', 'user_id'],
            limit_page_length: 1
        },
        callback: function(r) {
            if (!r.message || r.message.length === 0) {
                frappe.msgprint({
                    title: __('No Asan Login Found'),
                    indicator: 'red',
                    message: __('Please create an Asan Login first.')
                });
                return;
            }

            const asan_login = r.message[0];

            // Show confirmation dialog (same design as e-taxes)
            const d = new frappe.ui.Dialog({
                title: __('ƏMAS Connection Required'),
                fields: [
                    {
                        fieldname: 'message',
                        fieldtype: 'HTML',
                        options: `
                            <div class="text-center">
                                <p>ƏMAS connection is required to import employees.</p>
                                <p>Would you like to connect now?</p>
                            </div>
                        `
                    }
                ],
                primary_action_label: __('Connect'),
                primary_action: function() {
                    d.hide();

                    // Start full ƏMAS connection process (auto-select first organization)
                    start_mygovid_then_amas_for_employees(asan_login.name, function() {
                        // After successful connection, restart import
                        start_amas_employee_import(listview);
                    }, false);
                },
                secondary_action_label: __('Cancel')
            });

            d.show();
        }
    });
}

// Step 2: Fetch employees from ƏMAS API
function fetch_employees_from_amas(listview, asan_login_name, organization_name) {
    frappe.show_alert({
        message: __('Fetching employees from ƏMAS...'),
        indicator: 'blue'
    }, 3);

    frappe.call({
        method: 'invoice_az.amas_api.get_employees_report',
        args: {
            asan_login_name: asan_login_name,
            offset: 0,
            limit: 1000
        },
        freeze: true,
        freeze_message: __('Loading employees from') + ' ' + organization_name + '...',
        callback: function(r) {
            if (!r.message || !r.message.success) {
                // Check for CSRF error - need to reconnect
                if (r.message && r.message.csrf_error) {
                    show_amas_reconnect_dialog(asan_login_name, function() {
                        // Retry after successful reconnection
                        fetch_employees_from_amas(listview, asan_login_name, organization_name);
                    });
                    return;
                }

                frappe.msgprint({
                    title: __('Error'),
                    indicator: 'red',
                    message: r.message ? r.message.message : __('Failed to load employees from ƏMAS')
                });
                return;
            }

            const employees = r.message.employees || [];

            if (employees.length === 0) {
                frappe.msgprint({
                    title: __('Information'),
                    indicator: 'blue',
                    message: __('No employees found in ƏMAS for this organization.')
                });
                return;
            }

            frappe.show_alert({
                message: __('Processing ') + employees.length + __(' employees...'),
                indicator: 'blue'
            }, 3);

            check_existing_and_show(listview, asan_login_name, organization_name, employees);
        }
    });
}

// Step 3: Check existing employees and show selection dialog
function check_existing_and_show(listview, asan_login_name, organization_name, employees) {
    const fins = employees
        .map(e => e.identification_number)
        .filter(f => f);

    frappe.call({
        method: 'frappe.client.get_list',
        args: {
            doctype: 'Employee',
            filters: { passport_number: ['in', fins] },
            fields: ['name', 'passport_number', 'employee_name'],
            limit_page_length: 0
        },
        callback: function(r) {
            const existing_map = {};
            if (r.message) {
                r.message.forEach(function(emp) {
                    if (emp.passport_number) {
                        existing_map[emp.passport_number] = emp.name;
                    }
                });
            }

            show_employee_selection(listview, asan_login_name, organization_name, employees, existing_map);
        }
    });
}

// Step 4: Show selection dialog (e-taxes style)
function show_employee_selection(listview, asan_login_name, organization_name, employees, existing_map) {
    const new_count = employees.filter(e => !existing_map[e.identification_number]).length;
    const existing_count = employees.length - new_count;

    // Build table (e-taxes style)
    let invoiceTable = '<div style="max-height: 500px; overflow-y: auto;">' +
        '<table class="table table-bordered amas-employees-table" style="width: 100%; table-layout: fixed;">' +
        '<thead><tr>' +
        '<th style="width: 4%;"><input type="checkbox" class="select-all-employees"></th>' +
        '<th style="width: 20%;">' + __('Full Name') + '</th>' +
        '<th style="width: 10%;">' + __('FIN') + '</th>' +
        '<th style="width: 20%;">' + __('Position') + '</th>' +
        '<th style="width: 8%;">' + __('Salary') + '</th>' +
        '<th style="width: 12%;">' + __('Status') + '</th>' +
        '<th style="width: 10%;">' + __('Begin Date') + '</th>' +
        '<th style="width: 16%;">' + __('In System') + '</th>' +
        '</tr></thead><tbody>';

    employees.forEach(function(emp, idx) {
        const fin = emp.identification_number || '';
        const existing_id = existing_map[fin];
        const status_cell = existing_id
            ? '<span class="indicator-pill green" style="font-size: 11px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; display: inline-block;">' + frappe.utils.escape_html(existing_id) + '</span>'
            : '';

        invoiceTable += '<tr>' +
            '<td><input type="checkbox" class="select-employee" data-index="' + idx + '"></td>' +
            '<td style="word-break: break-word;">' + frappe.utils.escape_html(emp.full_name || '') + '</td>' +
            '<td>' + frappe.utils.escape_html(fin) + '</td>' +
            '<td style="word-break: break-word;">' + frappe.utils.escape_html(emp.work_position_text || '') + '</td>' +
            '<td style="text-align: right;">' + frappe.utils.escape_html(emp.monthly_salary || '') + '</td>' +
            '<td>' + frappe.utils.escape_html(emp.contract_status || '') + '</td>' +
            '<td>' + frappe.utils.escape_html(emp.begin_date || '') + '</td>' +
            '<td>' + status_cell + '</td>' +
            '</tr>';
    });

    invoiceTable += '</tbody></table></div>';

    const infoMessage = '<div class="alert alert-info">' +
        __('Employees found: ') + employees.length +
        ' (' + __('New') + ': ' + new_count + ', ' + __('Already in system') + ': ' + existing_count + ')' +
        '</div>';

    const default_company = frappe.defaults.get_user_default('Company') || frappe.defaults.get_global_default('company');

    const d = new frappe.ui.Dialog({
        title: __('Select employees from ƏMAS'),
        size: 'large',
        fields: [
            {
                fieldname: 'settings_section',
                fieldtype: 'Section Break',
                label: __('Settings')
            },
            {
                fieldname: 'create_designation',
                fieldtype: 'Check',
                label: __('Create Designation if not found'),
                default: 1
            },
            {
                fieldname: 'employees_section',
                fieldtype: 'Section Break',
                label: __('Employees')
            },
            {
                fieldname: 'info_html',
                fieldtype: 'HTML',
                options: infoMessage
            },
            {
                fieldname: 'employees_html',
                fieldtype: 'HTML',
                options: invoiceTable
            }
        ],
        primary_action_label: __('Load selected'),
        primary_action: function(values) {
            const selected_employees = [];
            d.$wrapper.find('.select-employee:checked').each(function() {
                const idx = $(this).data('index');
                selected_employees.push(employees[idx]);
            });

            if (selected_employees.length === 0) {
                frappe.msgprint({
                    title: __('Warning'),
                    indicator: 'orange',
                    message: __('No employees selected')
                });
                return;
            }

            d.hide();
            create_employees(listview, asan_login_name, selected_employees, default_company, values.create_designation);
        },
        secondary_action_label: __('Cancel'),
        secondary_action: function() { d.hide(); }
    });

    // Match e-taxes dialog sizing
    d.$wrapper.find('.modal-dialog').css({
        'max-width': '80%',
        'width': '80%',
        'margin': '30px auto'
    });

    d.$wrapper.find('.modal-body').css({
        'padding': '15px'
    });

    d.show();

    // Select all checkbox
    d.$wrapper.find('.select-all-employees').on('change', function() {
        const isChecked = $(this).prop('checked');
        d.$wrapper.find('.select-employee').prop('checked', isChecked);
    });
}

// Step 5: Create employees via backend - Socket.IO bulk import
function create_employees(listview, asan_login_name, selected_employees, company, create_designation) {
    const total = selected_employees.length;

    frappe.show_progress(
        __('Loading from ƏMAS'), 0, total,
        __('Starting import of {0} employees...', [total])
    );

    frappe.realtime.off('amas_import_progress');
    frappe.realtime.on('amas_import_progress', function(data) {
        let detail = __('Processing employee {0} of {1}', [data.current, data.total]);
        if (data.employee_name) {
            detail = __('Processing: ') + data.employee_name;
        }
        frappe.show_progress(__('Loading from ƏMAS'), data.current, data.total, detail);
    });

    frappe.realtime.off('amas_import_complete');
    frappe.realtime.on('amas_import_complete', function(data) {
        frappe.realtime.off('amas_import_progress');
        frappe.realtime.off('amas_import_complete');
        frappe.hide_progress();

        const error_messages = (data.errors || []).map(function(err) {
            return (err.employee || 'Unknown') + ': ' + (err.error || 'Unknown error');
        });

        const total_processed = data.created + data.updated + (data.errors || []).length;
        show_import_summary(total_processed, data.created, data.updated, (data.errors || []).length, error_messages);

        listview.refresh();
    });

    frappe.call({
        method: 'invoice_az.amas_api.import_bulk_employees',
        args: {
            asan_login_name: asan_login_name,
            employees_data: JSON.stringify(selected_employees),
            company: company,
            create_designation: create_designation ? 1 : 0
        },
        callback: function(r) {
            if (!r.message || !r.message.enqueued) {
                frappe.realtime.off('amas_import_progress');
                frappe.realtime.off('amas_import_complete');
                frappe.hide_progress();
                frappe.msgprint({
                    title: __('Error'),
                    indicator: 'red',
                    message: __('Failed to start import job')
                });
            }
        },
        error: function() {
            frappe.realtime.off('amas_import_progress');
            frappe.realtime.off('amas_import_complete');
            frappe.hide_progress();
            frappe.msgprint({
                title: __('Error'),
                indicator: 'red',
                message: __('Network error starting import')
            });
        }
    });
}

// Process employees one by one (like E-Taxes loadSelectedInvoices)
function process_employees_one_by_one(listview, asan_login_name, employees_queue, company, create_designation,
                                     created_count, updated_count, error_count, errors, current_index) {
    // Check if finished
    if (employees_queue.length === 0) {
        frappe.hide_progress();
        $(document).off('progress-cancel.loading_amas_employees');

        const total = created_count + updated_count + error_count;
        show_import_summary(total, created_count, updated_count, error_count, errors);

        window.amas_employees_to_process = null;
        listview.refresh();
        return;
    }

    // Check if cancelled
    if (window.amas_cancel_loading) {
        employees_queue = [];
        frappe.hide_progress();
        $(document).off('progress-cancel.loading_amas_employees');

        const total = created_count + updated_count + error_count;
        frappe.show_alert({
            message: __('Import cancelled. Processed ') + total + __(' employees.'),
            indicator: 'orange'
        }, 5);

        listview.refresh();
        return;
    }

    // Take first employee from queue
    const emp_data = employees_queue.shift();
    const is_last = employees_queue.length === 0;
    const total_to_process = window.amas_employees_to_process || (created_count + updated_count + error_count + employees_queue.length + 1);

    current_index++;

    // Show progress
    frappe.show_progress(
        __('Loading from ƏMAS'),
        current_index - 1,
        total_to_process,
        __('Processing employee ') + current_index + __(' of ') + total_to_process,
        null,
        true
    );

    // Setup cancel handler
    $(document).off('progress-cancel.loading_amas_employees');
    $(document).on('progress-cancel.loading_amas_employees', function() {
        window.amas_cancel_loading = true;
        frappe.show_alert({
            message: __('Cancelling import... Finishing current employee.'),
            indicator: 'orange'
        }, 3);
        frappe.hide_progress();
    });

    // Get employee name for progress message
    const emp_name = emp_data.full_name || emp_data.identification_number || 'Employee';

    // Update progress with employee name
    frappe.show_progress(
        __('Loading from ƏMAS'),
        current_index - 1,
        total_to_process,
        __('Processing: ') + emp_name
    );

    // Create/update single employee
    frappe.call({
        method: 'invoice_az.amas_api.create_single_employee_from_amas',
        args: {
            asan_login_name: asan_login_name,
            emp_data: emp_data,
            company: company,
            create_designation: create_designation ? 1 : 0
        },
        callback: function(r) {
            if (r.message && r.message.success) {
                // Success - increment counters
                if (r.message.action === 'created') {
                    created_count++;
                    frappe.show_alert({
                        message: __('Created: ') + r.message.full_name,
                        indicator: 'green'
                    }, 2);
                } else if (r.message.action === 'updated') {
                    updated_count++;
                    frappe.show_alert({
                        message: __('Updated: ') + r.message.full_name,
                        indicator: 'blue'
                    }, 2);
                }
            } else {
                // Error
                error_count++;
                const error_msg = r.message ? r.message.message : 'Unknown error';
                errors.push(error_msg);

                frappe.show_alert({
                    message: __('Error: ') + (r.message ? r.message.full_name : emp_name),
                    indicator: 'red'
                }, 3);
            }

            // Continue with next employee after short delay (like E-Taxes)
            setTimeout(function() {
                process_employees_one_by_one(
                    listview,
                    asan_login_name,
                    employees_queue,
                    company,
                    create_designation,
                    created_count,
                    updated_count,
                    error_count,
                    errors,
                    current_index
                );
            }, 50);  // 50ms delay like E-Taxes PROGRESS_UPDATE_DELAY
        },
        error: function(xhr, status, error) {
            // Network error
            error_count++;
            const error_msg = emp_name + ': Network error - ' + (error || 'Unknown error');
            errors.push(error_msg);

            frappe.show_alert({
                message: __('Network error: ') + emp_name,
                indicator: 'red'
            }, 3);

            // Continue with next employee
            setTimeout(function() {
                process_employees_one_by_one(
                    listview,
                    asan_login_name,
                    employees_queue,
                    company,
                    create_designation,
                    created_count,
                    updated_count,
                    error_count,
                    errors,
                    current_index
                );
            }, 50);
        }
    });
}

// Show final summary (like E-Taxes)
function show_import_summary(total, created_count, updated_count, error_count, errors) {
    if (error_count === 0) {
        let msg = __('All ') + total + __(' employees were processed successfully.');
        if (created_count > 0) {
            msg += '<br>' + __('Created: ') + '<strong>' + created_count + '</strong>';
        }
        if (updated_count > 0) {
            msg += '<br>' + __('Updated: ') + '<strong>' + updated_count + '</strong>';
        }

        frappe.msgprint({
            title: __('Import Completed Successfully'),
            indicator: 'green',
            message: msg
        });
    } else {
        let msg = '<div style="margin-bottom: 15px;">';
        if (created_count > 0) {
            msg += __('Successfully created: ') + '<strong>' + created_count + '</strong><br>';
        }
        if (updated_count > 0) {
            msg += __('Updated: ') + '<strong>' + updated_count + '</strong><br>';
        }
        msg += __('Failed: ') + '<strong>' + error_count + '</strong>';
        msg += '</div>';

        if (errors && errors.length > 0) {
            msg += '<div style="max-height: 200px; overflow-y: auto;">';
            msg += '<ul style="color: red; font-size: 12px; margin: 0; padding-left: 20px;">';
            errors.slice(0, 10).forEach(function(err) {
                msg += '<li>' + frappe.utils.escape_html(err) + '</li>';
            });
            if (errors.length > 10) {
                msg += '<li><em>... and ' + (errors.length - 10) + ' more errors</em></li>';
            }
            msg += '</ul></div>';
        }

        frappe.msgprint({
            title: created_count > 0 ? __('Import Completed with Errors') : __('Import Failed'),
            indicator: created_count > 0 ? 'orange' : 'red',
            message: msg
        });
    }
}

// Show reconnection dialog for ƏMAS when token expires (CSRF error)
function show_amas_reconnect_dialog(asan_login_name, retry_callback) {
    const d = new frappe.ui.Dialog({
        title: __('ƏMAS Authentication Required'),
        fields: [
            {
                fieldname: 'message',
                fieldtype: 'HTML',
                options: `
                    <div class="text-center" style="padding: 20px;">
                        <p style="font-size: 16px; margin-bottom: 15px;">
                            <i class="fa fa-exclamation-triangle" style="color: #f39c12; font-size: 48px;"></i>
                        </p>
                        <p style="font-size: 14px;">Your ƏMAS session has expired or authentication is required.</p>
                        <p style="font-size: 14px; margin-bottom: 0;">Would you like to reconnect to ƏMAS?</p>
                    </div>
                `
            }
        ],
        primary_action_label: __('Reconnect'),
        primary_action: function() {
            d.hide();

            // Try to reconnect to ƏMAS
            frappe.call({
                method: 'invoice_az.amas_api.connect_amas',
                args: {
                    'asan_login_name': asan_login_name
                },
                freeze: true,
                freeze_message: __('Reconnecting to ƏMAS...'),
                callback: function(r) {
                    if (r.message && r.message.success) {
                        // Check if we need to select organization
                        const accounts = r.message.accounts || [];

                        if (accounts.length > 0) {
                            // Show organization selection dialog
                            show_amas_accounts_dialog(asan_login_name, accounts, retry_callback);
                        } else {
                            // No accounts to select, just proceed
                            frappe.show_alert({
                                message: __('ƏMAS reconnected successfully!'),
                                indicator: 'green'
                            }, 3);

                            // Retry the original operation
                            if (typeof retry_callback === 'function') {
                                setTimeout(retry_callback, 500);
                            }
                        }
                    } else if (r.message && r.message.need_mygovid_login) {
                        // MyGovID token expired - start full authentication flow with organization selection
                        start_mygovid_then_amas_for_employees(asan_login_name, retry_callback, true);
                    } else {
                        frappe.msgprint({
                            title: __('Reconnection Failed'),
                            indicator: 'red',
                            message: r.message ? r.message.message : __('Failed to reconnect to ƏMAS.')
                        });
                    }
                }
            });
        },
        secondary_action_label: __('Cancel')
    });

    d.show();
}

// Start MyGovID authentication followed by ƏMAS connection (for employees)
// show_org_selection: true = show organization selection dialog, false = auto-select first org
function start_mygovid_then_amas_for_employees(asan_login_name, retry_callback, show_org_selection) {
    // Default to false (auto-select) for backward compatibility
    if (show_org_selection === undefined) {
        show_org_selection = false;
    }
    frappe.show_alert({
        message: __('Starting MyGovID authentication...'),
        indicator: 'blue'
    }, 3);

    // Create status dialog
    const status_dialog = new frappe.ui.Dialog({
        title: __('MyGovID Authentication for ƏMAS'),
        fields: [
            {
                fieldname: 'status_html',
                fieldtype: 'HTML',
                options: `
                    <div class="text-center">
                        <div class="mygovid-spinner" style="display: inline-block; width: 64px; height: 64px;">
                            <div style="width: 64px; height: 64px; border: 6px solid #4e73df; border-radius: 50%; border-color: #4e73df transparent #4e73df transparent; animation: mygovid-spin 1.2s linear infinite;"></div>
                        </div>
                        <style>
                            @keyframes mygovid-spin {
                                0% { transform: rotate(0deg); }
                                100% { transform: rotate(360deg); }
                            }
                        </style>
                        <div class="mt-3" id="mygovid_status">
                            <h4>Waiting for confirmation on your phone</h4>
                            <p>Please check your phone and confirm the authentication request in ASAN Sign app.</p>
                        </div>
                        <div id="mygovid_code" style="display:none; margin-top: 15px;">
                            <div style="background: #f8f9fa; padding: 10px; display: inline-block; border-radius: 5px; font-size: 24px; font-weight: bold; letter-spacing: 2px; color: #333; border: 1px solid #ddd;">
                                <span id="mygovid_code_text">----</span>
                            </div>
                            <p style="margin-top: 5px; font-size: 12px; color: #666;">Verification Code</p>
                        </div>
                    </div>
                `
            }
        ],
        primary_action_label: __('Cancel'),
        primary_action: function() {
            window.mygovidStop = true;
            status_dialog.hide();
        }
    });

    status_dialog.show();
    window.mygovidStop = false;

    // Start MyGovID login
    frappe.call({
        method: 'invoice_az.invoice_az.doctype.asan_login.asan_login.start_mygovid_login',
        args: {
            'asan_login_name': asan_login_name
        },
        timeout: 130000,
        callback: function(r) {
            if (window.mygovidStop) return;

            if (r.message && r.message.success) {
                // MyGovID login successful, now connect to ƏMAS
                $('#mygovid_status').html(`
                    <h4 style="color: blue;">Connecting to ƏMAS...</h4>
                    <p>MyGovID authentication successful. Now connecting to ƏMAS...</p>
                `);
                $('#mygovid_code').hide();

                // Connect to ƏMAS
                frappe.call({
                    method: 'invoice_az.amas_api.connect_amas',
                    args: {
                        'asan_login_name': asan_login_name
                    },
                    callback: function(r2) {
                        if (r2.message && r2.message.success) {
                            const accounts = r2.message.accounts || [];

                            if (accounts.length > 0) {
                                // Check if we should show selection dialog or auto-select
                                if (show_org_selection) {
                                    // Show organization selection dialog (for re-authentication)
                                    status_dialog.hide();
                                    show_amas_accounts_dialog(asan_login_name, accounts, retry_callback);
                                } else {
                                    // Auto-select first organization (for initial connection)
                                    const first_account = accounts[0];
                                    const oid = first_account.accountOid || first_account.oid || '';
                                    const name = first_account.accountName || first_account.name || 'Unknown';
                                    const number = first_account.accountNumber || first_account.voen || first_account.number || '';
                                    const role = first_account.roleName || first_account.role || 'chairman';

                                    $('#mygovid_status').html(`
                                        <h4 style="color: blue;">Selecting organization...</h4>
                                        <p>ƏMAS connected. Selecting ${frappe.utils.escape_html(name)}...</p>
                                    `);

                                    // Auto-select first account
                                    frappe.call({
                                        method: 'invoice_az.amas_api.change_amas_account',
                                        args: {
                                            'asan_login_name': asan_login_name,
                                            'account_oid': oid,
                                            'account_name': name,
                                            'account_number': number,
                                            'role_name': role
                                        },
                                        callback: function(r3) {
                                            if (r3.message && r3.message.success) {
                                                $('#mygovid_status').html(`
                                                    <h4 style="color: green;">ƏMAS Connected!</h4>
                                                    <p>Successfully connected to ${frappe.utils.escape_html(name)}.</p>
                                                `);
                                                $('.mygovid-spinner').html(`
                                                    <div style="width: 64px; height: 64px; display: flex; align-items: center; justify-content: center;">
                                                        <i class="fa fa-check" style="font-size: 48px; color: #5cb85c;"></i>
                                                    </div>
                                                `);

                                                setTimeout(function() {
                                                    status_dialog.hide();
                                                    if (typeof retry_callback === 'function') {
                                                        retry_callback();
                                                    }
                                                }, 2000);
                                            } else {
                                                $('#mygovid_status').html(`
                                                    <h4 style="color: red;">Organization Selection Failed</h4>
                                                    <p>${r3.message ? r3.message.message : 'Failed to select organization'}</p>
                                                `);
                                                $('.mygovid-spinner').css('display', 'none');

                                                status_dialog.set_primary_action(__('Close'), function() {
                                                    status_dialog.hide();
                                                });
                                            }
                                        }
                                    });
                                }
                            } else {
                                // No accounts - just complete
                                $('#mygovid_status').html(`
                                    <h4 style="color: green;">ƏMAS Connected!</h4>
                                    <p>Successfully connected to ƏMAS.</p>
                                `);
                                $('.mygovid-spinner').html(`
                                    <div style="width: 64px; height: 64px; display: flex; align-items: center; justify-content: center;">
                                        <i class="fa fa-check" style="font-size: 48px; color: #5cb85c;"></i>
                                    </div>
                                `);

                                status_dialog.set_primary_action(__('Continue'), function() {
                                    status_dialog.hide();
                                    if (typeof retry_callback === 'function') {
                                        setTimeout(retry_callback, 500);
                                    }
                                });

                                setTimeout(function() {
                                    status_dialog.hide();
                                    if (typeof retry_callback === 'function') {
                                        retry_callback();
                                    }
                                }, 2000);
                            }
                        } else {
                            $('#mygovid_status').html(`
                                <h4 style="color: red;">ƏMAS Connection Failed</h4>
                                <p>${r2.message ? r2.message.message : 'Unknown error'}</p>
                            `);
                            $('.mygovid-spinner').css('display', 'none');

                            status_dialog.set_primary_action(__('Close'), function() {
                                status_dialog.hide();
                            });
                        }
                    }
                });
            } else {
                // MyGovID login failed
                $('#mygovid_status').html(`
                    <h4 style="color: red;">Authentication Failed</h4>
                    <p>${r.message ? r.message.message : 'Unknown error'}</p>
                `);
                $('.mygovid-spinner').css('display', 'none');
                $('#mygovid_code').hide();

                status_dialog.set_primary_action(__('Close'), function() {
                    status_dialog.hide();
                });
            }
        },
        error: function() {
            if (window.mygovidStop) return;

            $('#mygovid_status').html(`
                <h4 style="color: red;">Authentication Timeout</h4>
                <p>The request timed out. Please try again.</p>
            `);
            $('.mygovid-spinner').css('display', 'none');
            $('#mygovid_code').hide();

            status_dialog.set_primary_action(__('Close'), function() {
                status_dialog.hide();
            });
        }
    });

    // Get verification code
    setTimeout(function() {
        frappe.call({
            method: 'invoice_az.invoice_az.doctype.asan_login.asan_login.get_mygovid_verification_code',
            args: { 'asan_login_name': asan_login_name },
            callback: function(r) {
                if (r.message && r.message.success) {
                    $('#mygovid_code_text').text(r.message.code);
                    $('#mygovid_code').show();
                }
            }
        });
    }, 500);
}

// Show ƏMAS accounts selection dialog
function show_amas_accounts_dialog(asan_login_name, accounts, retry_callback) {
    if (!accounts || accounts.length === 0) {
        frappe.msgprint(__('No organizations found in ƏMAS'));
        return;
    }

    // Build accounts table
    let table_html = '<div style="max-height: 400px; overflow-y: auto;">';
    table_html += '<table class="table table-bordered table-hover">';
    table_html += '<thead><tr><th style="width: 50%;">Organization Name</th><th style="width: 25%;">VOEN</th><th style="width: 15%;">Role</th><th style="width: 10%;">Action</th></tr></thead>';
    table_html += '<tbody>';

    accounts.forEach(function(account, index) {
        const name = account.accountName || account.name || 'Unknown';
        const voen = account.accountNumber || account.voen || account.number || '';
        const role = account.roleName || account.role || '';
        const oid = account.accountOid || account.oid || '';

        table_html += `<tr>
            <td>${frappe.utils.escape_html(name)}</td>
            <td>${frappe.utils.escape_html(voen)}</td>
            <td>${frappe.utils.escape_html(role)}</td>
            <td>
                <button class="btn btn-xs btn-primary select-amas-account"
                        data-oid="${frappe.utils.escape_html(oid)}"
                        data-name="${frappe.utils.escape_html(name)}"
                        data-number="${frappe.utils.escape_html(voen)}"
                        data-role="${frappe.utils.escape_html(role)}">
                    Select
                </button>
            </td>
        </tr>`;
    });

    table_html += '</tbody></table></div>';

    const d = new frappe.ui.Dialog({
        title: __('Select ƏMAS Organization'),
        fields: [
            {
                fieldname: 'info',
                fieldtype: 'HTML',
                options: '<div class="alert alert-info" style="margin-bottom: 15px;"><strong>Note:</strong> Please select the organization you want to work with.</div>'
            },
            {
                fieldname: 'accounts_html',
                fieldtype: 'HTML',
                options: table_html
            }
        ],
        primary_action_label: __('Cancel'),
        primary_action: function() {
            d.hide();
        }
    });

    // Make dialog wider
    d.$wrapper.find('.modal-dialog').css({
        'max-width': '70%',
        'width': '70%'
    });

    d.show();

    // Add click handler for account selection
    d.$wrapper.find('.select-amas-account').on('click', function() {
        const oid = $(this).data('oid');
        const name = $(this).data('name');
        const number = $(this).data('number');
        const role = $(this).data('role');

        // Disable all buttons
        d.$wrapper.find('.select-amas-account').prop('disabled', true);
        $(this).html('<i class="fa fa-spinner fa-spin"></i> Selecting...');

        // Call backend to select account
        frappe.call({
            method: 'invoice_az.amas_api.change_amas_account',
            args: {
                'asan_login_name': asan_login_name,
                'account_oid': oid,
                'account_name': name,
                'account_number': number,
                'role_name': role || 'chairman'
            },
            callback: function(r) {
                if (r.message && r.message.success) {
                    d.hide();
                    frappe.show_alert({
                        message: __('Organization selected: ') + name,
                        indicator: 'green'
                    }, 3);

                    // Retry the original operation after short delay
                    if (typeof retry_callback === 'function') {
                        setTimeout(retry_callback, 500);
                    }
                } else {
                    // Re-enable buttons on error
                    d.$wrapper.find('.select-amas-account').prop('disabled', false);
                    $(this).html('Select');

                    frappe.msgprint({
                        title: __('Error'),
                        indicator: 'red',
                        message: r.message ? r.message.message : __('Failed to select organization')
                    });
                }
            }
        });
    });
}
