select 'post_deployment_script';

SET QUOTED_IDENTIFIER ON; -- IMPORTANT TO ADD IN ALL DEPLOYS
GO

BEGIN
/*
    Caster Mold EQP Seed — 5 Strands + CAS Integration
    ====================================================
    Self-contained seed for clean DB deployments.
    Creates the EQP structure (types, tree, containers, spares, metrics)
    then backfills EQP mold assemblies from CAS.catMolds and installs
    current strand assignments from CAS.StrandData.

    NO hardcoded mold instances — all molds come from CAS.catMolds
    via EQP.CreateMoldAssembly with AuxPartId bridge.

    WarehouseNumber (WN-{MoldName}) is set automatically by
    CreateMoldAssembly for all 57 parts in each assembly.

    Idempotent — safe to run multiple times.
*/

SET NOCOUNT ON;

-- ============================================================================
-- 1. catPartTypes
-- ============================================================================

BEGIN TRY
    SET IDENTITY_INSERT [EQP].[catPartTypes] ON;

    MERGE INTO [EQP].[catPartTypes] AS target
    USING (VALUES
        (1000, 'Caster-Online',    1, 0, NULL, 0),
        (1001, 'Caster-Offline',   1, 0, NULL, 0),
        (1002, 'Strand',           0, 0, NULL, 0),
        (1003, 'Mold',             0, 1, NULL, 0),
        (1010, 'Flanges',              1, 0, NULL, 0),
        (1011, 'Collars',              1, 0, NULL, 0),
        (1012, 'Seals',                1, 0, NULL, 0),
        (1013, 'Foot-Roll-Assembly',   1, 0, NULL, 0),
        (1020, 'Copper-Mold-Tube',         0, 1, NULL, 1),
        (1021, 'Mold-Body',                0, 1, NULL, 2),
        (1022, 'Oil-Distribution-Gasket',  0, 1, NULL, 3),
        (1023, 'Level-Control',            0, 1, NULL, 4),
        (1024, 'Oil-Connector',            0, 1, NULL, 5),
        (1025, 'Water-Jacket-Flume',       0, 1, NULL, 6),
        (1030, 'Upper-Flange',                0, 1, NULL, 1),
        (1031, 'Low-Upper-Flange',            0, 1, NULL, 2),
        (1032, 'Lower-Flange',                0, 1, NULL, 3),
        (1033, 'Closing-Flange',              0, 1, NULL, 4),
        (1034, 'Water-Jacket-Fixing-Flange',  0, 1, NULL, 5),
        (1040, 'Upper-Collar',   0, 1, NULL, 1),
        (1041, 'Lower-Collar',   0, 1, NULL, 2),
        (1050, 'Upper-Seal',              0, 1, NULL, 1),
        (1051, 'Copper-Tube-Seal',        0, 1, NULL, 2),
        (1052, 'Water-Jacket-Seal',       0, 1, NULL, 3),
        (1053, 'Lower-Internal-Seal',     0, 1, NULL, 4),
        (1054, 'Lower-External-Seal',     0, 1, NULL, 5),
        (1055, 'Oil-Connector-O-Ring',    0, 1, NULL, 6),
        (1060, 'Foot-Roll',        0, 1, NULL, 1),
        (1061, 'Roll-Shaft',       0, 1, NULL, 2),
        (1062, 'Roller-Support',   0, 1, NULL, 3),
        (1063, 'Shaft-Keeper',     0, 1, NULL, 4),
        (1064, 'Z1-Spray',         0, 1, NULL, 5),
        (1065, 'Spray-Nozzle',     0, 1, NULL, 6),
        (1100, 'Molds',                        1, 0, NULL, 0),
        (1120, 'Copper-Mold-Tubes',            1, 0, NULL, 0),
        (1121, 'Mold-Bodys',                   1, 0, NULL, 0),
        (1122, 'Oil-Distribution-Gaskets',     1, 0, NULL, 0),
        (1123, 'Level-Controls',               1, 0, NULL, 0),
        (1124, 'Oil-Connectors',               1, 0, NULL, 0),
        (1125, 'Water-Jacket-Flumes',          1, 0, NULL, 0),
        (1130, 'Upper-Flanges',                1, 0, NULL, 0),
        (1131, 'Low-Upper-Flanges',            1, 0, NULL, 0),
        (1132, 'Lower-Flanges',                1, 0, NULL, 0),
        (1133, 'Closing-Flanges',              1, 0, NULL, 0),
        (1134, 'Water-Jacket-Fixing-Flanges',  1, 0, NULL, 0),
        (1140, 'Upper-Collars',                1, 0, NULL, 0),
        (1141, 'Lower-Collars',                1, 0, NULL, 0),
        (1150, 'Upper-Seals',                  1, 0, NULL, 0),
        (1151, 'Copper-Tube-Seals',            1, 0, NULL, 0),
        (1152, 'Water-Jacket-Seals',           1, 0, NULL, 0),
        (1153, 'Lower-Internal-Seals',         1, 0, NULL, 0),
        (1154, 'Lower-External-Seals',         1, 0, NULL, 0),
        (1155, 'Oil-Connector-O-Rings',        1, 0, NULL, 0),
        (1160, 'Foot-Rolls',                   1, 0, NULL, 0),
        (1161, 'Roll-Shafts',                  1, 0, NULL, 0),
        (1162, 'Roller-Supports',              1, 0, NULL, 0),
        (1163, 'Shaft-Keepers',                1, 0, NULL, 0),
        (1164, 'Z1-Sprays',                    1, 0, NULL, 0),
        (1165, 'Spray-Nozzles',               1, 0, NULL, 0)
    ) AS source ([PartTypeId], [PartTypeName], [IsArray], [IsReplaceable], [InheritedFrom], [SortOrder])
    ON target.[PartTypeId] = source.[PartTypeId]
    WHEN NOT MATCHED THEN
        INSERT ([PartTypeId], [PartTypeName], [IsArray], [IsReplaceable], [InheritedFrom], [SortOrder])
        VALUES (source.[PartTypeId], source.[PartTypeName], source.[IsArray],
        source.[IsReplaceable], source.[InheritedFrom], source.[SortOrder])
    WHEN MATCHED THEN
        UPDATE SET [PartTypeName] = source.[PartTypeName], [IsArray] = source.[IsArray];

    SET IDENTITY_INSERT [EQP].[catPartTypes] OFF;
