﻿IF OBJECT_ID(N'[__EFMigrationsHistory]') IS NULL
BEGIN
    CREATE TABLE [__EFMigrationsHistory] (
        [MigrationId] nvarchar(150) NOT NULL,
        [ProductVersion] nvarchar(32) NOT NULL,
        CONSTRAINT [PK___EFMigrationsHistory] PRIMARY KEY ([MigrationId])
    );
END;
GO

BEGIN TRANSACTION;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260710025438_InitialCreate'
)
BEGIN
    CREATE TABLE [Partners] (
        [Id] uniqueidentifier NOT NULL,
        [Code] nvarchar(40) NOT NULL,
        [Name] nvarchar(120) NOT NULL,
        [PartnerType] nvarchar(32) NOT NULL,
        [ContactName] nvarchar(80) NOT NULL,
        [ContactPhone] nvarchar(32) NOT NULL,
        CONSTRAINT [PK_Partners] PRIMARY KEY ([Id])
    );
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260710025438_InitialCreate'
)
BEGIN
    CREATE TABLE [ProductionLines] (
        [Id] uniqueidentifier NOT NULL,
        [Code] nvarchar(40) NOT NULL,
        [Name] nvarchar(80) NOT NULL,
        [Owner] nvarchar(80) NOT NULL,
        [IsActive] bit NOT NULL,
        CONSTRAINT [PK_ProductionLines] PRIMARY KEY ([Id])
    );
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260710025438_InitialCreate'
)
BEGIN
    CREATE TABLE [ProductionOrders] (
        [Id] uniqueidentifier NOT NULL,
        [Code] nvarchar(40) NOT NULL,
        [ProductName] nvarchar(200) NOT NULL,
        [BatchQty] int NOT NULL,
        [RequiredQty] int NOT NULL,
        [CompletedQty] int NOT NULL,
        [Progress] int NOT NULL,
        [Status] nvarchar(32) NOT NULL,
        [RiskLevel] nvarchar(32) NOT NULL,
        [DelayDays] int NOT NULL,
        [PlannedStart] datetime2 NOT NULL,
        [PlannedEnd] datetime2 NOT NULL,
        [LineId] uniqueidentifier NULL,
        [LineName] nvarchar(80) NOT NULL,
        [Owner] nvarchar(80) NOT NULL,
        [PlanAchievement] float NOT NULL,
        [DailyDelta] float NOT NULL,
        [ThumbnailKey] nvarchar(40) NOT NULL,
        [CreatedAt] datetimeoffset NOT NULL,
        [UpdatedAt] datetimeoffset NOT NULL,
        [RowVersion] rowversion NOT NULL,
        CONSTRAINT [PK_ProductionOrders] PRIMARY KEY ([Id]),
        CONSTRAINT [FK_ProductionOrders_ProductionLines_LineId] FOREIGN KEY ([LineId]) REFERENCES [ProductionLines] ([Id]) ON DELETE SET NULL
    );
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260710025438_InitialCreate'
)
BEGIN
    CREATE TABLE [ProductionNodes] (
        [Id] uniqueidentifier NOT NULL,
        [OrderId] uniqueidentifier NOT NULL,
        [ParentNodeId] uniqueidentifier NULL,
        [Code] nvarchar(60) NOT NULL,
        [OperationCode] nvarchar(60) NOT NULL,
        [MaterialCode] nvarchar(80) NOT NULL,
        [Name] nvarchar(240) NOT NULL,
        [Level] int NOT NULL,
        [NodeType] nvarchar(32) NOT NULL,
        [SupplyType] nvarchar(32) NOT NULL,
        [RequiredQty] int NOT NULL,
        [CompletedQty] int NOT NULL,
        [DefectQty] int NOT NULL,
        [Progress] int NOT NULL,
        [Status] nvarchar(32) NOT NULL,
        [RiskLevel] nvarchar(32) NOT NULL,
        [DelayDays] int NOT NULL,
        [DelayReason] nvarchar(500) NOT NULL,
        [PlannedStart] datetime2 NOT NULL,
        [PlannedEnd] datetime2 NOT NULL,
        [ActualStart] datetime2 NULL,
        [ActualEnd] datetime2 NULL,
        [Owner] nvarchar(100) NOT NULL,
        [Vendor] nvarchar(120) NOT NULL,
        [StationName] nvarchar(120) NOT NULL,
        [VisualKey] nvarchar(40) NOT NULL,
        [SortOrder] int NOT NULL,
        [UpdatedAt] datetimeoffset NOT NULL,
        [RowVersion] rowversion NOT NULL,
        CONSTRAINT [PK_ProductionNodes] PRIMARY KEY ([Id]),
        CONSTRAINT [AK_ProductionNodes_OrderId_Id] UNIQUE ([OrderId], [Id]),
        CONSTRAINT [FK_ProductionNodes_ProductionNodes_OrderId_ParentNodeId] FOREIGN KEY ([OrderId], [ParentNodeId]) REFERENCES [ProductionNodes] ([OrderId], [Id]) ON DELETE NO ACTION,
        CONSTRAINT [FK_ProductionNodes_ProductionOrders_OrderId] FOREIGN KEY ([OrderId]) REFERENCES [ProductionOrders] ([Id]) ON DELETE CASCADE
    );
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260710025438_InitialCreate'
)
BEGIN
    CREATE TABLE [ProductionTrendPoints] (
        [Id] uniqueidentifier NOT NULL,
        [OrderId] uniqueidentifier NOT NULL,
        [SampleTime] datetimeoffset NOT NULL,
        [Completion] int NOT NULL,
        [Risk] int NOT NULL,
        [PlannedQty] int NOT NULL,
        [ActualQty] int NOT NULL,
        [Achievement] float NOT NULL,
        CONSTRAINT [PK_ProductionTrendPoints] PRIMARY KEY ([Id]),
        CONSTRAINT [FK_ProductionTrendPoints_ProductionOrders_OrderId] FOREIGN KEY ([OrderId]) REFERENCES [ProductionOrders] ([Id]) ON DELETE CASCADE
    );
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260710025438_InitialCreate'
)
BEGIN
    CREATE TABLE [RiskEvents] (
        [Id] uniqueidentifier NOT NULL,
        [OrderId] uniqueidentifier NOT NULL,
        [NodeId] uniqueidentifier NOT NULL,
        [OrderCode] nvarchar(40) NOT NULL,
        [NodeName] nvarchar(240) NOT NULL,
        [RiskLevel] nvarchar(32) NOT NULL,
        [Message] nvarchar(500) NOT NULL,
        [HandlingStatus] nvarchar(32) NOT NULL,
        [OccurredAt] datetimeoffset NOT NULL,
        CONSTRAINT [PK_RiskEvents] PRIMARY KEY ([Id]),
        CONSTRAINT [FK_RiskEvents_ProductionNodes_OrderId_NodeId] FOREIGN KEY ([OrderId], [NodeId]) REFERENCES [ProductionNodes] ([OrderId], [Id]),
        CONSTRAINT [FK_RiskEvents_ProductionOrders_OrderId] FOREIGN KEY ([OrderId]) REFERENCES [ProductionOrders] ([Id]) ON DELETE CASCADE
    );
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260710025438_InitialCreate'
)
BEGIN
    CREATE UNIQUE INDEX [IX_Partners_Code] ON [Partners] ([Code]);
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260710025438_InitialCreate'
)
BEGIN
    CREATE UNIQUE INDEX [IX_ProductionLines_Code] ON [ProductionLines] ([Code]);
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260710025438_InitialCreate'
)
BEGIN
    CREATE INDEX [IX_ProductionNodes_OrderId_NodeType_RiskLevel_Status] ON [ProductionNodes] ([OrderId], [NodeType], [RiskLevel], [Status]);
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260710025438_InitialCreate'
)
BEGIN
    CREATE INDEX [IX_ProductionNodes_OrderId_ParentNodeId_SortOrder] ON [ProductionNodes] ([OrderId], [ParentNodeId], [SortOrder]);
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260710025438_InitialCreate'
)
BEGIN
    CREATE UNIQUE INDEX [IX_ProductionOrders_Code] ON [ProductionOrders] ([Code]);
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260710025438_InitialCreate'
)
BEGIN
    CREATE INDEX [IX_ProductionOrders_LineId] ON [ProductionOrders] ([LineId]);
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260710025438_InitialCreate'
)
BEGIN
    CREATE INDEX [IX_ProductionOrders_Status_RiskLevel_PlannedEnd] ON [ProductionOrders] ([Status], [RiskLevel], [PlannedEnd]);
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260710025438_InitialCreate'
)
BEGIN
    CREATE INDEX [IX_ProductionTrendPoints_OrderId_SampleTime] ON [ProductionTrendPoints] ([OrderId], [SampleTime] DESC);
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260710025438_InitialCreate'
)
BEGIN
    CREATE INDEX [IX_RiskEvents_OrderId_NodeId] ON [RiskEvents] ([OrderId], [NodeId]);
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260710025438_InitialCreate'
)
BEGIN
    CREATE INDEX [IX_RiskEvents_OrderId_OccurredAt] ON [RiskEvents] ([OrderId], [OccurredAt] DESC);
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260710025438_InitialCreate'
)
BEGIN
    INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
    VALUES (N'20260710025438_InitialCreate', N'8.0.20');
END;
GO

COMMIT;
GO
