using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace songrequests_blazor.Migrations
{
    /// <inheritdoc />
    public partial class AddQueueDataAndAdminUser : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.RenameColumn(
                name: "SourceUri",
                table: "Songs",
                newName: "Uri");

            migrationBuilder.RenameColumn(
                name: "Source",
                table: "Songs",
                newName: "RequestCount");

            migrationBuilder.AddColumn<int>(
                name: "Library_ID",
                table: "Songs",
                type: "INTEGER",
                nullable: false,
                defaultValue: 0);

            migrationBuilder.AddColumn<string>(
                name: "Discriminator",
                table: "AspNetUsers",
                type: "TEXT",
                maxLength: 13,
                nullable: false,
                defaultValue: "");

            migrationBuilder.CreateTable(
                name: "Libraries",
                columns: table => new
                {
                    Id = table.Column<int>(type: "INTEGER", nullable: false)
                        .Annotation("Sqlite:Autoincrement", true),
                    Name = table.Column<string>(type: "TEXT", nullable: false),
                    Type = table.Column<int>(type: "INTEGER", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Libraries", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "Sessions",
                columns: table => new
                {
                    Id = table.Column<int>(type: "INTEGER", nullable: false)
                        .Annotation("Sqlite:Autoincrement", true),
                    SessionCode = table.Column<string>(type: "TEXT", nullable: true),
                    Created = table.Column<DateTime>(type: "TEXT", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Sessions", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "QueuedSongs",
                columns: table => new
                {
                    Id = table.Column<int>(type: "INTEGER", nullable: false)
                        .Annotation("Sqlite:Autoincrement", true),
                    Position = table.Column<int>(type: "INTEGER", nullable: false),
                    Song_ID = table.Column<int>(type: "INTEGER", nullable: false),
                    AddedBy_User_ID = table.Column<string>(type: "TEXT", nullable: true),
                    Added = table.Column<DateTime>(type: "TEXT", nullable: false),
                    Played = table.Column<DateTime>(type: "TEXT", nullable: true),
                    Session_ID = table.Column<int>(type: "INTEGER", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_QueuedSongs", x => x.Id);
                    table.ForeignKey(
                        name: "FK_Session_QueuedSongs",
                        column: x => x.Session_ID,
                        principalTable: "Sessions",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_queuedsongs_song",
                        column: x => x.Song_ID,
                        principalTable: "Songs",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_user_addedsongs",
                        column: x => x.AddedBy_User_ID,
                        principalTable: "AspNetUsers",
                        principalColumn: "Id");
                });

            migrationBuilder.CreateTable(
                name: "Sessions_Libraries",
                columns: table => new
                {
                    EnabledLibrariesId = table.Column<int>(type: "INTEGER", nullable: false),
                    UsedInSessionsId = table.Column<int>(type: "INTEGER", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Sessions_Libraries", x => new { x.EnabledLibrariesId, x.UsedInSessionsId });
                    table.ForeignKey(
                        name: "FK_Sessions_Libraries_Libraries_EnabledLibrariesId",
                        column: x => x.EnabledLibrariesId,
                        principalTable: "Libraries",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_Sessions_Libraries_Sessions_UsedInSessionsId",
                        column: x => x.UsedInSessionsId,
                        principalTable: "Sessions",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "QueuedSongs_UserVotes",
                columns: table => new
                {
                    UserVotesId = table.Column<string>(type: "TEXT", nullable: false),
                    VotedSongsId = table.Column<int>(type: "INTEGER", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_QueuedSongs_UserVotes", x => new { x.UserVotesId, x.VotedSongsId });
                    table.ForeignKey(
                        name: "FK_QueuedSongs_UserVotes_AspNetUsers_UserVotesId",
                        column: x => x.UserVotesId,
                        principalTable: "AspNetUsers",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_QueuedSongs_UserVotes_QueuedSongs_VotedSongsId",
                        column: x => x.VotedSongsId,
                        principalTable: "QueuedSongs",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.InsertData(
                table: "AspNetRoles",
                columns: new[] { "Id", "ConcurrencyStamp", "Name", "NormalizedName" },
                values: new object[] { "fc985a25-19ce-48ed-8443-c845f550960a", null, "Administrator", "ADMINISTRATOR" });

            migrationBuilder.InsertData(
                table: "AspNetUsers",
                columns: new[] { "Id", "AccessFailedCount", "ConcurrencyStamp", "Discriminator", "Email", "EmailConfirmed", "LockoutEnabled", "LockoutEnd", "NormalizedEmail", "NormalizedUserName", "PasswordHash", "PhoneNumber", "PhoneNumberConfirmed", "SecurityStamp", "TwoFactorEnabled", "UserName" },
                values: new object[] { "5c2511d0-6a08-4950-babd-d107996749ba", 0, "828466b4-e932-48c2-9ac5-2d5a16bbbade", "IdentityUser", "admin@admin.com", false, false, null, "ADMIN@ADMIN.COM", "ADMIN@ADMIN.COM", "AQAAAAIAAYagAAAAEAHO/Ra7WSxWtOXx++pYevyMsD+Wt/8tsCfJtQ59NaWFxF7BztGzvgNrnblXuYOkww==", null, false, "23975ce5-b515-4a6a-a6b7-2a5e6d8c943e", false, "admin@admin.com" });

            migrationBuilder.InsertData(
                table: "AspNetUserRoles",
                columns: new[] { "RoleId", "UserId" },
                values: new object[] { "fc985a25-19ce-48ed-8443-c845f550960a", "5c2511d0-6a08-4950-babd-d107996749ba" });

            migrationBuilder.CreateIndex(
                name: "IX_Songs_Library_ID",
                table: "Songs",
                column: "Library_ID");

            migrationBuilder.CreateIndex(
                name: "IX_QueuedSongs_AddedBy_User_ID",
                table: "QueuedSongs",
                column: "AddedBy_User_ID");

            migrationBuilder.CreateIndex(
                name: "IX_QueuedSongs_Session_ID",
                table: "QueuedSongs",
                column: "Session_ID");

            migrationBuilder.CreateIndex(
                name: "IX_QueuedSongs_Song_ID",
                table: "QueuedSongs",
                column: "Song_ID");

            migrationBuilder.CreateIndex(
                name: "IX_QueuedSongs_UserVotes_VotedSongsId",
                table: "QueuedSongs_UserVotes",
                column: "VotedSongsId");

            migrationBuilder.CreateIndex(
                name: "IX_Sessions_Libraries_UsedInSessionsId",
                table: "Sessions_Libraries",
                column: "UsedInSessionsId");

            migrationBuilder.AddForeignKey(
                name: "FK_library_songs",
                table: "Songs",
                column: "Library_ID",
                principalTable: "Libraries",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
        }

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

            migrationBuilder.DropTable(
                name: "QueuedSongs_UserVotes");

            migrationBuilder.DropTable(
                name: "Sessions_Libraries");

            migrationBuilder.DropTable(
                name: "QueuedSongs");

            migrationBuilder.DropTable(
                name: "Libraries");

            migrationBuilder.DropTable(
                name: "Sessions");

            migrationBuilder.DropIndex(
                name: "IX_Songs_Library_ID",
                table: "Songs");

            migrationBuilder.DeleteData(
                table: "AspNetUserRoles",
                keyColumns: new[] { "RoleId", "UserId" },
                keyValues: new object[] { "fc985a25-19ce-48ed-8443-c845f550960a", "5c2511d0-6a08-4950-babd-d107996749ba" });

            migrationBuilder.DeleteData(
                table: "AspNetRoles",
                keyColumn: "Id",
                keyValue: "fc985a25-19ce-48ed-8443-c845f550960a");

            migrationBuilder.DeleteData(
                table: "AspNetUsers",
                keyColumn: "Id",
                keyValue: "5c2511d0-6a08-4950-babd-d107996749ba");

            migrationBuilder.DropColumn(
                name: "Library_ID",
                table: "Songs");

            migrationBuilder.DropColumn(
                name: "Discriminator",
                table: "AspNetUsers");

            migrationBuilder.RenameColumn(
                name: "Uri",
                table: "Songs",
                newName: "SourceUri");

            migrationBuilder.RenameColumn(
                name: "RequestCount",
                table: "Songs",
                newName: "Source");
        }
    }
}