END TRY
BEGIN CATCH
    SET IDENTITY_INSERT [EQP].[catPartTypes] OFF;
    THROW;
END CATCH

-- ============================================================================
-- 2. catPartTypesTree
-- ============================================================================

MERGE INTO [EQP].[catPartTypesTree] AS target
USING (VALUES
    (1000, 1001, 1, NULL),
    (1000, 1002, 2, 'Strand 1'),    (1000, 1002, 3, 'Strand 2'),
    (1000, 1002, 4, 'Strand 3'),    (1000, 1002, 5, 'Strand 4'),
    (1000, 1002, 6, 'Strand 5'),
    (1002, 1003, 1, NULL),
    (1003, 1020,  1, 'Copper Mold Tube'),    (1003, 1021,  2, 'Mold Body'),
    (1003, 1022,  3, 'Oil Distribution Gasket'),    (1003, 1023,  4, 'Level Control'),
    (1003, 1024,  5, 'Oil Connector'),    (1003, 1025,  6, 'Water Jacket Flume'),
    (1003, 1010,  7, 'Flanges'),    (1003, 1011,  8, 'Collars'),    (1003, 1012,  9, 'Seals'),
    (1003, 1013, 10, 'Foot Roll Left'),    (1003, 1013, 11, 'Foot Roll Right'),
    (1003, 1013, 12, 'Foot Roll Internal'),    (1003, 1013, 13, 'Foot Roll External'),
    (1010, 1030, 1, 'Upper Flange'),    (1010, 1031, 2, 'Low Upper Flange'),
    (1010, 1032, 3, 'Lower Flange'),    (1010, 1033, 4, 'Closing Flange'),
    (1010, 1034, 5, 'Water Jacket Fixing Flange'),
    (1011, 1040, 1, 'Upper Collar'),    (1011, 1041, 2, 'Lower Collar'),
    (1012, 1050, 1, 'Upper Seal'),    (1012, 1051, 2, 'Copper Tube Seal Upper'),
    (1012, 1051, 3, 'Copper Tube Seal Lower'),    (1012, 1052, 4, 'Water Jacket Seal'),
    (1012, 1053, 5, 'Lower Internal Seal'),    (1012, 1054, 6, 'Lower External Seal'),
    (1012, 1055, 7, 'Oil Connector O-Ring Upper'),    (1012, 1055, 8, 'Oil Connector O-Ring Lower'),
    (1013, 1060, 1, 'Foot Roll'),    (1013, 1061, 2, 'Roll Shaft'),
    (1013, 1062, 3, 'Roller Support'),    (1013, 1063, 4, 'Shaft Keeper'),
    (1013, 1064, 5, 'Z1 Spray'),    (1013, 1065, 6, 'Spray Nozzle Upper'),
    (1013, 1065, 7, 'Spray Nozzle Lower'),
    (1001, 1100,  1, NULL),
    (1001, 1120,  2, NULL),    (1001, 1121,  3, NULL),    (1001, 1122,  4, NULL),
    (1001, 1123,  5, NULL),    (1001, 1124,  6, NULL),    (1001, 1125,  7, NULL),
    (1001, 1130,  8, NULL),    (1001, 1131,  9, NULL),    (1001, 1132, 10, NULL),
    (1001, 1133, 11, NULL),    (1001, 1134, 12, NULL),
    (1001, 1140, 13, NULL),    (1001, 1141, 14, NULL),
    (1001, 1150, 15, NULL),    (1001, 1151, 16, NULL),    (1001, 1152, 17, NULL),
    (1001, 1153, 18, NULL),    (1001, 1154, 19, NULL),    (1001, 1155, 20, NULL),
    (1001, 1160, 21, NULL),    (1001, 1161, 22, NULL),    (1001, 1162, 23, NULL),
    (1001, 1163, 24, NULL),    (1001, 1164, 25, NULL),    (1001, 1165, 26, NULL),
    (1100, 1003, 1, NULL),
    (1120, 1020, 1, NULL),    (1121, 1021, 1, NULL),    (1122, 1022, 1, NULL),
    (1123, 1023, 1, NULL),    (1124, 1024, 1, NULL),    (1125, 1025, 1, NULL),
    (1130, 1030, 1, NULL),    (1131, 1031, 1, NULL),    (1132, 1032, 1, NULL),
    (1133, 1033, 1, NULL),    (1134, 1034, 1, NULL),
    (1140, 1040, 1, NULL),    (1141, 1041, 1, NULL),
    (1150, 1050, 1, NULL),    (1151, 1051, 1, NULL),    (1152, 1052, 1, NULL),
    (1153, 1053, 1, NULL),    (1154, 1054, 1, NULL),    (1155, 1055, 1, NULL),
    (1160, 1060, 1, NULL),    (1161, 1061, 1, NULL),    (1162, 1062, 1, NULL),
    (1163, 1063, 1, NULL),    (1164, 1064, 1, NULL),    (1165, 1065, 1, NULL)
) AS source ([ParentPartTypeId], [PartTypeId], [PositionId], [PositionName])
ON target.[ParentPartTypeId] = source.[ParentPartTypeId]
   AND target.[PartTypeId] = source.[PartTypeId]
   AND target.[PositionId] = source.[PositionId]
