using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace MilitaryTrainingManagement.Migrations
{
    /// <inheritdoc />
    public partial class AddConsumptionReportChangeRequests : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<int>(
                name: "PendingUpgradeByUnitId",
                table: "Personnel",
                type: "int",
                nullable: true);

            migrationBuilder.AddColumn<int>(
                name: "ReportedByUnitId",
                table: "ConsumptionReports",
                type: "int",
                nullable: false,
                defaultValue: 0);

            migrationBuilder.CreateTable(
                name: "ConsumptionReportChangeRequests",
                columns: table => new
                {
                    Id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    ConsumptionReportId = table.Column<int>(type: "int", nullable: false),
                    RequestType = table.Column<int>(type: "int", nullable: false),
                    Reason = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: false),
                    Status = table.Column<int>(type: "int", nullable: false),
                    RequestedByUnitId = table.Column<int>(type: "int", nullable: false),
                    RequestedByUserId = table.Column<int>(type: "int", nullable: false),
                    RequestedAt = table.Column<DateTime>(type: "datetime2", nullable: false),
                    ProcessedByUnitId = table.Column<int>(type: "int", nullable: true),
                    ProcessedByUserId = table.Column<int>(type: "int", nullable: true),
                    ProcessedAt = table.Column<DateTime>(type: "datetime2", nullable: true),
                    ProcessComments = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ConsumptionReportChangeRequests", x => x.Id);
                    table.ForeignKey(
                        name: "FK_ConsumptionReportChangeRequests_ConsumptionReports_ConsumptionReportId",
                        column: x => x.ConsumptionReportId,
                        principalTable: "ConsumptionReports",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_ConsumptionReportChangeRequests_OrganizationalUnits_ProcessedByUnitId",
                        column: x => x.ProcessedByUnitId,
                        principalTable: "OrganizationalUnits",
                        principalColumn: "Id");
                    table.ForeignKey(
                        name: "FK_ConsumptionReportChangeRequests_OrganizationalUnits_RequestedByUnitId",
                        column: x => x.RequestedByUnitId,
                        principalTable: "OrganizationalUnits",
                        principalColumn: "Id");
                    table.ForeignKey(
                        name: "FK_ConsumptionReportChangeRequests_UserAccounts_ProcessedByUserId",
                        column: x => x.ProcessedByUserId,
                        principalTable: "UserAccounts",
                        principalColumn: "Id");
                    table.ForeignKey(
                        name: "FK_ConsumptionReportChangeRequests_UserAccounts_RequestedByUserId",
                        column: x => x.RequestedByUserId,
                        principalTable: "UserAccounts",
                        principalColumn: "Id");
                });

            migrationBuilder.CreateIndex(
                name: "IX_Personnel_PendingUpgradeByUnitId",
                table: "Personnel",
                column: "PendingUpgradeByUnitId");

            migrationBuilder.CreateIndex(
                name: "IX_ConsumptionReports_ReportedByUnitId",
                table: "ConsumptionReports",
                column: "ReportedByUnitId");

            migrationBuilder.CreateIndex(
                name: "IX_ConsumptionReportChangeRequests_ConsumptionReportId",
                table: "ConsumptionReportChangeRequests",
                column: "ConsumptionReportId");

            migrationBuilder.CreateIndex(
                name: "IX_ConsumptionReportChangeRequests_ProcessedByUnitId",
                table: "ConsumptionReportChangeRequests",
                column: "ProcessedByUnitId");

            migrationBuilder.CreateIndex(
                name: "IX_ConsumptionReportChangeRequests_ProcessedByUserId",
                table: "ConsumptionReportChangeRequests",
                column: "ProcessedByUserId");

            migrationBuilder.CreateIndex(
                name: "IX_ConsumptionReportChangeRequests_RequestedByUnitId",
                table: "ConsumptionReportChangeRequests",
                column: "RequestedByUnitId");

            migrationBuilder.CreateIndex(
                name: "IX_ConsumptionReportChangeRequests_RequestedByUserId",
                table: "ConsumptionReportChangeRequests",
                column: "RequestedByUserId");

            migrationBuilder.CreateIndex(
                name: "IX_ConsumptionReportChangeRequests_Status",
                table: "ConsumptionReportChangeRequests",
                column: "Status");

            migrationBuilder.AddForeignKey(
                name: "FK_ConsumptionReports_OrganizationalUnits_ReportedByUnitId",
                table: "ConsumptionReports",
                column: "ReportedByUnitId",
                principalTable: "OrganizationalUnits",
                principalColumn: "Id");

            migrationBuilder.AddForeignKey(
                name: "FK_Personnel_OrganizationalUnits_PendingUpgradeByUnitId",
                table: "Personnel",
                column: "PendingUpgradeByUnitId",
                principalTable: "OrganizationalUnits",
                principalColumn: "Id");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_ConsumptionReports_OrganizationalUnits_ReportedByUnitId",
                table: "ConsumptionReports");

            migrationBuilder.DropForeignKey(
                name: "FK_Personnel_OrganizationalUnits_PendingUpgradeByUnitId",
                table: "Personnel");

            migrationBuilder.DropTable(
                name: "ConsumptionReportChangeRequests");

            migrationBuilder.DropIndex(
                name: "IX_Personnel_PendingUpgradeByUnitId",
                table: "Personnel");

            migrationBuilder.DropIndex(
                name: "IX_ConsumptionReports_ReportedByUnitId",
                table: "ConsumptionReports");

            migrationBuilder.DropColumn(
                name: "PendingUpgradeByUnitId",
                table: "Personnel");

            migrationBuilder.DropColumn(
                name: "ReportedByUnitId",
                table: "ConsumptionReports");
        }
    }
}
