-- V011_ROLLBACK.sql
-- Reversa de V011__create_medio_seccion.sql.
--
-- ⚠️  ADVERTENCIA: ejecutar ELIMINA Medio, Seccion, su historia temporal,
--     el permiso 'administracion:secciones:gestionar' y sus asignaciones.
--     ('administracion:medios:gestionar' NO se toca — es pre-existente de V005.)
--
-- Uso intended: ROLLBACK en entornos NO-productivos.
-- Prerequisito: no deben existir FKs vivas apuntando a Medio (p.ej., Punto de Venta, Tarifario).
--               Si ADM-008, ADM-009 o PRC-* ya están aplicados, este rollback falla — usar backup.

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

-- ═══════════════════════════════════════════════════════════════════════
-- 1. Apagar SYSTEM_VERSIONING + remover PERIOD en Seccion y Medio
-- ═══════════════════════════════════════════════════════════════════════

-- Seccion primero (FK al Medio)
IF EXISTS (SELECT 1 FROM sys.tables WHERE object_id = OBJECT_ID('dbo.Seccion') AND temporal_type = 2)
BEGIN
    ALTER TABLE dbo.Seccion SET (SYSTEM_VERSIONING = OFF);
    PRINT 'Seccion: SYSTEM_VERSIONING OFF.';
END
GO

IF EXISTS (SELECT 1 FROM sys.periods WHERE object_id = OBJECT_ID('dbo.Seccion'))
BEGIN
    ALTER TABLE dbo.Seccion DROP PERIOD FOR SYSTEM_TIME;
    PRINT 'Seccion: PERIOD FOR SYSTEM_TIME dropped.';
END
GO

IF COL_LENGTH('dbo.Seccion', 'ValidFrom') IS NOT NULL
BEGIN
    ALTER TABLE dbo.Seccion DROP CONSTRAINT IF EXISTS DF_Seccion_ValidFrom;
    ALTER TABLE dbo.Seccion DROP CONSTRAINT IF EXISTS DF_Seccion_ValidTo;
    ALTER TABLE dbo.Seccion DROP COLUMN ValidFrom, ValidTo;
    PRINT 'Seccion: ValidFrom/ValidTo dropped.';
END
GO

IF OBJECT_ID(N'dbo.Seccion_History', N'U') IS NOT NULL
BEGIN
    DROP TABLE dbo.Seccion_History;
    PRINT 'Seccion_History dropped.';
END
GO

-- Medio
IF EXISTS (SELECT 1 FROM sys.tables WHERE object_id = OBJECT_ID('dbo.Medio') AND temporal_type = 2)
BEGIN
    ALTER TABLE dbo.Medio SET (SYSTEM_VERSIONING = OFF);
    PRINT 'Medio: SYSTEM_VERSIONING OFF.';
END
GO

IF EXISTS (SELECT 1 FROM sys.periods WHERE object_id = OBJECT_ID('dbo.Medio'))
BEGIN
    ALTER TABLE dbo.Medio DROP PERIOD FOR SYSTEM_TIME;
    PRINT 'Medio: PERIOD FOR SYSTEM_TIME dropped.';
END
GO

IF COL_LENGTH('dbo.Medio', 'ValidFrom') IS NOT NULL
BEGIN
    ALTER TABLE dbo.Medio DROP CONSTRAINT IF EXISTS DF_Medio_ValidFrom;
    ALTER TABLE dbo.Medio DROP CONSTRAINT IF EXISTS DF_Medio_ValidTo;
    ALTER TABLE dbo.Medio DROP COLUMN ValidFrom, ValidTo;
    PRINT 'Medio: ValidFrom/ValidTo dropped.';
END
GO

IF OBJECT_ID(N'dbo.Medio_History', N'U') IS NOT NULL
BEGIN
    DROP TABLE dbo.Medio_History;
    PRINT 'Medio_History dropped.';
END
GO

-- ═══════════════════════════════════════════════════════════════════════
-- 2. Drop Seccion y Medio (Seccion primero por FK)
-- ═══════════════════════════════════════════════════════════════════════

IF OBJECT_ID(N'dbo.Seccion', N'U') IS NOT NULL
BEGIN
    DROP TABLE dbo.Seccion;
    PRINT 'Table dbo.Seccion dropped.';
END
GO

IF OBJECT_ID(N'dbo.Medio', N'U') IS NOT NULL
BEGIN
    DROP TABLE dbo.Medio;
    PRINT 'Table dbo.Medio dropped.';
END
GO

-- ═══════════════════════════════════════════════════════════════════════
-- 3. Remover permiso 'administracion:secciones:gestionar' + RolPermiso
-- ═══════════════════════════════════════════════════════════════════════

DELETE rp
FROM dbo.RolPermiso rp
JOIN dbo.Permiso p ON p.Id = rp.PermisoId
WHERE p.Codigo = 'administracion:secciones:gestionar';
GO

DELETE FROM dbo.Permiso
WHERE Codigo = 'administracion:secciones:gestionar';
GO

PRINT '';
PRINT 'V011 rolled back. dbo.Medio, dbo.Seccion and their history removed.';
PRINT 'administracion:medios:gestionar preserved (pre-existing from V005).';
GO
