import frappe

ACCOUNT_FIELDS = [
    "default_bank_account",
    "default_cash_account",
    "default_receivable_account",
    "round_off_account",
    "write_off_account",
    "exchange_gain_loss_account",
    "unrealized_exchange_gain_loss_account",
    "default_payable_account",
    "default_expense_account",
    "default_income_account",
    "default_deferred_revenue_account",
    "default_deferred_expense_account",
    "default_inventory_account",
    "stock_adjustment_account",
    "stock_received_but_not_billed",
    "accumulated_depreciation_account",
    "depreciation_expense_account",
    "disposal_account",
    "capital_work_in_progress_account",
    "asset_received_but_not_billed",
    "unrealized_profit_loss_account",
    "default_discount_account",
    "default_provisional_account",
    "default_advance_received_account",
    "default_advance_paid_account",
    "default_operating_cost_account",
    "round_off_for_opening",
    "purchase_expense_account",
    "purchase_expense_contra_account",
    "service_expense_account",
]


@frappe.whitelist()
def export_company_defaults():
    company = frappe.db.get_single_value("Global Defaults", "default_company")
    if not company:
        companies = frappe.get_all("Company", limit=1)
        if companies:
            company = companies[0].name

    if not company:
        frappe.throw("No company found")

    doc = frappe.get_doc("Company", company)
    result = {}

    for fieldname in ACCOUNT_FIELDS:
        full_account = doc.get(fieldname)
        if full_account:
            account_number = frappe.db.get_value("Account", full_account, "account_number")
            if account_number:
                result[fieldname] = account_number

    return result