WHEN NOT MATCHED THEN
    INSERT ([ParentPartTypeId], [PartTypeId], [PositionId], [PositionName])
    VALUES (source.[ParentPartTypeId], source.[PartTypeId], source.[PositionId],
    source.[PositionName])
WHEN MATCHED THEN
    UPDATE SET [PositionName] = source.[PositionName];

-- ============================================================================
-- 3. catParts — Structure containers + Spares (NO mold instances)
-- ============================================================================

BEGIN TRY
    SET IDENTITY_INSERT [EQP].[catParts] ON;

    MERGE INTO [EQP].[catParts] AS target
    USING (VALUES
        (1000, 'Caster Online',    1000, 1, 0, NULL),
        (1001, 'Caster Offline',   1001, 1, 0, NULL),
        (1002, 'Strand 1',         1002, 1, 0, NULL),
        (1003, 'Strand 2',         1002, 1, 0, NULL),
        (1004, 'Strand 3',         1002, 1, 0, NULL),
        (1005, 'Strand 4',         1002, 1, 0, NULL),
        (1006, 'Strand 5',         1002, 1, 0, NULL),
        (1100, 'Mold Spares',                     1100, 1, 1, NULL),
        (1120, 'Copper Mold Tube Spares',          1120, 1, 1, NULL),
        (1121, 'Mold Body Spares',                 1121, 1, 1, NULL),
        (1122, 'Oil Distribution Gasket Spares',   1122, 1, 1, NULL),
        (1123, 'Level Control Spares',             1123, 1, 1, NULL),
        (1124, 'Oil Connector Spares',             1124, 1, 1, NULL),
        (1125, 'Water Jacket Flume Spares',        1125, 1, 1, NULL),
        (1130, 'Upper Flange Spares',              1130, 1, 1, NULL),
        (1131, 'Low Upper Flange Spares',          1131, 1, 1, NULL),
        (1132, 'Lower Flange Spares',              1132, 1, 1, NULL),
        (1133, 'Closing Flange Spares',            1133, 1, 1, NULL),
        (1134, 'Water Jacket Fixing Flange Spares',1134, 1, 1, NULL),
        (1140, 'Upper Collar Spares',              1140, 1, 1, NULL),
        (1141, 'Lower Collar Spares',              1141, 1, 1, NULL),
        (1150, 'Upper Seal Spares',                1150, 1, 1, NULL),
        (1151, 'Copper Tube Seal Spares',          1151, 1, 1, NULL),
        (1152, 'Water Jacket Seal Spares',         1152, 1, 1, NULL),
        (1153, 'Lower Internal Seal Spares',       1153, 1, 1, NULL),
        (1154, 'Lower External Seal Spares',       1154, 1, 1, NULL),
        (1155, 'Oil Connector O-Ring Spares',      1155, 1, 1, NULL),
        (1160, 'Foot Roll Spares',                 1160, 1, 1, NULL),
        (1161, 'Roll Shaft Spares',                1161, 1, 1, NULL),
        (1162, 'Roller Support Spares',            1162, 1, 1, NULL),
        (1163, 'Shaft Keeper Spares',              1163, 1, 1, NULL),
        (1164, 'Z1 Spray Spares',                  1164, 1, 1, NULL),
        (1165, 'Spray Nozzle Spares',              1165, 1, 1, NULL)
    ) AS source ([PartId], [PartName], [PartTypeId], [IsActive], [IsDefaultArray], [AuxPartId])
    ON target.[PartId] = source.[PartId]
    WHEN NOT MATCHED THEN
        INSERT ([PartId], [PartName], [PartTypeId], [IsActive], [IsDefaultArray], [AuxPartId])
        VALUES (source.[PartId], source.[PartName], source.[PartTypeId], source.[IsActive],
        source.[IsDefaultArray], source.[AuxPartId])
    WHEN MATCHED THEN
        UPDATE SET [PartName] = source.[PartName], [PartTypeId] = source.[PartTypeId],
            [IsDefaultArray] = source.[IsDefaultArray];

    SET IDENTITY_INSERT [EQP].[catParts] OFF;
