using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace DodoSSH.Infrastructure.Migrations
{
    /// <summary>
    /// Adds the host group and snippet item tables, and drops the host column groups were once going to use.
    /// </summary>
    /// <remarks>
    /// <para>
    /// <c>host.group_id</c> was reserved by the initial schema and never written: the only shipped client
    /// builds its plaintext fields in one place, and that place has never set a group on any branch. So the
    /// drop cannot lose data, and the tooling's data-loss warning is answered by that fact rather than by a
    /// backup. Group membership now travels inside the host's encrypted payload; see <c>VaultHostGroup</c>.
    /// </para>
    /// <para>
    /// <c>Down</c> restores the column, not its contents, which is the honest shape for a reversal of a column
    /// that was always NULL — there is nothing to restore.
    /// </para>
    /// </remarks>
    public partial class AddHostGroupAndSnippetItems : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(
                name: "group_id",
                schema: "dodo",
                table: "host");

            migrationBuilder.CreateTable(
                name: "host_group",
                schema: "dodo",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false),
                    vault_id = table.Column<Guid>(type: "uuid", nullable: false),
                    payload = table.Column<byte[]>(type: "bytea", nullable: false),
                    data_key_wrap = table.Column<byte[]>(type: "bytea", nullable: true),
                    content_key_id = table.Column<Guid>(type: "uuid", nullable: true),
                    key_generation = table.Column<int>(type: "integer", nullable: false),
                    payload_aad_version = table.Column<short>(type: "smallint", nullable: false),
                    version = table.Column<int>(type: "integer", nullable: false),
                    change_sequence = table.Column<long>(type: "bigint", nullable: false),
                    created_at_utc = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    updated_at_utc = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    deleted_at_utc = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
                    created_by_user_id = table.Column<Guid>(type: "uuid", nullable: false),
                    updated_by_user_id = table.Column<Guid>(type: "uuid", nullable: false),
                    xmin = table.Column<uint>(type: "xid", rowVersion: true, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("pk_host_group", x => x.id);
                    table.CheckConstraint("ck_host_group_version", "version >= 1");
                    table.ForeignKey(
                        name: "fk_host_group_vaults_vault_id",
                        column: x => x.vault_id,
                        principalSchema: "dodo",
                        principalTable: "vault",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "snippet",
                schema: "dodo",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false),
                    vault_id = table.Column<Guid>(type: "uuid", nullable: false),
                    payload = table.Column<byte[]>(type: "bytea", nullable: false),
                    data_key_wrap = table.Column<byte[]>(type: "bytea", nullable: true),
                    content_key_id = table.Column<Guid>(type: "uuid", nullable: true),
                    key_generation = table.Column<int>(type: "integer", nullable: false),
                    payload_aad_version = table.Column<short>(type: "smallint", nullable: false),
                    version = table.Column<int>(type: "integer", nullable: false),
                    change_sequence = table.Column<long>(type: "bigint", nullable: false),
                    created_at_utc = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    updated_at_utc = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    deleted_at_utc = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
                    created_by_user_id = table.Column<Guid>(type: "uuid", nullable: false),
                    updated_by_user_id = table.Column<Guid>(type: "uuid", nullable: false),
                    xmin = table.Column<uint>(type: "xid", rowVersion: true, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("pk_snippet", x => x.id);
                    table.CheckConstraint("ck_snippet_version", "version >= 1");
                    table.ForeignKey(
                        name: "fk_snippet_vaults_vault_id",
                        column: x => x.vault_id,
                        principalSchema: "dodo",
                        principalTable: "vault",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "ix_host_group_vault_id_change_sequence",
                schema: "dodo",
                table: "host_group",
                columns: new[] { "vault_id", "change_sequence" });

            migrationBuilder.CreateIndex(
                name: "ix_host_group_vault_live",
                schema: "dodo",
                table: "host_group",
                column: "vault_id",
                filter: "deleted_at_utc IS NULL");

            migrationBuilder.CreateIndex(
                name: "ix_snippet_vault_id_change_sequence",
                schema: "dodo",
                table: "snippet",
                columns: new[] { "vault_id", "change_sequence" });

            migrationBuilder.CreateIndex(
                name: "ix_snippet_vault_live",
                schema: "dodo",
                table: "snippet",
                column: "vault_id",
                filter: "deleted_at_utc IS NULL");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "host_group",
                schema: "dodo");

            migrationBuilder.DropTable(
                name: "snippet",
                schema: "dodo");

            migrationBuilder.AddColumn<Guid>(
                name: "group_id",
                schema: "dodo",
                table: "host",
                type: "uuid",
                nullable: true);
        }
    }
}
