using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace DodoSSH.Client.Storage.Migrations
{
    /// <inheritdoc />
    public partial class InitialCache : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "conflict",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "TEXT", nullable: false),
                    vault_id = table.Column<Guid>(type: "TEXT", nullable: false),
                    entity_type = table.Column<int>(type: "INTEGER", nullable: false),
                    entity_id = table.Column<Guid>(type: "TEXT", nullable: false),
                    kind = table.Column<int>(type: "INTEGER", nullable: false),
                    detail = table.Column<byte[]>(type: "BLOB", nullable: false),
                    detected_at_utc = table.Column<long>(type: "INTEGER", nullable: false),
                    acknowledged = table.Column<bool>(type: "INTEGER", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("pk_conflict", x => x.id);
                });

            migrationBuilder.CreateTable(
                name: "item",
                columns: table => new
                {
                    vault_id = table.Column<Guid>(type: "TEXT", nullable: false),
                    entity_type = table.Column<int>(type: "INTEGER", nullable: false),
                    entity_id = table.Column<Guid>(type: "TEXT", nullable: false),
                    version = table.Column<int>(type: "INTEGER", nullable: false),
                    change_sequence = table.Column<long>(type: "INTEGER", nullable: false),
                    payload = table.Column<byte[]>(type: "BLOB", nullable: true),
                    wrapped_data_key = table.Column<byte[]>(type: "BLOB", nullable: true),
                    data_key_id = table.Column<Guid>(type: "TEXT", nullable: true),
                    key_generation = table.Column<uint>(type: "INTEGER", nullable: false),
                    aad_version = table.Column<byte>(type: "INTEGER", nullable: false),
                    protected_fields = table.Column<byte[]>(type: "BLOB", nullable: true),
                    is_deleted = table.Column<bool>(type: "INTEGER", nullable: false),
                    updated_at_utc = table.Column<long>(type: "INTEGER", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("pk_item", x => new { x.vault_id, x.entity_type, x.entity_id });
                });

            migrationBuilder.CreateTable(
                name: "outbox",
                columns: table => new
                {
                    sequence = table.Column<long>(type: "INTEGER", nullable: false)
                        .Annotation("Sqlite:Autoincrement", true),
                    operation_id = table.Column<Guid>(type: "TEXT", nullable: false),
                    vault_id = table.Column<Guid>(type: "TEXT", nullable: false),
                    entity_type = table.Column<int>(type: "INTEGER", nullable: false),
                    entity_id = table.Column<Guid>(type: "TEXT", nullable: false),
                    operation = table.Column<int>(type: "INTEGER", nullable: false),
                    expected_version = table.Column<int>(type: "INTEGER", nullable: true),
                    payload = table.Column<byte[]>(type: "BLOB", nullable: true),
                    wrapped_data_key = table.Column<byte[]>(type: "BLOB", nullable: true),
                    data_key_id = table.Column<Guid>(type: "TEXT", nullable: true),
                    key_generation = table.Column<uint>(type: "INTEGER", nullable: false),
                    aad_version = table.Column<byte>(type: "INTEGER", nullable: false),
                    protected_fields = table.Column<byte[]>(type: "BLOB", nullable: true),
                    ancestor_version = table.Column<int>(type: "INTEGER", nullable: true),
                    ancestor_payload = table.Column<byte[]>(type: "BLOB", nullable: true),
                    ancestor_wrapped_data_key = table.Column<byte[]>(type: "BLOB", nullable: true),
                    ancestor_data_key_id = table.Column<Guid>(type: "TEXT", nullable: true),
                    ancestor_key_generation = table.Column<uint>(type: "INTEGER", nullable: true),
                    ancestor_aad_version = table.Column<byte>(type: "INTEGER", nullable: true),
                    ancestor_protected_fields = table.Column<byte[]>(type: "BLOB", nullable: true),
                    queued_at_utc = table.Column<long>(type: "INTEGER", nullable: false),
                    attempts = table.Column<int>(type: "INTEGER", nullable: false),
                    last_error = table.Column<string>(type: "TEXT", nullable: true),
                    is_parked = table.Column<bool>(type: "INTEGER", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("pk_outbox", x => x.sequence);
                });

            migrationBuilder.CreateTable(
                name: "sync_state",
                columns: table => new
                {
                    vault_id = table.Column<Guid>(type: "TEXT", nullable: false),
                    cursor = table.Column<string>(type: "TEXT", nullable: true),
                    key_generation = table.Column<uint>(type: "INTEGER", nullable: false),
                    last_pulled_at_utc = table.Column<long>(type: "INTEGER", nullable: true),
                    last_pushed_at_utc = table.Column<long>(type: "INTEGER", nullable: true),
                    server_time_skew_ms = table.Column<long>(type: "INTEGER", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("pk_sync_state", x => x.vault_id);
                });

            migrationBuilder.CreateTable(
                name: "unlock_material",
                columns: table => new
                {
                    id = table.Column<int>(type: "INTEGER", nullable: false),
                    server_url = table.Column<string>(type: "TEXT", nullable: false),
                    user_id = table.Column<Guid>(type: "TEXT", nullable: false),
                    issuer = table.Column<string>(type: "TEXT", nullable: false),
                    subject = table.Column<string>(type: "TEXT", nullable: false),
                    email = table.Column<string>(type: "TEXT", nullable: true),
                    display_name = table.Column<string>(type: "TEXT", nullable: true),
                    key_generation = table.Column<uint>(type: "INTEGER", nullable: false),
                    wrapped_private_key = table.Column<byte[]>(type: "BLOB", nullable: false),
                    kdf_algorithm = table.Column<string>(type: "TEXT", nullable: false),
                    kdf_salt = table.Column<byte[]>(type: "BLOB", nullable: false),
                    kdf_memory_kibibytes = table.Column<int>(type: "INTEGER", nullable: false),
                    kdf_passes = table.Column<int>(type: "INTEGER", nullable: false),
                    kdf_parallelism = table.Column<int>(type: "INTEGER", nullable: false),
                    updated_at_utc = table.Column<long>(type: "INTEGER", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("pk_unlock_material", x => x.id);
                    table.CheckConstraint("ck_unlock_material_singleton", "id = 1");
                });

            migrationBuilder.CreateTable(
                name: "vault",
                columns: table => new
                {
                    vault_id = table.Column<Guid>(type: "TEXT", nullable: false),
                    name = table.Column<string>(type: "TEXT", nullable: false),
                    is_personal = table.Column<bool>(type: "INTEGER", nullable: false),
                    team_id = table.Column<Guid>(type: "TEXT", nullable: true),
                    key_generation = table.Column<uint>(type: "INTEGER", nullable: false),
                    permissions = table.Column<int>(type: "INTEGER", nullable: false),
                    wrapped_vault_key = table.Column<byte[]>(type: "BLOB", nullable: true),
                    rekey_required = table.Column<bool>(type: "INTEGER", nullable: false),
                    updated_at_utc = table.Column<long>(type: "INTEGER", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("pk_vault", x => x.vault_id);
                });

            migrationBuilder.CreateIndex(
                name: "ix_conflict_vault_id_acknowledged",
                table: "conflict",
                columns: new[] { "vault_id", "acknowledged" });

            migrationBuilder.CreateIndex(
                name: "ix_conflict_vault_id_entity_type_entity_id",
                table: "conflict",
                columns: new[] { "vault_id", "entity_type", "entity_id" });

            migrationBuilder.CreateIndex(
                name: "ix_item_vault_id_change_sequence",
                table: "item",
                columns: new[] { "vault_id", "change_sequence" });

            migrationBuilder.CreateIndex(
                name: "ix_item_vault_id_entity_type",
                table: "item",
                columns: new[] { "vault_id", "entity_type" });

            migrationBuilder.CreateIndex(
                name: "ix_outbox_operation_id",
                table: "outbox",
                column: "operation_id",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "ix_outbox_vault_id_entity_type_entity_id",
                table: "outbox",
                columns: new[] { "vault_id", "entity_type", "entity_id" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "ix_outbox_vault_id_is_parked_sequence",
                table: "outbox",
                columns: new[] { "vault_id", "is_parked", "sequence" });
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "conflict");

            migrationBuilder.DropTable(
                name: "item");

            migrationBuilder.DropTable(
                name: "outbox");

            migrationBuilder.DropTable(
                name: "sync_state");

            migrationBuilder.DropTable(
                name: "unlock_material");

            migrationBuilder.DropTable(
                name: "vault");
        }
    }
}