END TRY
BEGIN CATCH
    SET IDENTITY_INSERT [EQP].[catParts] OFF;
    THROW;
END CATCH

-- ============================================================================
-- 4. Tasks — Wire structure (containers + spares)
-- ============================================================================

INSERT INTO [EQP].[Tasks] ([Timestamp], [ParentPartId], [PositionId], [PartId], [IsReset], [User])
SELECT NULL, v.ParentPartId, v.PositionId, v.PartId, 0, 'SEED'
FROM (VALUES
    (1000, 1, 1001),
    (1000, 2, 1002),    (1000, 3, 1003),    (1000, 4, 1004),
    (1000, 5, 1005),    (1000, 6, 1006),
    (1001,  1, 1100),    (1001,  2, 1120),    (1001,  3, 1121),
    (1001,  4, 1122),    (1001,  5, 1123),    (1001,  6, 1124),    (1001,  7, 1125),
    (1001,  8, 1130),    (1001,  9, 1131),    (1001, 10, 1132),
    (1001, 11, 1133),    (1001, 12, 1134),
    (1001, 13, 1140),    (1001, 14, 1141),
    (1001, 15, 1150),    (1001, 16, 1151),    (1001, 17, 1152),
    (1001, 18, 1153),    (1001, 19, 1154),    (1001, 20, 1155),
    (1001, 21, 1160),    (1001, 22, 1161),    (1001, 23, 1162),
    (1001, 24, 1163),    (1001, 25, 1164),    (1001, 26, 1165)
) AS v(ParentPartId, PositionId, PartId)
WHERE NOT EXISTS (
    SELECT 1 FROM [EQP].[Tasks] t
    WHERE t.ParentPartId = v.ParentPartId AND t.PartId = v.PartId
);

-- ============================================================================
-- 5. catPartTypesMetricTypes
-- ============================================================================

MERGE INTO [EQP].[catPartTypesMetricTypes] AS target
USING (VALUES
    (1003, 1,   8000, 10000,   4000, 5000,   50000, 60000),
    (1020, 1,   3000,  4000,   1500, 2000,   20000, 25000),
    (1021, 1,   8000, 10000,   4000, 5000,   50000, 60000),
    (1022, 1,   5000,  6000,   2500, 3000,   30000, 35000),
    (1023, 1,   4000,  5000,   2000, 2500,   25000, 30000),
    (1024, 1,   5000,  6000,   2500, 3000,   30000, 35000),
    (1025, 1,   5000,  6000,   2500, 3000,   30000, 35000),
    (1030, 1,   6000,  8000,   3000, 4000,   40000, 50000),
    (1031, 1,   6000,  8000,   3000, 4000,   40000, 50000),
    (1032, 1,   6000,  8000,   3000, 4000,   40000, 50000),
    (1033, 1,   6000,  8000,   3000, 4000,   40000, 50000),
    (1034, 1,   6000,  8000,   3000, 4000,   40000, 50000),
    (1040, 1,   6000,  8000,   3000, 4000,   40000, 50000),
    (1041, 1,   6000,  8000,   3000, 4000,   40000, 50000),
    (1050, 1,   2000,  2500,   1000, 1500,   15000, 20000),
    (1051, 1,   2000,  2500,   1000, 1500,   15000, 20000),
    (1052, 1,   2000,  2500,   1000, 1500,   15000, 20000),
    (1053, 1,   2000,  2500,   1000, 1500,   15000, 20000),
    (1054, 1,   2000,  2500,   1000, 1500,   15000, 20000),
    (1055, 1,   1500,  2000,    800, 1000,   10000, 12000),
    (1060, 1,   3000,  4000,   1500, 2000,   20000, 25000),
    (1061, 1,   4000,  5000,   2000, 2500,   25000, 30000),
    (1062, 1,   5000,  6000,   2500, 3000,   30000, 35000),
    (1063, 1,   5000,  6000,   2500, 3000,   30000, 35000),
    (1064, 1,   3000,  4000,   1500, 2000,   20000, 25000),
    (1065, 1,   2000,  2500,   1000, 1500,   15000, 20000)
) AS source ([PartTypeId], [MetricTypeId],
    [CampaignWarningLimit], [CampaignAlertLimit],
    [PartialWarningLimit], [PartialAlertLimit],
    [TotalWarningLimit], [TotalAlertLimit])
ON target.[PartTypeId] = source.[PartTypeId] AND target.[MetricTypeId] = source.[MetricTypeId]
WHEN NOT MATCHED THEN
    INSERT ([PartTypeId], [MetricTypeId],
        [CampaignWarningLimit], [CampaignAlertLimit],
        [PartialWarningLimit], [PartialAlertLimit],
        [TotalWarningLimit], [TotalAlertLimit])
    VALUES (source.[PartTypeId], source.[MetricTypeId],
        source.[CampaignWarningLimit], source.[CampaignAlertLimit],
        source.[PartialWarningLimit], source.[PartialAlertLimit],
        source.[TotalWarningLimit], source.[TotalAlertLimit])
