using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Exferia_EntityFramework.Script_Actualizacion
{
    class Scripts_Version_1429
    {
        public static Version G_VRS_VERSION = new Version("1.4.2.9");
        public static long G_LNG_VERSION = 1429;
        public static int G_INT_TIPO = 0;
        public static string G_STR_SCRIPT =
            $@"
            IF exists 
                (
                    SELECT *
                    FROM INFORMATION_SCHEMA.TABLES
                    WHERE TABLE_NAME = 'CRM_AccionesLineas'
                )
            BEGIN
                IF exists 
                (
                    SELECT *
                    FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
                    WHERE CONSTRAINT_NAME='PK_CRM_AccionesLineas' AND TABLE_NAME = 'CRM_AccionesLineas'
                )
                BEGIN
                    ALTER TABLE [{nameof(CRM_AccionesLineas)}] DROP CONSTRAINT [PK_CRM_AccionesLineas]
                END

                IF exists 
                (
                    SELECT *
                    FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
                    WHERE CONSTRAINT_NAME='FK_CRM_AccionesLineas_CRM_AccionesCabecera' AND TABLE_NAME = 'CRM_AccionesLineas'
                )
                BEGIN
                    ALTER TABLE [{nameof(CRM_AccionesLineas)}] DROP CONSTRAINT [FK_CRM_AccionesLineas_CRM_AccionesCabecera]
                END

                DROP TABLE [CRM_AccionesLineas]
            END

            IF not exists
            (
                SELECT * 
                FROM INFORMATION_SCHEMA.TABLES
                WHERE TABLE_NAME = 'CRM_AccionesLineas'
            )
            BEGIN
                CREATE TABLE CRM_AccionesLineas
                (
	                [id] [bigint] IDENTITY(1,1) NOT NULL,
	                [idCabecera][bigint]NOT NULL,
                    [numeroLinea] [int] NOT NULL,
	                [descripcion] [varchar](100) NULL,
                    [fecha] [datetime] NULL,
                    [idAccion][bigint]NOT NULL,
                    [observaciones] [text] NULL,
                    [fechaBorrado] [datetime] NULL,

                    CONSTRAINT [PK_CRM_AccionesLineas] PRIMARY KEY CLUSTERED 
                        ([id] ASC) WITH 
                        (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
                ) ON [PRIMARY]

                ALTER TABLE CRM_AccionesLineas WITH CHECK ADD  CONSTRAINT [FK_CRM_AccionesLineas_CRM_AccionesCabecera] FOREIGN KEY([idCabecera]) REFERENCES CRM_AccionesCabecera ([id])
                ALTER TABLE CRM_AccionesLineas CHECK CONSTRAINT [FK_CRM_AccionesLineas_CRM_AccionesCabecera]

                ALTER TABLE CRM_AccionesLineas WITH CHECK ADD  CONSTRAINT [FK_CRM_AccionesLineas_CRM_Acciones] FOREIGN KEY([idAccion]) REFERENCES CRM_Acciones ([id])
                ALTER TABLE CRM_AccionesLineas CHECK CONSTRAINT [FK_CRM_AccionesLineas_CRM_Acciones]
            END";
    }

}
