
ALTER TABLE platform.notas_internas
    DROP CONSTRAINT IF EXISTS fk_notas_estadistica;

ALTER TABLE platform.notas_internas
    ADD CONSTRAINT fk_notas_estadistica
    FOREIGN KEY (id_estadistica)
    REFERENCES platform.estadisticas (id_estadistica)
    ON DELETE RESTRICT;

ALTER TABLE platform.notas_internas
    DROP CONSTRAINT IF EXISTS fk_notas_plan;

ALTER TABLE platform.notas_internas
    ADD CONSTRAINT fk_notas_plan
    FOREIGN KEY (id_plan_pago)
    REFERENCES commerce.planes_pago (id_plan)
    ON DELETE RESTRICT;

ALTER TABLE platform.notas_internas
    DROP CONSTRAINT IF EXISTS fk_notas_venta;

ALTER TABLE platform.notas_internas
    ADD CONSTRAINT fk_notas_venta
    FOREIGN KEY (id_venta)
    REFERENCES commerce.ventas (id_venta)
    ON DELETE RESTRICT;

ALTER TABLE platform.notas_internas
    DROP CONSTRAINT IF EXISTS notas_cliente_id_cliente_fkey;

ALTER TABLE platform.notas_internas
    ADD CONSTRAINT notas_cliente_id_cliente_fkey
    FOREIGN KEY (id_cliente)
    REFERENCES crm.clientes (id_cliente);

TRUNCATE TABLE catalog.inventario_variantes_resolucion_log;

INSERT INTO catalog.inventario_variantes_resolucion_log (
    id_log,
    business_id,
    id_item,
    id_variante,
    id_cliente,
    category_id_contexto,
    strategy,
    source_context,
    nombre_final,
    precio_final,
    moneda,
    origen,
    payload_contexto,
    resultado_contexto,
    id_venta,
    id_transaccion,
    created_at,
    created_by_login_id
)
SELECT
    id_log,
    business_id,
    id_item,
    id_variante,
    id_cliente,
    category_id_contexto,
    strategy,
    source_context,
    nombre_final,
    precio_final,
    moneda,
    origen,
    payload_contexto,
    resultado_contexto,
    id_venta,
    id_transaccion,
    created_at,
    created_by_login_id
FROM public.inventario_variantes_resolucion_log;

SELECT setval(
    'catalog.inventario_variantes_resolucion_log_id_log_seq'::regclass,
    COALESCE((SELECT MAX(id_log) FROM catalog.inventario_variantes_resolucion_log), 1),
    COALESCE((SELECT MAX(id_log) FROM catalog.inventario_variantes_resolucion_log), 0) > 0
);

ALTER TABLE commerce.ventas
    DROP CONSTRAINT IF EXISTS fk_ventas_cliente;

ALTER TABLE commerce.ventas
    ADD CONSTRAINT fk_ventas_cliente
    FOREIGN KEY (id_cliente)
    REFERENCES crm.clientes (id_cliente)
    ON DELETE SET NULL
    ON UPDATE NO ACTION;

ALTER TABLE commerce.planes_pago
    DROP CONSTRAINT IF EXISTS fk_planes_cliente;

ALTER TABLE commerce.planes_pago
    ADD CONSTRAINT fk_planes_cliente
    FOREIGN KEY (id_cliente)
    REFERENCES crm.clientes (id_cliente)
    ON DELETE RESTRICT;

ALTER TABLE commerce.pagos_planes
    DROP CONSTRAINT IF EXISTS fk_pago_pagador;

ALTER TABLE commerce.pagos_planes
    ADD CONSTRAINT fk_pago_pagador
    FOREIGN KEY (id_cliente_pagador)
    REFERENCES crm.clientes (id_cliente)
    ON DELETE RESTRICT;

ANALYZE platform.notas_internas;
ANALYZE commerce.ventas;
ANALYZE commerce.planes_pago;
ANALYZE commerce.pagos_planes;
ANALYZE catalog.inventario_variantes_resolucion_log;
