DECLARE @JustForDebug TINYINT = 0;

-- SETUP VARIABLES
DECLARE @Type NVARCHAR(3) = 'D1';
DECLARE @Month INT = 11;
DECLARE @Year INT = 2025;
DECLARE @MonthName NVARCHAR(20) = 'Pazdziernik_2025';

IF @Type NOT IN ('K5', 'PU', 'AK', 'FK', 'D1', 'FK2')
BEGIN
    SELECT 'Type is not valid' AS Logger;
    RETURN;
END;

DECLARE @Range NVARCHAR(20);
SET @Range = 
    CASE @Type
        WHEN 'D1'  THEN 'A7:S200'
        WHEN 'FK2' THEN 'A8:J1652'
        ELSE 'E4:DA5' 
    END;
DECLARE @ImportType NVARCHAR(20);
SET @ImportType = 
    CASE @Type
        WHEN 'D1' THEN 'D1'
        WHEN 'FK2' THEN 'FK2'
        ELSE 'Standard' 
    END;
DECLARE @StartDate NVARCHAR(10) = FORMAT(DATEADD(DAY, 24, DATEADD(MONTH, @Month - 2, DATEFROMPARTS(YEAR(GETDATE()), 1, 1))), 'yyyy.MM.dd');
DECLARE @EndDate NVARCHAR(10) = FORMAT(DATEFROMPARTS(YEAR(GETDATE()), @Month + 1, 5), 'yyyy.MM.dd');
DECLARE @Number INT = (SELECT COUNT(id) + 1  FROM [diunabi-morska].[dbo].[Layers]);
DECLARE @CurrentTimestamp NVARCHAR(14) = FORMAT(GETDATE(), 'yyyyMMddHHmm');
DECLARE @FormattedMonth NVARCHAR(2) = FORMAT(@Month, '00');
DECLARE @Name NVARCHAR(50) = CONCAT(
    'L', @Number, '-A-IW_', @Type, '-', @Year, '/', @FormattedMonth, '-', @CurrentTimestamp
);
DECLARE @SheetId NVARCHAR(100);
SET @SheetId = 
    CASE @Type
        WHEN 'K5'  THEN '1ZzndU8HjYqz5VKCcrVHBOFW8fqpYfwquclznX9q39Yk'
        WHEN 'PU'  THEN '1gnJhqdlL622GOJEuAiVzxJu1N1ZlSb4a-JvGB1rwHAo'
        WHEN 'AK'  THEN '1epk7AdNNvTyN4TKIIOqan-h5A8HYGbRVM_OS-Ns-Qlg'
        WHEN 'FK'  THEN '1k_frcE49O-J_n5MaNiVLKq62-OTCJku1qutGb8YF9M0'
        WHEN 'D1'  THEN '1wplF3CRHweDE8A3Dk9dYbhWAatj1kp0UBM7MiFE26hE'
        WHEN 'FK2' THEN '1k_frcE49O-J_n5MaNiVLKq62-OTCJku1qutGb8YF9M0'
        ELSE NULL  -- If @Type doesn't match, set it to NULL
    END;
IF @SheetId IS NULL
BEGIN
    SELECT 'SheetId is NULL' AS Logger;
    RETURN;
END;
DECLARE @Plugin NVARCHAR(100);
SET @Plugin =
        CASE @Type
            WHEN 'K5'  THEN 'Morska.Import.Standard'
            WHEN 'PU'  THEN 'Morska.Import.Standard'
            WHEN 'AK'  THEN 'Morska.Import.Standard'
            WHEN 'FK'  THEN 'Morska.Import.Standard'
            WHEN 'D1'  THEN 'Morska.Import.D1'
            WHEN 'FK2' THEN 'Morska.Import.FK2'
            ELSE NULL  -- If @Type doesn't match, set it to NULL
            END;
IF @SheetId IS NULL
    BEGIN
        SELECT 'SheetId is NULL' AS Logger;
        RETURN;
    END;
DECLARE @LayerId UNIQUEIDENTIFIER = NEWID();

SELECT @Name AS Name,@Range AS Range, @ImportType AS ImportType, @StartDate AS StartDate, @EndDate AS EndDate;

IF @JustForDebug = 1
BEGIN
    SELECT 'Just for debug' AS Logger;
    RETURN;
END;


