-- =============================================================================
-- DENTALSYS — Actualización de esquema (consolidado, entorno local)
-- Para PRODUCCIÓN dentalbuysv_genesis use: dentalbuysv_genesis_schema.sql
-- Idempotente: ignora error "Duplicate column name" (1060).
-- =============================================================================

DROP PROCEDURE IF EXISTS dentalsys_apply_schema_update;

DELIMITER $$

CREATE PROCEDURE dentalsys_apply_schema_update()
BEGIN
  DECLARE CONTINUE HANDLER FOR 1060 BEGIN END;

  ALTER TABLE `evolution`
    ADD COLUMN `treatment_id` INT UNSIGNED NULL DEFAULT NULL AFTER `medic_id`;

  ALTER TABLE `forecast`
    ADD COLUMN `medic_id` INT UNSIGNED NULL DEFAULT NULL AFTER `specialty_id`;

  ALTER TABLE `forecast`
    ADD COLUMN `date` DATE NULL DEFAULT NULL AFTER `medic_id`;

  ALTER TABLE `history`
    ADD COLUMN `prescription_number` INT UNSIGNED NULL DEFAULT NULL AFTER `prescription_id`;

  ALTER TABLE `forecast`
    ADD COLUMN `plan_items` JSON NULL DEFAULT NULL AFTER `description`;
END$$

DELIMITER ;

CALL dentalsys_apply_schema_update();
DROP PROCEDURE dentalsys_apply_schema_update;

-- Verificación
SHOW COLUMNS FROM `evolution` LIKE 'treatment_id';
SHOW COLUMNS FROM `forecast` LIKE 'medic_id';
SHOW COLUMNS FROM `forecast` LIKE 'date';
SHOW COLUMNS FROM `forecast` LIKE 'plan_items';
SHOW COLUMNS FROM `history` LIKE 'prescription_number';
