-- V021_ROLLBACK.sql
-- PRC-001: Reversa de V021__create_chargeable_char_config.sql.
--
-- Pasos:
--   1. Deshabilita SYSTEM_VERSIONING en dbo.ChargeableCharConfig (requerido antes de DROP TABLE).
--   2. Elimina el PERIOD FOR SYSTEM_TIME y las columnas hidden SysStartTime/SysEndTime.
--   3. Drop de dbo.ChargeableCharConfig_History.
--   4. Drop de dbo.ChargeableCharConfig (constraints + índices en cascada).
--   5. Drop de dbo.usp_ChargeableCharConfig_InsertWithClose.
--   6. Drop de dbo.usp_ChargeableCharConfig_GetActiveForMedio.
--
-- ADVERTENCIA: destruye toda la configuración de caracteres tasables. Solo DEV/TEST.
-- Run on: SIGCM2 (dev), SIGCM2_Test_App, SIGCM2_Test_Api.

SET QUOTED_IDENTIFIER ON;
SET ANSI_NULLS ON;
SET NOCOUNT ON;
GO

-- 1. Deshabilita SYSTEM_VERSIONING (imprescindible antes de DROP TABLE temporal).
IF EXISTS (SELECT 1 FROM sys.tables WHERE object_id = OBJECT_ID('dbo.ChargeableCharConfig') AND temporal_type = 2)
BEGIN
    ALTER TABLE dbo.ChargeableCharConfig SET (SYSTEM_VERSIONING = OFF);
    PRINT 'ChargeableCharConfig: SYSTEM_VERSIONING = OFF.';
END
GO

-- 2. Elimina el PERIOD y las hidden cols.
IF COL_LENGTH('dbo.ChargeableCharConfig', 'SysStartTime') IS NOT NULL
BEGIN
    ALTER TABLE dbo.ChargeableCharConfig
    DROP PERIOD FOR SYSTEM_TIME;

    IF EXISTS (SELECT 1 FROM sys.default_constraints WHERE name = 'DF_ChargeableCharConfig_SysStartTime')
        ALTER TABLE dbo.ChargeableCharConfig DROP CONSTRAINT DF_ChargeableCharConfig_SysStartTime;
    IF EXISTS (SELECT 1 FROM sys.default_constraints WHERE name = 'DF_ChargeableCharConfig_SysEndTime')
        ALTER TABLE dbo.ChargeableCharConfig DROP CONSTRAINT DF_ChargeableCharConfig_SysEndTime;

    ALTER TABLE dbo.ChargeableCharConfig DROP COLUMN SysStartTime;
    ALTER TABLE dbo.ChargeableCharConfig DROP COLUMN SysEndTime;
    PRINT 'ChargeableCharConfig: PERIOD + hidden cols dropped.';
END
GO

-- 3. Drop de la history table.
IF OBJECT_ID(N'dbo.ChargeableCharConfig_History', N'U') IS NOT NULL
BEGIN
    DROP TABLE dbo.ChargeableCharConfig_History;
    PRINT 'Table dbo.ChargeableCharConfig_History dropped.';
END
GO

-- 4. Drop de la tabla principal.
IF OBJECT_ID(N'dbo.ChargeableCharConfig', N'U') IS NOT NULL
BEGIN
    DROP TABLE dbo.ChargeableCharConfig;
    PRINT 'Table dbo.ChargeableCharConfig dropped.';
END
GO

-- 5. Drop del SP InsertWithClose.
IF OBJECT_ID(N'dbo.usp_ChargeableCharConfig_InsertWithClose', N'P') IS NOT NULL
BEGIN
    DROP PROCEDURE dbo.usp_ChargeableCharConfig_InsertWithClose;
    PRINT 'Procedure dbo.usp_ChargeableCharConfig_InsertWithClose dropped.';
END
GO

-- 6. Drop del SP GetActiveForMedio.
IF OBJECT_ID(N'dbo.usp_ChargeableCharConfig_GetActiveForMedio', N'P') IS NOT NULL
BEGIN
    DROP PROCEDURE dbo.usp_ChargeableCharConfig_GetActiveForMedio;
    PRINT 'Procedure dbo.usp_ChargeableCharConfig_GetActiveForMedio dropped.';
END
GO

PRINT '';
PRINT 'V021 rollback complete — dbo.ChargeableCharConfig, dbo.ChargeableCharConfig_History, usp_ChargeableCharConfig_InsertWithClose, usp_ChargeableCharConfig_GetActiveForMedio removed.';
GO