INSERT INTO [diunabi-morska].[dbo].[Layers]
([Id], [Number], [Name], [CreatedAt], [ModifiedAt], [IsDeleted], [IsCancelled], [CreatedById], [ModifiedById], [Type])
VALUES (@LayerId, @Number, @Name, GETDATE(), GETDATE(), 0, 0, '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 2);

INSERT INTO [diunabi-morska].[dbo].[Records]
([Id], [Code], [Desc1], [CreatedAt], [ModifiedAt], [CreatedById], [ModifiedById], [IsDeleted], [LayerId])
VALUES ((SELECT NEWID()), 'StartDate', @StartDate, GETDATE(), GETDATE(), '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 0, @LayerId);

INSERT INTO [diunabi-morska].[dbo].[Records]
([Id], [Code], [Desc1], [CreatedAt], [ModifiedAt], [CreatedById], [ModifiedById], [IsDeleted], [LayerId])
VALUES ((SELECT NEWID()), 'EndDate', @EndDate, GETDATE(), GETDATE(), '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 0, @LayerId);

INSERT INTO [diunabi-morska].[dbo].[Records]
([Id], [Code], [Desc1], [CreatedAt], [ModifiedAt], [CreatedById], [ModifiedById], [IsDeleted], [LayerId])
VALUES ((SELECT NEWID()), 'Source', 'GoogleSheet', GETDATE(), GETDATE(), '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 0, @LayerId);

INSERT INTO [diunabi-morska].[dbo].[Records]
([Id], [Code], [Desc1], [CreatedAt], [ModifiedAt], [CreatedById], [ModifiedById], [IsDeleted], [LayerId])
VALUES ((SELECT NEWID()), 'SheetTabName', @MonthName, GETDATE(), GETDATE(), '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 0, @LayerId);

INSERT INTO [diunabi-morska].[dbo].[Records]
([Id], [Code], [Desc1], [CreatedAt], [ModifiedAt], [CreatedById], [ModifiedById], [IsDeleted], [LayerId])
VALUES ((SELECT NEWID()), 'SheetId', @SheetId, GETDATE(), GETDATE(), '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 0, @LayerId);

INSERT INTO [diunabi-morska].[dbo].[Records]
([Id], [Code], [Desc1], [CreatedAt], [ModifiedAt], [CreatedById], [ModifiedById], [IsDeleted], [LayerId])
VALUES ((SELECT NEWID()), 'DataRange', @Range, GETDATE(), GETDATE(), '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 0, @LayerId);

INSERT INTO [diunabi-morska].[dbo].[Records]
([Id], [Code], [Desc1], [CreatedAt], [ModifiedAt], [CreatedById], [ModifiedById], [IsDeleted], [LayerId])
VALUES ((SELECT NEWID()), 'ImportName', @Type, GETDATE(), GETDATE(), '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 0, @LayerId);

INSERT INTO [diunabi-morska].[dbo].[Records]
([Id], [Code], [Desc1], [CreatedAt], [ModifiedAt], [CreatedById], [ModifiedById], [IsDeleted], [LayerId])
VALUES ((SELECT NEWID()), 'ImportMonth', @FormattedMonth, GETDATE(), GETDATE(), '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 0, @LayerId);

INSERT INTO [diunabi-morska].[dbo].[Records]
([Id], [Code], [Desc1], [CreatedAt], [ModifiedAt], [CreatedById], [ModifiedById], [IsDeleted], [LayerId])
VALUES ((SELECT NEWID()), 'ImportYear', @Year, GETDATE(), GETDATE(), '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 0, @LayerId);

INSERT INTO [diunabi-morska].[dbo].[Records]
([Id], [Code], [Desc1], [CreatedAt], [ModifiedAt], [CreatedById], [ModifiedById], [IsDeleted], [LayerId])
VALUES ((SELECT NEWID()), 'Type', 'ImportWorker', GETDATE(), GETDATE(), '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 0, @LayerId);

INSERT INTO [diunabi-morska].[dbo].[Records]
([Id], [Code], [Desc1], [CreatedAt], [ModifiedAt], [CreatedById], [ModifiedById], [IsDeleted], [LayerId])
VALUES ((SELECT NEWID()), 'Plugin', @Plugin, GETDATE(), GETDATE(), '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 0, @LayerId);

INSERT INTO [diunabi-morska].[dbo].[Records]
([Id], [Code], [Desc1], [CreatedAt], [ModifiedAt], [CreatedById], [ModifiedById], [IsDeleted], [LayerId])
VALUES ((SELECT NEWID()), 'IsEnabled', 'True', GETDATE(), GETDATE(), '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 0, @LayerId);

INSERT INTO [diunabi-morska].[dbo].[Records]
([Id], [Code], [Desc1], [CreatedAt], [ModifiedAt], [CreatedById], [ModifiedById], [IsDeleted], [LayerId])
VALUES ((SELECT NEWID()), 'ImportType', @ImportType, GETDATE(), GETDATE(), '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 0, @LayerId);

SELECT 'ImportWorker created' AS Logger;