﻿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'20260114141545_AddMaterialCategory'
)
BEGIN
    CREATE TABLE [MaterialCategories] (
        [Id] int NOT NULL IDENTITY,
        [Name] nvarchar(50) NOT NULL,
        [Description] nvarchar(200) NULL,
        [IsActive] bit NOT NULL,
        [CreatedAt] datetime2 NOT NULL,
        [SortOrder] int NOT NULL,
        CONSTRAINT [PK_MaterialCategories] PRIMARY KEY ([Id])
    );
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260114141545_AddMaterialCategory'
)
BEGIN
    CREATE TABLE [OrganizationalUnits] (
        [Id] int NOT NULL IDENTITY,
        [Name] nvarchar(100) NOT NULL,
        [Level] int NOT NULL,
        [ParentId] int NULL,
        [CreatedAt] datetime2 NOT NULL,
        CONSTRAINT [PK_OrganizationalUnits] PRIMARY KEY ([Id]),
        CONSTRAINT [FK_OrganizationalUnits_OrganizationalUnits_ParentId] FOREIGN KEY ([ParentId]) REFERENCES [OrganizationalUnits] ([Id]) ON DELETE NO ACTION
    );
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260114141545_AddMaterialCategory'
)
BEGIN
    CREATE TABLE [MaterialAllocations] (
        [Id] int NOT NULL IDENTITY,
        [Category] nvarchar(100) NOT NULL,
        [MaterialName] nvarchar(200) NOT NULL,
        [Unit] nvarchar(50) NOT NULL,
        [TotalQuota] decimal(18,2) NOT NULL,
        [CreatedByUnitId] int NOT NULL,
        [CreatedAt] datetime2 NOT NULL,
        CONSTRAINT [PK_MaterialAllocations] PRIMARY KEY ([Id]),
        CONSTRAINT [FK_MaterialAllocations_OrganizationalUnits_CreatedByUnitId] FOREIGN KEY ([CreatedByUnitId]) REFERENCES [OrganizationalUnits] ([Id]) ON DELETE NO ACTION
    );
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260114141545_AddMaterialCategory'
)
BEGIN
    CREATE TABLE [Personnel] (
        [Id] int NOT NULL IDENTITY,
        [Name] nvarchar(50) NOT NULL,
        [PhotoPath] nvarchar(max) NULL,
        [Position] nvarchar(100) NOT NULL,
        [Rank] nvarchar(50) NOT NULL,
        [Gender] nvarchar(10) NOT NULL,
        [IdNumber] nvarchar(18) NOT NULL,
        [ProfessionalTitle] nvarchar(max) NULL,
        [EducationLevel] nvarchar(max) NULL,
        [Age] int NOT NULL,
        [Height] decimal(5,2) NULL,
        [ContactInfo] nvarchar(max) NULL,
        [Hometown] nvarchar(max) NULL,
        [TrainingParticipation] nvarchar(max) NULL,
        [Achievements] nvarchar(max) NULL,
        [SupportingDocuments] nvarchar(max) NULL,
        [SubmittedByUnitId] int NOT NULL,
        [ApprovedLevel] int NULL,
        [ApprovedByUnitId] int NULL,
        [Status] int NOT NULL,
        [SubmittedAt] datetime2 NOT NULL,
        [ApprovedAt] datetime2 NULL,
        CONSTRAINT [PK_Personnel] PRIMARY KEY ([Id]),
        CONSTRAINT [FK_Personnel_OrganizationalUnits_ApprovedByUnitId] FOREIGN KEY ([ApprovedByUnitId]) REFERENCES [OrganizationalUnits] ([Id]),
        CONSTRAINT [FK_Personnel_OrganizationalUnits_SubmittedByUnitId] FOREIGN KEY ([SubmittedByUnitId]) REFERENCES [OrganizationalUnits] ([Id])
    );
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260114141545_AddMaterialCategory'
)
BEGIN
    CREATE TABLE [UserAccounts] (
        [Id] int NOT NULL IDENTITY,
        [Username] nvarchar(50) NOT NULL,
        [PasswordHash] nvarchar(max) NOT NULL,
        [DisplayName] nvarchar(100) NOT NULL,
        [OrganizationalUnitId] int NOT NULL,
        [IsActive] bit NOT NULL,
        [CreatedAt] datetime2 NOT NULL,
        [LastLoginAt] datetime2 NULL,
        CONSTRAINT [PK_UserAccounts] PRIMARY KEY ([Id]),
        CONSTRAINT [FK_UserAccounts_OrganizationalUnits_OrganizationalUnitId] FOREIGN KEY ([OrganizationalUnitId]) REFERENCES [OrganizationalUnits] ([Id]) ON DELETE NO ACTION
    );
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260114141545_AddMaterialCategory'
)
BEGIN
    CREATE TABLE [AllocationDistributions] (
        [Id] int NOT NULL IDENTITY,
        [AllocationId] int NOT NULL,
        [TargetUnitId] int NOT NULL,
        [UnitQuota] decimal(18,2) NOT NULL,
        [ActualCompletion] decimal(18,2) NULL,
        [ReportedAt] datetime2 NULL,
        [ReportedByUserId] int NULL,
        [IsApproved] bit NOT NULL,
        [ApprovedAt] datetime2 NULL,
        [ApprovedByUserId] int NULL,
        CONSTRAINT [PK_AllocationDistributions] PRIMARY KEY ([Id]),
        CONSTRAINT [FK_AllocationDistributions_MaterialAllocations_AllocationId] FOREIGN KEY ([AllocationId]) REFERENCES [MaterialAllocations] ([Id]) ON DELETE CASCADE,
        CONSTRAINT [FK_AllocationDistributions_OrganizationalUnits_TargetUnitId] FOREIGN KEY ([TargetUnitId]) REFERENCES [OrganizationalUnits] ([Id]),
        CONSTRAINT [FK_AllocationDistributions_UserAccounts_ApprovedByUserId] FOREIGN KEY ([ApprovedByUserId]) REFERENCES [UserAccounts] ([Id]),
        CONSTRAINT [FK_AllocationDistributions_UserAccounts_ReportedByUserId] FOREIGN KEY ([ReportedByUserId]) REFERENCES [UserAccounts] ([Id])
    );
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260114141545_AddMaterialCategory'
)
BEGIN
    CREATE TABLE [ApprovalRequests] (
        [Id] int NOT NULL IDENTITY,
        [Type] int NOT NULL,
        [TargetEntityId] int NOT NULL,
        [RequestedByUserId] int NOT NULL,
        [RequestedByUnitId] int NOT NULL,
        [Reason] nvarchar(500) NOT NULL,
        [OriginalData] nvarchar(max) NOT NULL,
        [RequestedChanges] nvarchar(max) NOT NULL,
        [Status] int NOT NULL,
        [ReviewedByUserId] int NULL,
        [ReviewComments] nvarchar(max) NULL,
        [RequestedAt] datetime2 NOT NULL,
        [ReviewedAt] datetime2 NULL,
        CONSTRAINT [PK_ApprovalRequests] PRIMARY KEY ([Id]),
        CONSTRAINT [FK_ApprovalRequests_OrganizationalUnits_RequestedByUnitId] FOREIGN KEY ([RequestedByUnitId]) REFERENCES [OrganizationalUnits] ([Id]),
        CONSTRAINT [FK_ApprovalRequests_UserAccounts_RequestedByUserId] FOREIGN KEY ([RequestedByUserId]) REFERENCES [UserAccounts] ([Id]),
        CONSTRAINT [FK_ApprovalRequests_UserAccounts_ReviewedByUserId] FOREIGN KEY ([ReviewedByUserId]) REFERENCES [UserAccounts] ([Id])
    );
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260114141545_AddMaterialCategory'
)
BEGIN
    CREATE TABLE [AuditLogs] (
        [Id] int NOT NULL IDENTITY,
        [EntityType] nvarchar(100) NOT NULL,
        [EntityId] int NOT NULL,
        [Action] nvarchar(50) NOT NULL,
        [Description] nvarchar(500) NULL,
        [OldValues] nvarchar(max) NULL,
        [NewValues] nvarchar(max) NULL,
        [ChangedFields] nvarchar(max) NULL,
        [UserId] int NULL,
        [OrganizationalUnitId] int NULL,
        [Timestamp] datetime2 NOT NULL,
        [IpAddress] nvarchar(50) NULL,
        [UserAgent] nvarchar(500) NULL,
        [RequestPath] nvarchar(500) NULL,
        [IsSuccess] bit NOT NULL,
        [ErrorMessage] nvarchar(2000) NULL,
        CONSTRAINT [PK_AuditLogs] PRIMARY KEY ([Id]),
        CONSTRAINT [FK_AuditLogs_OrganizationalUnits_OrganizationalUnitId] FOREIGN KEY ([OrganizationalUnitId]) REFERENCES [OrganizationalUnits] ([Id]),
        CONSTRAINT [FK_AuditLogs_UserAccounts_UserId] FOREIGN KEY ([UserId]) REFERENCES [UserAccounts] ([Id])
    );
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260114141545_AddMaterialCategory'
)
BEGIN
    CREATE TABLE [PersonnelApprovalHistories] (
        [Id] int NOT NULL IDENTITY,
        [PersonnelId] int NOT NULL,
        [Action] int NOT NULL,
        [PreviousStatus] int NULL,
        [NewStatus] int NOT NULL,
        [PreviousLevel] int NULL,
        [NewLevel] int NULL,
        [ReviewedByUserId] int NOT NULL,
        [ReviewedByUnitId] int NULL,
        [Comments] nvarchar(max) NULL,
        [ReviewedAt] datetime2 NOT NULL,
        CONSTRAINT [PK_PersonnelApprovalHistories] PRIMARY KEY ([Id]),
        CONSTRAINT [FK_PersonnelApprovalHistories_OrganizationalUnits_ReviewedByUnitId] FOREIGN KEY ([ReviewedByUnitId]) REFERENCES [OrganizationalUnits] ([Id]),
        CONSTRAINT [FK_PersonnelApprovalHistories_Personnel_PersonnelId] FOREIGN KEY ([PersonnelId]) REFERENCES [Personnel] ([Id]) ON DELETE CASCADE,
        CONSTRAINT [FK_PersonnelApprovalHistories_UserAccounts_ReviewedByUserId] FOREIGN KEY ([ReviewedByUserId]) REFERENCES [UserAccounts] ([Id])
    );
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260114141545_AddMaterialCategory'
)
BEGIN
    CREATE INDEX [IX_AllocationDistributions_AllocationId] ON [AllocationDistributions] ([AllocationId]);
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260114141545_AddMaterialCategory'
)
BEGIN
    CREATE INDEX [IX_AllocationDistributions_ApprovedByUserId] ON [AllocationDistributions] ([ApprovedByUserId]);
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260114141545_AddMaterialCategory'
)
BEGIN
    CREATE INDEX [IX_AllocationDistributions_ReportedByUserId] ON [AllocationDistributions] ([ReportedByUserId]);
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260114141545_AddMaterialCategory'
)
BEGIN
    CREATE INDEX [IX_AllocationDistributions_TargetUnitId] ON [AllocationDistributions] ([TargetUnitId]);
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260114141545_AddMaterialCategory'
)
BEGIN
    CREATE INDEX [IX_ApprovalRequests_RequestedByUnitId] ON [ApprovalRequests] ([RequestedByUnitId]);
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260114141545_AddMaterialCategory'
)
BEGIN
    CREATE INDEX [IX_ApprovalRequests_RequestedByUserId] ON [ApprovalRequests] ([RequestedByUserId]);
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260114141545_AddMaterialCategory'
)
BEGIN
    CREATE INDEX [IX_ApprovalRequests_ReviewedByUserId] ON [ApprovalRequests] ([ReviewedByUserId]);
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260114141545_AddMaterialCategory'
)
BEGIN
    CREATE INDEX [IX_AuditLogs_Action] ON [AuditLogs] ([Action]);
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260114141545_AddMaterialCategory'
)
BEGIN
    CREATE INDEX [IX_AuditLogs_EntityId] ON [AuditLogs] ([EntityId]);
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260114141545_AddMaterialCategory'
)
BEGIN
    CREATE INDEX [IX_AuditLogs_EntityType] ON [AuditLogs] ([EntityType]);
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260114141545_AddMaterialCategory'
)
BEGIN
    CREATE INDEX [IX_AuditLogs_OrganizationalUnitId] ON [AuditLogs] ([OrganizationalUnitId]);
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260114141545_AddMaterialCategory'
)
BEGIN
    CREATE INDEX [IX_AuditLogs_Timestamp] ON [AuditLogs] ([Timestamp]);
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260114141545_AddMaterialCategory'
)
BEGIN
    CREATE INDEX [IX_AuditLogs_UserId] ON [AuditLogs] ([UserId]);
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260114141545_AddMaterialCategory'
)
BEGIN
    CREATE INDEX [IX_MaterialAllocations_CreatedByUnitId] ON [MaterialAllocations] ([CreatedByUnitId]);
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260114141545_AddMaterialCategory'
)
BEGIN
    CREATE UNIQUE INDEX [IX_MaterialCategories_Name] ON [MaterialCategories] ([Name]);
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260114141545_AddMaterialCategory'
)
BEGIN
    CREATE INDEX [IX_OrganizationalUnits_ParentId] ON [OrganizationalUnits] ([ParentId]);
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260114141545_AddMaterialCategory'
)
BEGIN
    CREATE INDEX [IX_Personnel_ApprovedByUnitId] ON [Personnel] ([ApprovedByUnitId]);
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260114141545_AddMaterialCategory'
)
BEGIN
    CREATE UNIQUE INDEX [IX_Personnel_IdNumber] ON [Personnel] ([IdNumber]);
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260114141545_AddMaterialCategory'
)
BEGIN
    CREATE INDEX [IX_Personnel_SubmittedByUnitId] ON [Personnel] ([SubmittedByUnitId]);
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260114141545_AddMaterialCategory'
)
BEGIN
    CREATE INDEX [IX_PersonnelApprovalHistories_PersonnelId] ON [PersonnelApprovalHistories] ([PersonnelId]);
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260114141545_AddMaterialCategory'
)
BEGIN
    CREATE INDEX [IX_PersonnelApprovalHistories_ReviewedByUnitId] ON [PersonnelApprovalHistories] ([ReviewedByUnitId]);
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260114141545_AddMaterialCategory'
)
BEGIN
    CREATE INDEX [IX_PersonnelApprovalHistories_ReviewedByUserId] ON [PersonnelApprovalHistories] ([ReviewedByUserId]);
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260114141545_AddMaterialCategory'
)
BEGIN
    CREATE INDEX [IX_UserAccounts_OrganizationalUnitId] ON [UserAccounts] ([OrganizationalUnitId]);
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260114141545_AddMaterialCategory'
)
BEGIN
    CREATE UNIQUE INDEX [IX_UserAccounts_Username] ON [UserAccounts] ([Username]);
