using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace DodoSSH.Infrastructure.Migrations
{
    /// <inheritdoc />
    public partial class AddSshKeyItem : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "ssh_key",
                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),
                    public_key_fingerprint = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: true),
                    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_ssh_key", x => x.id);
                    table.CheckConstraint("ck_ssh_key_version", "version >= 1");
                    table.ForeignKey(
                        name: "fk_ssh_key_vaults_vault_id",
                        column: x => x.vault_id,
                        principalSchema: "dodo",
                        principalTable: "vault",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                });

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

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

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