# Copyright 2026 Criptomart
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import logging

_logger = logging.getLogger(__name__)


def migrate(cr, version):
    """Migrate price fields from product.template to product.product.

    In version 18.0.2.1.0, these fields were moved from product.template
    to product.product for proper variant handling:
    - last_purchase_price_received
    - list_price_theoritical
    - last_purchase_price_updated
    - last_purchase_price_compute_type
    """
    if not version:
        return

    _logger.info("Migrating price fields from product.template to product.product...")

    # Check if columns exist before attempting migration
    # (they may not exist in fresh installations)
    cr.execute("""
        SELECT EXISTS (
            SELECT FROM information_schema.columns
            WHERE table_name = 'product_product'
            AND column_name = 'last_purchase_price_received'
        );
        """)
    columns_exist = cr.fetchone()[0]

    if not columns_exist:
        _logger.info(
            "Columns do not exist in product_product, skipping migration. "
            "Fresh installation detected."
        )
        return

    # Migrate last_purchase_price_received
    cr.execute("""
        UPDATE product_product pp
        SET last_purchase_price_received = pt.last_purchase_price_received
        FROM product_template pt
        WHERE pp.product_tmpl_id = pt.id
          AND pt.last_purchase_price_received IS NOT NULL
          AND (pp.last_purchase_price_received IS NULL
               OR pp.last_purchase_price_received = '{}')
        """)
    _logger.info("Migrated last_purchase_price_received: %d rows", cr.rowcount)

    # Migrate list_price_theoritical
    cr.execute("""
        UPDATE product_product pp
        SET list_price_theoritical = pt.list_price_theoritical
        FROM product_template pt
        WHERE pp.product_tmpl_id = pt.id
          AND pt.list_price_theoritical IS NOT NULL
          AND (pp.list_price_theoritical IS NULL
               OR pp.list_price_theoritical = '{}')
        """)
    _logger.info("Migrated list_price_theoritical: %d rows", cr.rowcount)

    # Migrate last_purchase_price_updated
    cr.execute("""
        UPDATE product_product pp
        SET last_purchase_price_updated = pt.last_purchase_price_updated
        FROM product_template pt
        WHERE pp.product_tmpl_id = pt.id
          AND pt.last_purchase_price_updated IS NOT NULL
          AND (pp.last_purchase_price_updated IS NULL
               OR pp.last_purchase_price_updated = '{}')
        """)
    _logger.info("Migrated last_purchase_price_updated: %d rows", cr.rowcount)

    # Migrate last_purchase_price_compute_type
    cr.execute("""
        UPDATE product_product pp
        SET last_purchase_price_compute_type = pt.last_purchase_price_compute_type
        FROM product_template pt
        WHERE pp.product_tmpl_id = pt.id
          AND pt.last_purchase_price_compute_type IS NOT NULL
          AND (pp.last_purchase_price_compute_type IS NULL
               OR pp.last_purchase_price_compute_type = '{}')
        """)
    _logger.info("Migrated last_purchase_price_compute_type: %d rows", cr.rowcount)

    _logger.info("Migration of price fields completed.")
