import frappe
from frappe.custom.doctype.custom_field.custom_field import create_custom_field

def create_custom_fields():
    custom_fields = {
        "Payment Entry": [
            dict(
                fieldname='custom_note',
                label='Note',
                fieldtype='Data',
                insert_after='status'
            )
        ],
        "Bank": [
            dict(
                fieldname='bank_code',
                label='Bank Code',
                fieldtype='Data',
                insert_after='swift_number'
            )
        ],
        "Bank Account": [
            dict(
                fieldname='payment_account',
                label='Payment account (h/h)',
                fieldtype='Data',
                insert_after='iban'
            ),
            dict(
                fieldname='сorrespondent_account',
                label='Correspondent account (m/h)',
                fieldtype='Data',
                insert_after='payment_account'
            )
        ],
        "Employee Transfer": [
            dict(
                fieldname='reason',
                label='Reason',
                fieldtype='Data',
                insert_after='amended_from'
            ),
            dict(
                fieldname='order_number',
                label='Order number',
                fieldtype='Data',
                insert_after='reason'
            )
        ],
        "Employee Onboarding": [
            dict(
                fieldname='from_date_temporary',
                label='A temporary employee starts working from',
                fieldtype='Date',
                insert_after='boarding_begins_on'
            ),
            dict(
                fieldname='to_date_temporary',
                label='A temporary employee starts working to',
                fieldtype='Date',
                insert_after='from_date_temporary'
            )
        ],
        "Account": [
            dict(
                fieldname='is_account',
                label='Is Account',
                fieldtype='Check',
                insert_after='is_group'
            ),
            dict(
                fieldname='is_sub_account',
                label='Is Sub Account',
                fieldtype='Check',
                insert_after='is_account'
            ),
            dict(
                fieldname='description',
                label='Description',
                fieldtype='Text',
                insert_after='balance_must_be'
            )            
        ],        
        "Company": [
            dict(
                fieldname='ceo',
                label='Chief Executive Officer',
                fieldtype='Link',
                options='Employee',
                insert_after='default_holiday_list',
                read_only=1
            ),
            dict(
                fieldname='director',
                label='Director',
                fieldtype='Link',
                options='Employee',
                insert_after='ceo'
            ),
            dict(
                fieldname='city_print',
                label='City (for print formats)',
                fieldtype='Data',
                insert_after='director'
            )
        ]                        
    }

    for doctype, fields in custom_fields.items():
        for field in fields:
            # Попытка удалить поле, если оно существует
            if frappe.db.exists("Custom Field", {"dt": doctype, "fieldname": field['fieldname']}):
                try:
                    custom_field_name = frappe.db.get_value("Custom Field", {"dt": doctype, "fieldname": field['fieldname']}, "name")
                    frappe.delete_doc("Custom Field", custom_field_name)
                    frappe.db.commit()
                    print(f"Deleted field {field['fieldname']} in {doctype}")
                except Exception as e:
                    print(f"Failed to delete field {field['fieldname']} in {doctype}: {e}")

            # Создание поля
            create_custom_field(doctype, field)
            print(f"Created field {field['fieldname']} in {doctype}")
