// <auto-generated />
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace AliasServerDb.Migrations
{
    /// <inheritdoc />
    public partial class MoveSrpColsToVaultTable : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<string>(
                name: "Salt",
                table: "Vaults",
                type: "TEXT",
                maxLength: 100,
                nullable: false,
                defaultValue: "");

            migrationBuilder.AddColumn<string>(
                name: "Verifier",
                table: "Vaults",
                type: "TEXT",
                maxLength: 1000,
                nullable: false,
                defaultValue: "");

            // Transfer data from AliasVaultUsers to Vaults
            migrationBuilder.Sql(@"
            UPDATE Vaults
            SET Salt = (SELECT Salt FROM AliasVaultUsers WHERE AliasVaultUsers.Id = Vaults.UserId),
                Verifier = (SELECT Verifier FROM AliasVaultUsers WHERE AliasVaultUsers.Id = Vaults.UserId)
            WHERE EXISTS (SELECT 1 FROM AliasVaultUsers WHERE AliasVaultUsers.Id = Vaults.UserId);
        ");

            // Create new vault entries for users without existing vaults
            migrationBuilder.Sql(@"
            INSERT INTO Vaults (UserId, VaultBlob, Version, Salt, Verifier, CreatedAt, UpdatedAt)
            SELECT
                Id,
                '',
                '0.0.0',
                Salt,
                Verifier,
                DATETIME('now'),
                DATETIME('now')
            FROM AliasVaultUsers
            WHERE Id NOT IN (SELECT DISTINCT UserId FROM Vaults);
        ");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(
                name: "Salt",
                table: "Vaults");

            migrationBuilder.DropColumn(
                name: "Verifier",
                table: "Vaults");
        }
    }
}
