﻿DECLARE intezmenytanevekCursor CURSOR LOCAL FOR 
	SELECT IntezmenyAdatok.C_INTEZMENYID, IntezmenyAdatok.C_TANEVID
		FROM T_INTEZMENYADATOK IntezmenyAdatok
    INNER JOIN T_TANEV Tanev ON Tanev.ID = C_TANEVID
      AND Tanev.TOROLT = 'F'
      AND Tanev.C_AKTIV = 'T'

DECLARE @intezmenyId int, @tanevId int

OPEN intezmenytanevekCursor  
FETCH NEXT FROM intezmenytanevekCursor INTO @intezmenyId, @tanevId

WHILE @@FETCH_STATUS = 0 
BEGIN  

  DECLARE @LELTAR_CONCERNEDId INT = NULL

  SELECT TOP 1 @LELTAR_CONCERNEDId = ID FROM dbo.T_SZEREPKOR WHERE C_SZEREPKORTIPUS = 8755 AND C_INTEZMENYID = @intezmenyId AND C_TANEVID = @tanevId
  IF(@LELTAR_CONCERNEDId IS NULL)  BEGIN
  	INSERT INTO [dbo].T_SZEREPKOR
  			   ([C_SZEREPKORTIPUS]
  			   ,[C_INTEZMENYID]
  			   ,[C_TANEVID]
  			   ,[SERIAL]
  			   ,[LASTCHANGED]
  			   ,[CREATED]
  			   ,[MODIFIER]
  			   ,[CREATOR])
  		 VALUES
  			   (8755  -- LELTAR_CONCERNED
  			   ,@intezmenyId
  			   ,@tanevId
  			   ,0
  			   ,GETDATE()
  			   ,GETDATE()
  			   ,NULL
  			   ,1)
  	SET @LELTAR_CONCERNEDId = SCOPE_IDENTITY()	
  END

  DECLARE @LELTAR_CONCERNEDJogId INT = NULL

  SELECT TOP 1 @LELTAR_CONCERNEDJogId = ID FROM dbo.T_JOGOSULTSAG WHERE C_JOG = 'LELTAR_CONCERNED' AND C_INTEZMENYID = @intezmenyId AND C_TANEVID = @tanevId
  IF(@LELTAR_CONCERNEDJogId IS NULL)
  BEGIN
  	INSERT INTO dbo.T_JOGOSULTSAG
  	(
  		C_JOG,  C_SZEREPKORTIPUS,[C_INTEZMENYID] , [C_TANEVID],  TOROLT,  SERIAL,  CREATED,  CREATOR 
  	)
  	VALUES
  	(
  		N'LELTAR_CONCERNED', -- C_JOG - nvarchar
  		8755, -- LELTAR_CONCERNED
  		@intezmenyId , @tanevId,
  		'F', -- TOROLT - char
  		0, -- SERIAL - int
  		GETDATE(), -- CREATED - datetime
  		0 -- CREATOR - int
  	)

  	SET @LELTAR_CONCERNEDJogId = SCOPE_IDENTITY()
  END

  FETCH NEXT FROM intezmenytanevekCursor INTO @intezmenyId, @tanevId
END  

CLOSE intezmenytanevekCursor  
DEALLOCATE intezmenytanevekCursor

GO

SET IDENTITY_INSERT [T_CONFIGTIPUS] ON
---------------------------------------
INSERT INTO T_CONFIGTIPUS ([ID], [C_CONFIG], [C_MODUL], [TOROLT], [SERIAL], [LASTCHANGED], [CREATED], [MODIFIER], [CREATOR], [ELOZOTANEVIREKORDID])
VALUES
	(15, 'IsEnabled', 'Leltar', 'F', 0, GETDATE(), GETDATE(), 0, 0, NULL)
	,(16, 'Url', 'Leltar', 'F', 0, GETDATE(), GETDATE(), 0, 0, NULL)
---------------------------------------
SET IDENTITY_INSERT [T_CONFIGTIPUS] OFF

DECLARE @EnvironmentName nvarchar(1000)
	SET @EnvironmentName = (SELECT TOP 1 [Value] FROM dev.EnvironmentInfo WHERE [Key] = 'Kornyezet')

DECLARE @leltarUrl nvarchar(255) = CASE @EnvironmentName
  WHEN 'AZURE' THEN 'https://leltar-ema.e-kreta.hu/sso'
  WHEN 'HOI' THEN 'https://leltar-hoi.e-kreta.hu/sso' 
  WHEN 'KK' THEN 'https://leltar-kk.e-kreta.hu/sso'
  WHEN 'NSZFH' THEN 'https://leltar-nszfh.e-kreta.hu/sso'
  WHEN 'NSZFH-EMA' THEN 'https://leltar-nszfh-ema.e-kreta.hu/sso'
END

DECLARE @leltarIsEnabled nvarchar(255) = CASE WHEN @EnvironmentName IN ('AZURE','HOI','KK','NSZFH','NSZFH-EMA') THEN 'true' ELSE 'false' END

INSERT INTO T_INTEZMENYCONFIG ([C_ERTEK], [C_CONFIGTIPUSID], [C_INTEZMENYID], [TOROLT], [SERIAL], [LASTCHANGED], [CREATED], [MODIFIER], [CREATOR], [ELOZOTANEVIREKORDID])
	SELECT @leltarIsEnabled, 15, i.ID, 'F', 0, GETDATE(), GETDATE(), 0, 0, NULL FROM T_INTEZMENY i WHERE i.TOROLT = 'F'
​
INSERT INTO T_INTEZMENYCONFIG ([C_ERTEK], [C_CONFIGTIPUSID], [C_INTEZMENYID], [TOROLT], [SERIAL], [LASTCHANGED], [CREATED], [MODIFIER], [CREATOR], [ELOZOTANEVIREKORDID])
	SELECT @leltarUrl, 16, i.ID, 'F', 0, GETDATE(), GETDATE(), 0, 0, NULL FROM T_INTEZMENY i WHERE i.TOROLT = 'F'
