/*
    Post-Deployment Script

    Historical seeds (products, specs, assets, shifts, delays, associations)
    were applied to production on 2026-04-14 and removed from this file.

    Add new post-deployment steps below as needed.
*/

-- ───────────────────────────────────────────────────────────────────────────
-- Punch module SPs: register procedures and grant to 'admin Profile'.
-- ───────────────────────────────────────────────────────────────────────────

-- PunchSpecs (stage-specific specs)
EXEC [SEC].[UpsertProcedureAndSetToProfile] 'FAB.GetPunchSpecs', 'admin Profile', 'dexteel'
EXEC [SEC].[UpsertProcedureAndSetToProfile] 'FAB.UpsertPunchSpecs', 'admin Profile', 'dexteel'
EXEC [SEC].[UpsertProcedureAndSetToProfile] 'FAB.DeletePunchSpecs', 'admin Profile', 'dexteel'

-- PunchBeamPositions (1:N array)
EXEC [SEC].[UpsertProcedureAndSetToProfile] 'FAB.GetPunchBeamPositions', 'admin Profile', 'dexteel'
EXEC [SEC].[UpsertProcedureAndSetToProfile] 'FAB.SetPunchBeamPositions', 'admin Profile', 'dexteel'

-- PunchPerforations (1:N array)
EXEC [SEC].[UpsertProcedureAndSetToProfile] 'FAB.GetPunchPerforations', 'admin Profile', 'dexteel'
EXEC [SEC].[UpsertProcedureAndSetToProfile] 'FAB.SetPunchPerforations', 'admin Profile', 'dexteel'

-- FinishSpecs (bundle delivery attributes)
EXEC [SEC].[UpsertProcedureAndSetToProfile] 'FAB.UpsertFinishSpecs', 'admin Profile', 'dexteel'

-- ───────────────────────────────────────────────────────────────────────────
-- Seed FAB Products (Saw area). PartNumber + ExtendedPartNumber identical for now.
-- Catalogs (catCustomers, catSections, catGrades) are assumed to already exist.
-- ───────────────────────────────────────────────────────────────────────────
DECLARE @CustomerHyundaiId INT = (SELECT [CustomerId] FROM [FAB].[catCustomers] WHERE [CustomerName] = 'Hyundai');
DECLARE @Section2695Id     INT = (SELECT [SectionId]  FROM [FAB].[catSections]  WHERE [SectionName]  = '2695');
DECLARE @Section2718Id     INT = (SELECT [SectionId]  FROM [FAB].[catSections]  WHERE [SectionName]  = '2718');
DECLARE @Grade41Id         INT = (SELECT [GradeId]    FROM [FAB].[catGrades]    WHERE [GradeName]    = '41');

DECLARE @ProductId INT;

IF NOT EXISTS (SELECT 1 FROM [FAB].[Products] WHERE [ExtendedPartNumber] = '2695 Gr41 101.19')
BEGIN
    INSERT INTO [FAB].[Products] ([PartNumber], [ExtendedPartNumber], [CustomerId], [SectionId], [GradeId], [AreaAssetId])
    VALUES ('2695 Gr41 101.19', '2695 Gr41 101.19', NULL, @Section2695Id, @Grade41Id, 20);
    SET @ProductId = SCOPE_IDENTITY();
    INSERT INTO [FAB].[SawSpecs] ([ProductId], [TargetLength], [TopTolerance], [BottomTolerance], [FinishPcB], [MillLength], [MillPcB], [AreaAssetId])
    VALUES (@ProductId, 101.19, 101.23, 101.14, 120, 53.5, 60, 20);
END

IF NOT EXISTS (SELECT 1 FROM [FAB].[Products] WHERE [ExtendedPartNumber] = '2718 Gr41 101.19')
BEGIN
    INSERT INTO [FAB].[Products] ([PartNumber], [ExtendedPartNumber], [CustomerId], [SectionId], [GradeId], [AreaAssetId])
    VALUES ('2718 Gr41 101.19', '2718 Gr41 101.19', @CustomerHyundaiId, @Section2718Id, @Grade41Id, 20);
    SET @ProductId = SCOPE_IDENTITY();
    INSERT INTO [FAB].[SawSpecs] ([ProductId], [TargetLength], [TopTolerance], [BottomTolerance], [FinishPcB], [MillLength], [MillPcB], [AreaAssetId])
    VALUES (@ProductId, 101.19, 101.23, 101.14, 144, 53.5, 72, 20);
END

IF NOT EXISTS (SELECT 1 FROM [FAB].[Products] WHERE [ExtendedPartNumber] = '2718 Gr41 94.93 T')
BEGIN
    INSERT INTO [FAB].[Products] ([PartNumber], [ExtendedPartNumber], [CustomerId], [SectionId], [GradeId], [AreaAssetId])
    VALUES ('2718 Gr41 94.93 T', '2718 Gr41 94.93 T', @CustomerHyundaiId, @Section2718Id, @Grade41Id, 20);
    SET @ProductId = SCOPE_IDENTITY();
    INSERT INTO [FAB].[SawSpecs] ([ProductId], [TargetLength], [TopTolerance], [BottomTolerance], [FinishPcB], [MillLength], [MillPcB], [AreaAssetId])
    VALUES (@ProductId, 94.93, 94.97, 94.88, 144, NULL, 72, 20);
END
