import frappe

def execute():
    """
    Migrate agricultural fields to purchase_type fields

    This migration renames:
    - agricultural_goods → purchase_type (Purchase Order, Purchase Invoice)
    - purchase_confirmation_doc_type → act_type (Purchase Order, Purchase Invoice)
    - agricultural_tax_amount → purchase_tax_amount (Purchase Invoice Item)
    """

    frappe.logger().info("Starting migration: rename_agricultural_to_purchase_type")

    # Purchase Order - migrate agricultural_goods to purchase_type
    if frappe.db.has_column("Purchase Order", "agricultural_goods"):
        frappe.logger().info("Migrating Purchase Order: agricultural_goods → purchase_type")
        frappe.db.sql("""
            UPDATE `tabPurchase Order`
            SET purchase_type = agricultural_goods
            WHERE agricultural_goods IS NOT NULL
        """)
        frappe.logger().info("Purchase Order: agricultural_goods migration complete")

    # Purchase Order - migrate purchase_confirmation_doc_type to act_type
    if frappe.db.has_column("Purchase Order", "purchase_confirmation_doc_type"):
        frappe.logger().info("Migrating Purchase Order: purchase_confirmation_doc_type → act_type")
        frappe.db.sql("""
            UPDATE `tabPurchase Order`
            SET act_type = purchase_confirmation_doc_type
            WHERE purchase_confirmation_doc_type IS NOT NULL
        """)
        frappe.logger().info("Purchase Order: purchase_confirmation_doc_type migration complete")

    # Purchase Invoice - migrate agricultural_goods to purchase_type
    if frappe.db.has_column("Purchase Invoice", "agricultural_goods"):
        frappe.logger().info("Migrating Purchase Invoice: agricultural_goods → purchase_type")
        frappe.db.sql("""
            UPDATE `tabPurchase Invoice`
            SET purchase_type = agricultural_goods
            WHERE agricultural_goods IS NOT NULL
        """)
        frappe.logger().info("Purchase Invoice: agricultural_goods migration complete")

    # Purchase Invoice - migrate purchase_confirmation_doc_type to act_type
    if frappe.db.has_column("Purchase Invoice", "purchase_confirmation_doc_type"):
        frappe.logger().info("Migrating Purchase Invoice: purchase_confirmation_doc_type → act_type")
        frappe.db.sql("""
            UPDATE `tabPurchase Invoice`
            SET act_type = purchase_confirmation_doc_type
            WHERE purchase_confirmation_doc_type IS NOT NULL
        """)
        frappe.logger().info("Purchase Invoice: purchase_confirmation_doc_type migration complete")

    # Purchase Invoice Item - migrate agricultural_tax_amount to purchase_tax_amount
    if frappe.db.has_column("Purchase Invoice Item", "agricultural_tax_amount"):
        frappe.logger().info("Migrating Purchase Invoice Item: agricultural_tax_amount → purchase_tax_amount")
        frappe.db.sql("""
            UPDATE `tabPurchase Invoice Item`
            SET purchase_tax_amount = agricultural_tax_amount
            WHERE agricultural_tax_amount IS NOT NULL
        """)
        frappe.logger().info("Purchase Invoice Item: agricultural_tax_amount migration complete")

    # Commit changes
    frappe.db.commit()

    frappe.logger().info("Migration completed successfully: rename_agricultural_to_purchase_type")
    print("Migration completed: agricultural fields renamed to purchase_type")