WHEN MATCHED THEN
    UPDATE SET
        [CampaignWarningLimit] = source.[CampaignWarningLimit],
        [CampaignAlertLimit] = source.[CampaignAlertLimit],
        [PartialWarningLimit] = source.[PartialWarningLimit],
        [PartialAlertLimit] = source.[PartialAlertLimit],
        [TotalWarningLimit] = source.[TotalWarningLimit],
        [TotalAlertLimit] = source.[TotalAlertLimit];

-- Set timestamps on seed tasks only
UPDATE [EQP].[Tasks]                                                                                                                                                                                                                           
SET [Timestamp] = '2026-01-01'                   
WHERE [Timestamp] IS NULL AND [User] = 'SEED';  


-- ═══════════════════════════════════════════════════════════════════
-- EQP CASTER INITIALIZATION
-- Step A: Create mold assemblies for all CAS molds
-- Step B: Install current molds on strands (at fixed early date)
-- Step C: Backdate installs to real campaign start (when mold changed)
-- Step D: Run tracking job to create batches from campaign start
-- Step E: Set CASTER_EQP_INITIAL_DATETIME to latest install date
-- ═══════════════════════════════════════════════════════════════════
IF NOT EXISTS (
    SELECT 1 FROM EQP.catParts
    WHERE PartTypeId = 1003 AND IsActive = 1 AND AuxPartId IS NOT NULL
)
BEGIN
    PRINT 'Initializing EQP Caster...'

    -- Step A: Create mold assemblies for all CAS molds
    DECLARE @SeedCreationDate DATETIME = '2024-01-01 00:00:00'
    DECLARE @CasMoldId INT, @CasMoldName VARCHAR(50)
    DECLARE mold_cursor CURSOR LOCAL FAST_FORWARD FOR
        SELECT MoldId, MoldName FROM CAS.catMolds WHERE IsActive = 1
    OPEN mold_cursor
    FETCH NEXT FROM mold_cursor INTO @CasMoldId, @CasMoldName
    WHILE @@FETCH_STATUS = 0
    BEGIN
        IF NOT EXISTS (SELECT 1 FROM EQP.catParts WHERE AuxPartId = @CasMoldId AND PartTypeId = 1003)
        BEGIN
            EXEC RBD.EQPCreateMoldAssembly @MoldName = @CasMoldName, @AuxPartId = @CasMoldId, @Timestamp = @SeedCreationDate
            PRINT '  Created mold assembly: ' + @CasMoldName + ' (CAS MoldId=' + CAST(@CasMoldId AS VARCHAR) + ')'
        END
        FETCH NEXT FROM mold_cursor INTO @CasMoldId, @CasMoldName
    END
    CLOSE mold_cursor
    DEALLOCATE mold_cursor

    -- Step B: Install current molds on strands (from last Caster heat, at fixed early date)
    DECLARE @SeedInstallDate DATETIME = '2025-01-01 00:00:00'
    DECLARE @LastProcId INT
    SELECT TOP 1 @LastProcId = ProcessId FROM MS.Processes
    WHERE ProcessCode = 'C' AND IsActive = 1 ORDER BY ProcessId DESC

    IF @LastProcId IS NOT NULL
    BEGIN
        DECLARE @Strand INT, @MoldPartId INT, @StrandPartId INT
        DECLARE strand_cursor CURSOR LOCAL FAST_FORWARD FOR
            SELECT sd.Strand,
                   RBD.fGetEqpPartIdByMoldId(sd.MoldId) AS MoldPartId,
                   RBD.fGetCasterStrandPartId(sd.Strand) AS StrandPartId
            FROM CAS.StrandData sd
            WHERE sd.ProcessId = @LastProcId AND sd.MoldId IS NOT NULL
        OPEN strand_cursor
        FETCH NEXT FROM strand_cursor INTO @Strand, @MoldPartId, @StrandPartId
        WHILE @@FETCH_STATUS = 0
        BEGIN
            IF @MoldPartId IS NOT NULL AND @StrandPartId IS NOT NULL
            BEGIN
                EXEC EQP.MovePart @PartId = @MoldPartId, @ParentPartId = @StrandPartId, @PositionId = 1, @Timestamp = @SeedInstallDate, @CurrentUser = 'SEED'
                PRINT '  Strand ' + CAST(@Strand AS VARCHAR) + ': installed MoldPartId=' + CAST(@MoldPartId AS VARCHAR)
            END
            FETCH NEXT FROM strand_cursor INTO @Strand, @MoldPartId, @StrandPartId
        END
        CLOSE strand_cursor
        DEALLOCATE strand_cursor

        -- Step C: Backdate install timestamps to real campaign start dates
        -- For each strand, go backwards from current heat to find when the mold changed.
        -- Campaign start = End of the last heat that had a DIFFERENT mold on that strand.
        PRINT ''
        PRINT '=== Backdating install dates to campaign start ==='

        DECLARE @BdStrand INT, @BdMoldCasId INT, @BdTaskId INT, @CampaignStart DATETIME
        DECLARE backdate_cursor CURSOR LOCAL FAST_FORWARD FOR
            SELECT sd.Strand, sd.MoldId
            FROM CAS.StrandData sd
            WHERE sd.ProcessId = @LastProcId AND sd.MoldId IS NOT NULL
        OPEN backdate_cursor
        FETCH NEXT FROM backdate_cursor INTO @BdStrand, @BdMoldCasId
        WHILE @@FETCH_STATUS = 0
        BEGIN
            SET @CampaignStart = NULL
            SELECT TOP 1 @CampaignStart = DATEADD(MINUTE, 1, p.[End])
            FROM CAS.StrandData sd
            JOIN MS.Processes p ON p.ProcessId = sd.ProcessId
            WHERE sd.Strand = @BdStrand
              AND sd.MoldId != @BdMoldCasId
              AND p.[End] IS NOT NULL
              AND p.ProcessCode = 'C' AND p.IsActive = 1
            ORDER BY p.[End] DESC

            IF @CampaignStart IS NOT NULL
            BEGIN
                DECLARE @BdMoldPartId INT = RBD.fGetEqpPartIdByMoldId(@BdMoldCasId)
                SELECT TOP 1 @BdTaskId = TaskId
                FROM EQP.Tasks
                WHERE PartId = @BdMoldPartId AND IsReset = 0
                ORDER BY TaskId DESC

                IF @BdTaskId IS NOT NULL
                BEGIN
                    EXEC EQP.UpdateTaskTimestamp @TaskId = @BdTaskId, @NewTimestamp = @CampaignStart, @CurrentUser = 'SEED'
                    PRINT '  Strand ' + CAST(@BdStrand AS VARCHAR) + ': backdated to ' + CONVERT(VARCHAR, @CampaignStart, 120)
                END
            END
            ELSE
            BEGIN
                PRINT '  Strand ' + CAST(@BdStrand AS VARCHAR) + ': no mold change found in history, keeping seed timestamp'
            END

            FETCH NEXT FROM backdate_cursor INTO @BdStrand, @BdMoldCasId
        END
        CLOSE backdate_cursor
        DEALLOCATE backdate_cursor

        -- Step D: Run tracking job to create batches from campaign start dates
        DECLARE @EarliestCampaign DATETIME, @LatestCampaign DATETIME
        SELECT
            @EarliestCampaign = MIN(t.[Timestamp]),
            @LatestCampaign = MAX(t.[Timestamp])
        FROM EQP.Tasks t
        JOIN EQP.catParts p ON p.PartId = t.PartId
        WHERE p.PartTypeId = 1003 AND p.IsActive = 1 AND t.IsReset = 0
          AND t.[User] = 'SEED'

        IF @EarliestCampaign IS NOT NULL
        BEGIN
            DECLARE @DaysBack INT = DATEDIFF(DAY, @EarliestCampaign, GETUTCDATE()) + 1
            PRINT ''
            PRINT 'Running initial Caster tracking job (' + CAST(@DaysBack AS VARCHAR) + ' days back)...'
            EXEC RBD.EQPTrackCasterJob @Days = @DaysBack
        END

        -- Step E: Set CASTER_EQP_INITIAL_DATETIME to latest install date
        IF @LatestCampaign IS NOT NULL
        BEGIN
            DECLARE @InitDateStr VARCHAR(50) = CONVERT(VARCHAR, @LatestCampaign, 120)
            EXEC UTL.SetSetting 'CASTER_EQP_INITIAL_DATETIME', 'CASTER_EQP_INITIAL_DATETIME', @InitDateStr, 'Minimum allowed timestamp for Caster equipment task date corrections'
            PRINT '  CASTER_EQP_INITIAL_DATETIME = ' + @InitDateStr
        END
    END

    PRINT 'EQP Caster initialization complete.'
