using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Elecciones.Database.Migrations
{
    /// <inheritdoc />
    public partial class AddBancadasAndOcupantesSchema : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_OcupantesBancas_ComposicionBancadas_ComposicionBancadaId",
                table: "OcupantesBancas");

            migrationBuilder.DropTable(
                name: "ComposicionBancadas");

            migrationBuilder.DropIndex(
                name: "IX_OcupantesBancas_ComposicionBancadaId",
                table: "OcupantesBancas");

            migrationBuilder.RenameColumn(
                name: "ComposicionBancadaId",
                table: "OcupantesBancas",
                newName: "BancadaId");

            migrationBuilder.AlterColumn<string>(
                name: "Periodo",
                table: "OcupantesBancas",
                type: "nvarchar(max)",
                nullable: true,
                oldClrType: typeof(string),
                oldType: "nvarchar(max)");

            migrationBuilder.AlterColumn<string>(
                name: "FotoUrl",
                table: "OcupantesBancas",
                type: "nvarchar(max)",
                nullable: true,
                oldClrType: typeof(string),
                oldType: "nvarchar(max)");

            migrationBuilder.CreateTable(
                name: "Bancadas",
                columns: table => new
                {
                    Id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Camara = table.Column<int>(type: "int", nullable: false),
                    AgrupacionPoliticaId = table.Column<string>(type: "nvarchar(450)", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Bancadas", x => x.Id);
                    table.ForeignKey(
                        name: "FK_Bancadas_AgrupacionesPoliticas_AgrupacionPoliticaId",
                        column: x => x.AgrupacionPoliticaId,
                        principalTable: "AgrupacionesPoliticas",
                        principalColumn: "Id");
                });

            migrationBuilder.CreateIndex(
                name: "IX_OcupantesBancas_BancadaId",
                table: "OcupantesBancas",
                column: "BancadaId",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_Bancadas_AgrupacionPoliticaId",
                table: "Bancadas",
                column: "AgrupacionPoliticaId");

            migrationBuilder.AddForeignKey(
                name: "FK_OcupantesBancas_Bancadas_BancadaId",
                table: "OcupantesBancas",
                column: "BancadaId",
                principalTable: "Bancadas",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
        }

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

            migrationBuilder.DropTable(
                name: "Bancadas");

            migrationBuilder.DropIndex(
                name: "IX_OcupantesBancas_BancadaId",
                table: "OcupantesBancas");

            migrationBuilder.RenameColumn(
                name: "BancadaId",
                table: "OcupantesBancas",
                newName: "ComposicionBancadaId");

            migrationBuilder.AlterColumn<string>(
                name: "Periodo",
                table: "OcupantesBancas",
                type: "nvarchar(max)",
                nullable: false,
                defaultValue: "",
                oldClrType: typeof(string),
                oldType: "nvarchar(max)",
                oldNullable: true);

            migrationBuilder.AlterColumn<string>(
                name: "FotoUrl",
                table: "OcupantesBancas",
                type: "nvarchar(max)",
                nullable: false,
                defaultValue: "",
                oldClrType: typeof(string),
                oldType: "nvarchar(max)",
                oldNullable: true);

            migrationBuilder.CreateTable(
                name: "ComposicionBancadas",
                columns: table => new
                {
                    Id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    AgrupacionPoliticaId = table.Column<string>(type: "nvarchar(450)", nullable: false),
                    BancasEnJuego = table.Column<int>(type: "int", nullable: false),
                    BancasTotales = table.Column<int>(type: "int", nullable: false),
                    Camara = table.Column<int>(type: "int", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ComposicionBancadas", x => x.Id);
                    table.ForeignKey(
                        name: "FK_ComposicionBancadas_AgrupacionesPoliticas_AgrupacionPoliticaId",
                        column: x => x.AgrupacionPoliticaId,
                        principalTable: "AgrupacionesPoliticas",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "IX_OcupantesBancas_ComposicionBancadaId",
                table: "OcupantesBancas",
                column: "ComposicionBancadaId");

            migrationBuilder.CreateIndex(
                name: "IX_ComposicionBancadas_AgrupacionPoliticaId",
                table: "ComposicionBancadas",
                column: "AgrupacionPoliticaId");

            migrationBuilder.AddForeignKey(
                name: "FK_OcupantesBancas_ComposicionBancadas_ComposicionBancadaId",
                table: "OcupantesBancas",
                column: "ComposicionBancadaId",
                principalTable: "ComposicionBancadas",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
        }
    }
}
