using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;

#nullable disable

namespace NexusReader.Data.Migrations
{
    /// <inheritdoc />
    public partial class NormalizeAuthor : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(
                name: "Author",
                table: "Ebooks");

            migrationBuilder.AddColumn<int>(
                name: "AuthorId",
                table: "Ebooks",
                type: "integer",
                nullable: false,
                defaultValue: 0);

            migrationBuilder.CreateTable(
                name: "Authors",
                columns: table => new
                {
                    Id = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    Name = table.Column<string>(type: "character varying(255)", maxLength: 255, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Authors", x => x.Id);
                });

            migrationBuilder.CreateIndex(
                name: "IX_Ebooks_AuthorId",
                table: "Ebooks",
                column: "AuthorId");

            migrationBuilder.AddForeignKey(
                name: "FK_Ebooks_Authors_AuthorId",
                table: "Ebooks",
                column: "AuthorId",
                principalTable: "Authors",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);
        }

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

            migrationBuilder.DropTable(
                name: "Authors");

            migrationBuilder.DropIndex(
                name: "IX_Ebooks_AuthorId",
                table: "Ebooks");

            migrationBuilder.DropColumn(
                name: "AuthorId",
                table: "Ebooks");

            migrationBuilder.AddColumn<string>(
                name: "Author",
                table: "Ebooks",
                type: "character varying(255)",
                maxLength: 255,
                nullable: false,
                defaultValue: "");
        }
    }
}
