using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Jiaowu.Api.Infrastructure.Persistence.Migrations.MySql;

public partial class SplitCentralizedExperimentProjectsByWeek : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        ExecuteWhenMissing(
            migrationBuilder,
            "COLUMNS",
            "COLUMN_NAME = 'ScheduleWeek'",
            "ALTER TABLE `ExperimentProjects` ADD COLUMN `ScheduleWeek` int NULL");

        ExecuteWhenPresent(
            migrationBuilder,
            "STATISTICS",
            "INDEX_NAME = 'IX_ExperimentProjects_TeachingTaskId_Code_ScheduleEntryId'",
            "DROP INDEX `IX_ExperimentProjects_TeachingTaskId_Code_ScheduleEntryId` ON `ExperimentProjects`");

        ExecuteWhenMissing(
            migrationBuilder,
            "STATISTICS",
            "INDEX_NAME = 'IX_ExpProj_Task_Code_Entry_Week'",
            "CREATE UNIQUE INDEX `IX_ExpProj_Task_Code_Entry_Week` ON `ExperimentProjects` (`TeachingTaskId`, `Code`, `ScheduleEntryId`, `ScheduleWeek`)");
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropIndex(
            name: "IX_ExpProj_Task_Code_Entry_Week",
            table: "ExperimentProjects");

        migrationBuilder.CreateIndex(
            name: "IX_ExperimentProjects_TeachingTaskId_Code_ScheduleEntryId",
            table: "ExperimentProjects",
            columns: new[] { "TeachingTaskId", "Code", "ScheduleEntryId" },
            unique: true);

        migrationBuilder.DropColumn(
            name: "ScheduleWeek",
            table: "ExperimentProjects");
    }

    private static void ExecuteWhenMissing(
        MigrationBuilder migrationBuilder,
        string informationSchemaTable,
        string condition,
        string command)
    {
        ExecuteConditionally(migrationBuilder, informationSchemaTable, condition, command, "= 0");
    }

    private static void ExecuteWhenPresent(
        MigrationBuilder migrationBuilder,
        string informationSchemaTable,
        string condition,
        string command)
    {
        ExecuteConditionally(migrationBuilder, informationSchemaTable, condition, command, "> 0");
    }

    private static void ExecuteConditionally(
        MigrationBuilder migrationBuilder,
        string informationSchemaTable,
        string condition,
        string command,
        string comparison)
    {
        migrationBuilder.Sql($"SET @jiaowu_exists = (SELECT COUNT(*) FROM `information_schema`.`{informationSchemaTable}` WHERE `TABLE_SCHEMA` = DATABASE() AND `TABLE_NAME` = 'ExperimentProjects' AND {condition})");
        migrationBuilder.Sql($"SET @jiaowu_sql = IF(@jiaowu_exists {comparison}, '{command}', 'SELECT 1')");
        migrationBuilder.Sql("PREPARE jiaowu_migration_statement FROM @jiaowu_sql");
        migrationBuilder.Sql("EXECUTE jiaowu_migration_statement");
        migrationBuilder.Sql("DEALLOCATE PREPARE jiaowu_migration_statement");
    }
}