END
ELSE
BEGIN
    PRINT 'EQP Caster already initialized. Skipping.'
END

-- ═══════════════════════════════════════════════════════════════════
-- EQP Core Convergence: Register new SPs for UI permissions
-- Replaces old EQP SP permissions with new EQP core + RBD SPs
-- ═══════════════════════════════════════════════════════════════════



-- ═══════════════════════════════════════════════════════════════════
-- EAF ROOF
-- ═══════════════════════════════════════════════════════════════════

---EAF Roof Operator Profile
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPGetPartsTree]', 'EAF Roof Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[EQP].[UpsertTask]', 'EAF Roof Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[EQP].[GetPartsTree]', 'EAF Roof Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[EQP].[ResetPart]', 'EAF Roof Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[EQP].[GetAvailableParts]', 'EAF Roof Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[EQP].[UpdateMetricsCache]', 'EAF Roof Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[EQP].[DeleteTask]', 'EAF Roof Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPGetPartDetails]', 'EAF Roof Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPGetRoofPartDetails]', 'EAF Roof Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPInstallPart]', 'EAF Roof Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPUninstallPart]', 'EAF Roof Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPRepairPart]', 'EAF Roof Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPAddTaskMetadata]', 'EAF Roof Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPGetTaskMetadata]', 'EAF Roof Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPCreatePart]', 'EAF Roof Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[UpsertPartDetails]', 'EAF Roof Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[GetPartTypesMetricLimits]', 'EAF Roof Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[UpsertPartTypeMetricLimits]', 'EAF Roof Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPSetInitialMetricValue]', 'EAF Roof Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPGetTaskHistory]', 'EAF Roof Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[EQP].[UpdateTaskTimestamp]', 'EAF Roof Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[EQP].[DeletePart]', 'EAF Roof Operator', 'devops'

