-- Migration 022: Snapshots y Datos Crudos
-- Plan: SNAPSHOTS_Y_DATOS_CRUDOS
-- Adds columns for raw data preservation, payment snapshots, and change history

-- 1. transacciones: Raw data columns for price and type at time of sale
ALTER TABLE transacciones ADD COLUMN IF NOT EXISTS precio_unitario numeric(10,2) DEFAULT NULL;
ALTER TABLE transacciones ADD COLUMN IF NOT EXISTS tipo_item integer DEFAULT NULL;

-- 2. pagos_planes: Payer name snapshot and plan state before payment
ALTER TABLE pagos_planes ADD COLUMN IF NOT EXISTS nombre_pagador varchar(100) DEFAULT NULL;
ALTER TABLE pagos_planes ADD COLUMN IF NOT EXISTS snapshot_plan_antes jsonb DEFAULT NULL;

-- 3. inventario: Change history and audit trail
ALTER TABLE inventario ADD COLUMN IF NOT EXISTS historial_cambios jsonb DEFAULT '[]'::jsonb;
ALTER TABLE inventario ADD COLUMN IF NOT EXISTS updated_at timestamptz DEFAULT now();
ALTER TABLE inventario ADD COLUMN IF NOT EXISTS updated_by_login_id integer DEFAULT NULL;

-- 4. modelos_planes_pago: Change history only (updated_at and updated_by_login_id already exist)
ALTER TABLE modelos_planes_pago ADD COLUMN IF NOT EXISTS historial_cambios jsonb DEFAULT '[]'::jsonb;
