using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace esuite.Database.SqlServer.Migrations
{
    /// <inheritdoc />
    public partial class ActivityTables : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.EnsureSchema(
                name: "Activity");

            migrationBuilder.CreateTable(
                name: "Activities",
                schema: "Activity",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Guid = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    Name = table.Column<string>(type: "nvarchar(450)", nullable: false),
                    WorkflowVersionId = table.Column<long>(type: "bigint", nullable: false),
                    ActivityState = table.Column<int>(type: "int", nullable: false),
                    Deleted = table.Column<bool>(type: "bit", nullable: false),
                    LastUpdated = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Activities", x => x.Id);
                    table.ForeignKey(
                        name: "FK_Activities_WorkflowVersions_WorkflowVersionId",
                        column: x => x.WorkflowVersionId,
                        principalSchema: "Workflow",
                        principalTable: "WorkflowVersions",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "ActivityTasks",
                schema: "Activity",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Guid = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    ParentId = table.Column<long>(type: "bigint", nullable: true),
                    ActivityId = table.Column<long>(type: "bigint", nullable: false),
                    ActivityOrdinal = table.Column<long>(type: "bigint", nullable: false),
                    TaskGuid = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    TaskName = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    ActivityState = table.Column<int>(type: "int", nullable: false),
                    StartDateTime = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: true),
                    FinishDateTime = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: true),
                    Deleted = table.Column<bool>(type: "bit", nullable: false),
                    LastUpdated = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ActivityTasks", x => x.Id);
                    table.ForeignKey(
                        name: "FK_ActivityTasks_Activities_ActivityId",
                        column: x => x.ActivityId,
                        principalSchema: "Activity",
                        principalTable: "Activities",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_ActivityTasks_ActivityTasks_ParentId",
                        column: x => x.ParentId,
                        principalSchema: "Activity",
                        principalTable: "ActivityTasks",
                        principalColumn: "Id");
                });

            migrationBuilder.CreateIndex(
                name: "IX_Activities_Guid",
                schema: "Activity",
                table: "Activities",
                column: "Guid",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_Activities_Name",
                schema: "Activity",
                table: "Activities",
                column: "Name",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_Activities_WorkflowVersionId",
                schema: "Activity",
                table: "Activities",
                column: "WorkflowVersionId");

            migrationBuilder.CreateIndex(
                name: "IX_ActivityTasks_ActivityId_ActivityOrdinal",
                schema: "Activity",
                table: "ActivityTasks",
                columns: new[] { "ActivityId", "ActivityOrdinal" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_ActivityTasks_Guid",
                schema: "Activity",
                table: "ActivityTasks",
                column: "Guid",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_ActivityTasks_ParentId",
                schema: "Activity",
                table: "ActivityTasks",
                column: "ParentId");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "ActivityTasks",
                schema: "Activity");

            migrationBuilder.DropTable(
                name: "Activities",
                schema: "Activity");
        }
    }
}