END;
GO

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

COMMIT;
GO

BEGIN TRANSACTION;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260115063457_UpdatePersonnelFieldsComplete'
)
BEGIN
    DROP INDEX [IX_Personnel_IdNumber] ON [Personnel];
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260115063457_UpdatePersonnelFieldsComplete'
)
BEGIN
    DECLARE @var0 sysname;
    SELECT @var0 = [d].[name]
    FROM [sys].[default_constraints] [d]
    INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id] AND [d].[parent_object_id] = [c].[object_id]
    WHERE ([d].[parent_object_id] = OBJECT_ID(N'[Personnel]') AND [c].[name] = N'IdNumber');
    IF @var0 IS NOT NULL EXEC(N'ALTER TABLE [Personnel] DROP CONSTRAINT [' + @var0 + '];');
    ALTER TABLE [Personnel] ALTER COLUMN [IdNumber] nvarchar(50) NULL;
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260115063457_UpdatePersonnelFieldsComplete'
)
BEGIN
    ALTER TABLE [Personnel] ADD [BirthDate] nvarchar(max) NULL;
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260115063457_UpdatePersonnelFieldsComplete'
)
BEGIN
    ALTER TABLE [Personnel] ADD [EnlistmentDate] nvarchar(max) NULL;
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260115063457_UpdatePersonnelFieldsComplete'
)
BEGIN
    ALTER TABLE [Personnel] ADD [Ethnicity] nvarchar(max) NULL;
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260115063457_UpdatePersonnelFieldsComplete'
)
BEGIN
    ALTER TABLE [Personnel] ADD [PoliticalStatus] nvarchar(max) NULL;
END;
GO

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260115063457_UpdatePersonnelFieldsComplete'
)
BEGIN
    ALTER TABLE [Personnel] ADD [Specialty] nvarchar(max) NULL;
END;
GO

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

COMMIT;
GO

