using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace IRaCIS.Core.Infra.EFCore.Migrations
{
    /// <inheritdoc />
    public partial class EventStore : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_ReadingTableAnswerRowInfo_DicomInstance_InstanceId",
                table: "ReadingTableAnswerRowInfo");

            migrationBuilder.DropForeignKey(
                name: "FK_SubjectCriteriaEvaluationVisitStudyFilter_DicomSeries_SeriesId",
                table: "SubjectCriteriaEvaluationVisitStudyFilter");

            migrationBuilder.DropForeignKey(
                name: "FK_SubjectCriteriaEvaluationVisitStudyFilter_DicomStudy_StudyId",
                table: "SubjectCriteriaEvaluationVisitStudyFilter");

            migrationBuilder.AlterColumn<Guid>(
                name: "JudgeResultTaskId",
                table: "VisitTask",
                type: "uniqueidentifier",
                nullable: true,
                comment: "裁判结果的任务ID（访视或者全局）",
                oldClrType: typeof(Guid),
                oldType: "uniqueidentifier",
                oldNullable: true,
                oldComment: "裁判结果的任务ID");

            migrationBuilder.AddUniqueConstraint(
                name: "AK_DicomInstance_Id",
                table: "DicomInstance",
                column: "Id");

            migrationBuilder.CreateTable(
                name: "EventStoreRecord",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    EventType = table.Column<string>(type: "nvarchar(400)", maxLength: 400, nullable: false),
                    EventData = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    EventState = table.Column<int>(type: "int", nullable: false),
                    CreateUserId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    CreateTime = table.Column<DateTime>(type: "datetime2", nullable: false),
                    UpdateUserId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    UpdateTime = table.Column<DateTime>(type: "datetime2", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_EventStoreRecord", x => x.Id);
                    table.ForeignKey(
                        name: "FK_EventStoreRecord_User_CreateUserId",
                        column: x => x.CreateUserId,
                        principalTable: "User",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                },
                comment: "记录触发的事件，以及状态，从而方便重试操作");

            migrationBuilder.CreateIndex(
                name: "IX_EventStoreRecord_CreateUserId",
                table: "EventStoreRecord",
                column: "CreateUserId");

            migrationBuilder.AddForeignKey(
                name: "FK_ReadingTableAnswerRowInfo_DicomInstance_InstanceId",
                table: "ReadingTableAnswerRowInfo",
                column: "InstanceId",
                principalTable: "DicomInstance",
                principalColumn: "Id");

            migrationBuilder.AddForeignKey(
                name: "FK_SubjectCriteriaEvaluationVisitStudyFilter_DicomSeries_SeriesId",
                table: "SubjectCriteriaEvaluationVisitStudyFilter",
                column: "SeriesId",
                principalTable: "DicomSeries",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_SubjectCriteriaEvaluationVisitStudyFilter_DicomStudy_StudyId",
                table: "SubjectCriteriaEvaluationVisitStudyFilter",
                column: "StudyId",
                principalTable: "DicomStudy",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
        }

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

            migrationBuilder.DropForeignKey(
                name: "FK_SubjectCriteriaEvaluationVisitStudyFilter_DicomSeries_SeriesId",
                table: "SubjectCriteriaEvaluationVisitStudyFilter");

            migrationBuilder.DropForeignKey(
                name: "FK_SubjectCriteriaEvaluationVisitStudyFilter_DicomStudy_StudyId",
                table: "SubjectCriteriaEvaluationVisitStudyFilter");

            migrationBuilder.DropTable(
                name: "EventStoreRecord");

            migrationBuilder.DropUniqueConstraint(
                name: "AK_DicomInstance_Id",
                table: "DicomInstance");

            migrationBuilder.AlterColumn<Guid>(
                name: "JudgeResultTaskId",
                table: "VisitTask",
                type: "uniqueidentifier",
                nullable: true,
                comment: "裁判结果的任务ID",
                oldClrType: typeof(Guid),
                oldType: "uniqueidentifier",
                oldNullable: true,
                oldComment: "裁判结果的任务ID（访视或者全局）");

            migrationBuilder.AddForeignKey(
                name: "FK_ReadingTableAnswerRowInfo_DicomInstance_InstanceId",
                table: "ReadingTableAnswerRowInfo",
                column: "InstanceId",
                principalTable: "DicomInstance",
                principalColumn: "SeqId");

            migrationBuilder.AddForeignKey(
                name: "FK_SubjectCriteriaEvaluationVisitStudyFilter_DicomSeries_SeriesId",
                table: "SubjectCriteriaEvaluationVisitStudyFilter",
                column: "SeriesId",
                principalTable: "DicomSeries",
                principalColumn: "SeqId",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_SubjectCriteriaEvaluationVisitStudyFilter_DicomStudy_StudyId",
                table: "SubjectCriteriaEvaluationVisitStudyFilter",
                column: "StudyId",
                principalTable: "DicomStudy",
                principalColumn: "SeqId",
                onDelete: ReferentialAction.Cascade);
        }
    }
}
