-- V013_ROLLBACK.sql
-- Reversa de V013__create_puntos_de_venta.sql.
--
-- ADVERTENCIA: ejecutar ELIMINA PuntoDeVenta, SecuenciaComprobante, su historia temporal,
--   el permiso 'administracion:puntos_de_venta:gestionar', sus asignaciones y el SP.
--
-- Uso intended: ROLLBACK en entornos NO-productivos.
-- Prerequisito: no deben existir FKs vivas apuntando a PuntoDeVenta (p.ej., comprobantes FAC-001).
--   Si FAC-001 ya está aplicado, este rollback fallará — usar backup.

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

-- ═══════════════════════════════════════════════════════════════════════
-- 1. Drop SP
-- ═══════════════════════════════════════════════════════════════════════

IF OBJECT_ID(N'dbo.usp_ReservarNumeroComprobante', N'P') IS NOT NULL
BEGIN
    DROP PROCEDURE dbo.usp_ReservarNumeroComprobante;
    PRINT 'SP dbo.usp_ReservarNumeroComprobante dropped.';
END
GO

-- ═══════════════════════════════════════════════════════════════════════
-- 2. Apagar SYSTEM_VERSIONING + remover PERIOD — SecuenciaComprobante primero (FK a PdV)
-- ═══════════════════════════════════════════════════════════════════════

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

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

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

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

-- ═══════════════════════════════════════════════════════════════════════
-- 3. Apagar SYSTEM_VERSIONING + remover PERIOD — PuntoDeVenta
-- ═══════════════════════════════════════════════════════════════════════

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

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

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

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

-- ═══════════════════════════════════════════════════════════════════════
-- 4. Drop tablas (SecuenciaComprobante primero por FK)
-- ═══════════════════════════════════════════════════════════════════════

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

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

-- ═══════════════════════════════════════════════════════════════════════
-- 5. Remover permiso 'administracion:puntos_de_venta:gestionar' + RolPermiso
-- ═══════════════════════════════════════════════════════════════════════

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

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

PRINT '';
PRINT 'V013 rolled back. dbo.PuntoDeVenta, dbo.SecuenciaComprobante and their history removed.';
PRINT 'SP dbo.usp_ReservarNumeroComprobante removed.';
PRINT 'Permiso administracion:puntos_de_venta:gestionar removed.';
GO
