-- CreateTable
CREATE TABLE "backup_shares" (
    "share_id" BIGSERIAL NOT NULL,
    "user_id" BIGINT NOT NULL,
    "account_sequence" BIGINT NOT NULL,
    "public_key" VARCHAR(130) NOT NULL,
    "party_index" INTEGER NOT NULL DEFAULT 2,
    "threshold" INTEGER NOT NULL DEFAULT 2,
    "total_parties" INTEGER NOT NULL DEFAULT 3,
    "encrypted_share_data" TEXT NOT NULL,
    "encryption_key_id" VARCHAR(64) NOT NULL,
    "status" VARCHAR(20) NOT NULL DEFAULT 'ACTIVE',
    "access_count" INTEGER NOT NULL DEFAULT 0,
    "last_accessed_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,
    "revoked_at" TIMESTAMP(3),

    CONSTRAINT "backup_shares_pkey" PRIMARY KEY ("share_id")
);

-- CreateTable
CREATE TABLE "share_access_logs" (
    "log_id" BIGSERIAL NOT NULL,
    "share_id" BIGINT NOT NULL,
    "user_id" BIGINT NOT NULL,
    "action" VARCHAR(20) NOT NULL,
    "source_service" VARCHAR(50) NOT NULL,
    "source_ip" VARCHAR(45) NOT NULL,
    "success" BOOLEAN NOT NULL DEFAULT true,
    "error_message" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "share_access_logs_pkey" PRIMARY KEY ("log_id")
);

-- CreateIndex
CREATE UNIQUE INDEX "backup_shares_user_id_key" ON "backup_shares"("user_id");

-- CreateIndex
CREATE UNIQUE INDEX "backup_shares_account_sequence_key" ON "backup_shares"("account_sequence");

-- CreateIndex
CREATE UNIQUE INDEX "backup_shares_public_key_key" ON "backup_shares"("public_key");

-- CreateIndex
CREATE INDEX "idx_backup_public_key" ON "backup_shares"("public_key");

-- CreateIndex
CREATE INDEX "idx_backup_status" ON "backup_shares"("status");

-- CreateIndex
CREATE INDEX "idx_backup_created" ON "backup_shares"("created_at");

-- CreateIndex
CREATE INDEX "idx_log_share" ON "share_access_logs"("share_id");

-- CreateIndex
CREATE INDEX "idx_log_user" ON "share_access_logs"("user_id");

-- CreateIndex
CREATE INDEX "idx_log_action" ON "share_access_logs"("action");

-- CreateIndex
CREATE INDEX "idx_log_created" ON "share_access_logs"("created_at");