---EAF Roof Fab Shop Profile
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPGetPartsTree]', 'EAF Roof Fab Shop', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[EQP].[UpsertTask]', 'EAF Roof Fab Shop', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[EQP].[GetPartsTree]', 'EAF Roof Fab Shop', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[EQP].[ResetPart]', 'EAF Roof Fab Shop', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[EQP].[GetAvailableParts]', 'EAF Roof Fab Shop', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[EQP].[UpdateMetricsCache]', 'EAF Roof Fab Shop', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[EQP].[DeleteTask]', 'EAF Roof Fab Shop', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPGetPartDetails]', 'EAF Roof Fab Shop', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPGetRoofPartDetails]', 'EAF Roof Fab Shop', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPInstallPart]', 'EAF Roof Fab Shop', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPUninstallPart]', 'EAF Roof Fab Shop', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPRepairPart]', 'EAF Roof Fab Shop', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPAddTaskMetadata]', 'EAF Roof Fab Shop', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPGetTaskMetadata]', 'EAF Roof Fab Shop', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPCreatePart]', 'EAF Roof Fab Shop', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[UpsertPartDetails]', 'EAF Roof Fab Shop', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[GetPartTypesMetricLimits]', 'EAF Roof Fab Shop', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[UpsertPartTypeMetricLimits]', 'EAF Roof Fab Shop', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPSetInitialMetricValue]', 'EAF Roof Fab Shop', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPGetTaskHistory]', 'EAF Roof Fab Shop', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[EQP].[UpdateTaskTimestamp]', 'EAF Roof Fab Shop', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[EQP].[DeletePart]', 'EAF Roof Fab Shop', 'devops'

-- ═══════════════════════════════════════════════════════════════════
-- EAF SIDEWALL
-- ═══════════════════════════════════════════════════════════════════

IF NOT EXISTS (SELECT 1 FROM SEC.catProfiles WHERE ProfileName = 'EAF Sidewall Operator')
    INSERT INTO SEC.catProfiles (ProfileName) VALUES ('EAF Sidewall Operator');

EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPGetPartsTree]', 'EAF Sidewall Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[EQP].[UpsertTask]', 'EAF Sidewall Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[EQP].[GetPartsTree]', 'EAF Sidewall Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[EQP].[ResetPart]', 'EAF Sidewall Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[EQP].[GetAvailableParts]', 'EAF Sidewall Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[EQP].[UpdateMetricsCache]', 'EAF Sidewall Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[EQP].[DeleteTask]', 'EAF Sidewall Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPGetPartDetails]', 'EAF Sidewall Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPGetSidewallPartDetails]', 'EAF Sidewall Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPInstallPart]', 'EAF Sidewall Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPUninstallPart]', 'EAF Sidewall Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPRepairPart]', 'EAF Sidewall Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPAddTaskMetadata]', 'EAF Sidewall Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPGetTaskMetadata]', 'EAF Sidewall Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPCreatePart]', 'EAF Sidewall Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[UpsertPartDetails]', 'EAF Sidewall Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[GetPartTypesMetricLimits]', 'EAF Sidewall Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[UpsertPartTypeMetricLimits]', 'EAF Sidewall Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPSetInitialMetricValue]', 'EAF Sidewall Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPGetTaskHistory]', 'EAF Sidewall Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[EQP].[UpdateTaskTimestamp]', 'EAF Sidewall Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[EQP].[DeletePart]', 'EAF Sidewall Operator', 'devops'

-- ═══════════════════════════════════════════════════════════════════
-- CASTER MOLD
-- ═══════════════════════════════════════════════════════════════════

EXEC SEC.UpsertProcedureAndSetToProfile 'ui.navbar.caster.mold', 'admin Profile', 'devops';

IF NOT EXISTS (SELECT 1 FROM SEC.catProfiles WHERE ProfileName = 'Caster Mold Operator')
    INSERT INTO SEC.catProfiles (ProfileName) VALUES ('Caster Mold Operator');

EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPGetPartsTree]', 'Caster Mold Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[EQP].[UpsertTask]', 'Caster Mold Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[EQP].[GetPartsTree]', 'Caster Mold Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[EQP].[ResetPart]', 'Caster Mold Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[EQP].[GetAvailableParts]', 'Caster Mold Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[EQP].[UpdateMetricsCache]', 'Caster Mold Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[EQP].[DeleteTask]', 'Caster Mold Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPGetPartDetails]', 'Caster Mold Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPGetCasterPartDetails]', 'Caster Mold Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[GetCasterOfflinePartsTree]', 'Caster Mold Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPInstallPart]', 'Caster Mold Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPUninstallPart]', 'Caster Mold Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPRepairPart]', 'Caster Mold Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPAddTaskMetadata]', 'Caster Mold Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPGetTaskMetadata]', 'Caster Mold Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPCreatePart]', 'Caster Mold Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPCreateMoldAssembly]', 'Caster Mold Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[UpsertPartDetails]', 'Caster Mold Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[GetPartTypesMetricLimits]', 'Caster Mold Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[UpsertPartTypeMetricLimits]', 'Caster Mold Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPSetInitialMetricValue]', 'Caster Mold Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPGetTaskHistory]', 'Caster Mold Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPUpdateTaskTimestampCast]', 'Caster Mold Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[EQP].[DeletePart]', 'Caster Mold Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[GetMetricTypes]', 'Caster Mold Operator', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[GetPartTypes]', 'Caster Mold Operator', 'devops'

-- ═══════════════════════════════════════════════════════════════════
-- ADMIN PROFILE (all EQP SPs for admin users)
-- ═══════════════════════════════════════════════════════════════════

EXEC SEC.UpsertProcedureAndSetToProfile '[EQP].[UpsertTask]', 'admin Profile', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[EQP].[GetPartsTree]', 'admin Profile', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[EQP].[ResetPart]', 'admin Profile', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[EQP].[GetAvailableParts]', 'admin Profile', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[EQP].[UpdateMetricsCache]', 'admin Profile', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[EQP].[DeleteTask]', 'admin Profile', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPGetPartsTree]', 'admin Profile', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPGetPartDetails]', 'admin Profile', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPGetRoofPartDetails]', 'admin Profile', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPGetSidewallPartDetails]', 'admin Profile', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPGetCasterPartDetails]', 'admin Profile', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[GetCasterOfflinePartsTree]', 'admin Profile', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPInstallPart]', 'admin Profile', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPUninstallPart]', 'admin Profile', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPRepairPart]', 'admin Profile', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPAddTaskMetadata]', 'admin Profile', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPGetTaskMetadata]', 'admin Profile', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPCreatePart]', 'admin Profile', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPCreateMoldAssembly]', 'admin Profile', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[UpsertPartDetails]', 'admin Profile', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[GetPartTypesMetricLimits]', 'admin Profile', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[UpsertPartTypeMetricLimits]', 'admin Profile', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[FixPartialMetric]', 'admin Profile', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[DeleteSparePart]', 'admin Profile', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[GetMetricTypes]', 'admin Profile', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[GetPartTypes]', 'admin Profile', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPSetInitialMetricValue]', 'admin Profile', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPGetTaskHistory]', 'admin Profile', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[EQP].[UpdateTaskTimestamp]', 'admin Profile', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPUpdateTaskTimestampCast]', 'admin Profile', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPUpdateTaskTimestamp]', 'admin Profile', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPTrackRoofJob]', 'admin Profile', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPTrackSidewallJob]', 'admin Profile', 'devops'
EXEC SEC.UpsertProcedureAndSetToProfile '[RBD].[EQPTrackCasterJob]', 'admin Profile', 'devops'

-- ═══════════════════════════════════════════════════════════════════
-- DATA CLEANUP
-- ═══════════════════════════════════════════════════════════════════
UPDATE EQP.catParts SET PartName = REPLACE(PartName, N'Â°', N'°') WHERE PartName LIKE N'%Â°%';
UPDATE EQP.catPartTypes SET PartTypeName = REPLACE(PartTypeName, N'Â°', N'°') WHERE PartTypeName LIKE N'%Â°%';

-- ═══════════════════════════════════════════════════════════════════
-- EQP TRACKING JOBS (catJobs registration)
-- ═══════════════════════════════════════════════════════════════════

-- Remove old TrackEQPHeatsJob
DELETE FROM SYSTEM.catJobs WHERE sProcedure = 'RBD.TrackEQPHeatsJob';

-- Register 3 new EQP tracking jobs (idempotent — skip if already exists)
IF NOT EXISTS (SELECT 1 FROM SYSTEM.catJobs WHERE sProcedure = 'RBD.EQPTrackRoofJob')
    INSERT INTO SYSTEM.catJobs (sProcedure, RestartAfterFailure, PeriodTime, Enabled)
    VALUES ('RBD.EQPTrackRoofJob', 1, 30, 1);

IF NOT EXISTS (SELECT 1 FROM SYSTEM.catJobs WHERE sProcedure = 'RBD.EQPTrackSidewallJob')
    INSERT INTO SYSTEM.catJobs (sProcedure, RestartAfterFailure, PeriodTime, Enabled)
    VALUES ('RBD.EQPTrackSidewallJob', 1, 30, 1);

IF NOT EXISTS (SELECT 1 FROM SYSTEM.catJobs WHERE sProcedure = 'RBD.EQPTrackCasterJob')
    INSERT INTO SYSTEM.catJobs (sProcedure, RestartAfterFailure, PeriodTime, Enabled)
    VALUES ('RBD.EQPTrackCasterJob', 1, 30, 1);

-- (Caster initialization is handled above in steps A-E)



update top (1) eqp.catparts set PArtNAme = 'Sidewall Online' where partId = 500

END
