-- CreateTable
CREATE TABLE "party_shares" (
    "id" VARCHAR(255) NOT NULL,
    "party_id" VARCHAR(255) NOT NULL,
    "session_id" VARCHAR(255) NOT NULL,
    "share_type" VARCHAR(20) NOT NULL,
    "share_data" TEXT NOT NULL,
    "public_key" TEXT NOT NULL,
    "threshold_n" INTEGER NOT NULL,
    "threshold_t" INTEGER NOT NULL,
    "status" VARCHAR(20) NOT NULL DEFAULT 'active',
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,
    "last_used_at" TIMESTAMP(3),

    CONSTRAINT "party_shares_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "session_states" (
    "id" VARCHAR(255) NOT NULL,
    "session_id" VARCHAR(255) NOT NULL,
    "party_id" VARCHAR(255) NOT NULL,
    "party_index" INTEGER NOT NULL,
    "session_type" VARCHAR(20) NOT NULL,
    "participants" TEXT NOT NULL,
    "threshold_n" INTEGER NOT NULL,
    "threshold_t" INTEGER NOT NULL,
    "status" VARCHAR(20) NOT NULL,
    "current_round" INTEGER NOT NULL DEFAULT 0,
    "error_message" TEXT,
    "public_key" TEXT,
    "message_hash" VARCHAR(66),
    "signature" TEXT,
    "started_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "completed_at" TIMESTAMP(3),

    CONSTRAINT "session_states_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "share_backups" (
    "id" VARCHAR(255) NOT NULL,
    "share_id" VARCHAR(255) NOT NULL,
    "backup_data" TEXT NOT NULL,
    "backup_type" VARCHAR(20) NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "created_by" VARCHAR(255),

    CONSTRAINT "share_backups_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "uk_party_session" ON "party_shares"("party_id", "session_id");

-- CreateIndex
CREATE INDEX "idx_ps_party_id" ON "party_shares"("party_id");

-- CreateIndex
CREATE INDEX "idx_ps_session_id" ON "party_shares"("session_id");

-- CreateIndex
CREATE INDEX "idx_ps_status" ON "party_shares"("status");

-- CreateIndex
CREATE UNIQUE INDEX "uk_session_party" ON "session_states"("session_id", "party_id");

-- CreateIndex
CREATE INDEX "idx_ss_session_id" ON "session_states"("session_id");

-- CreateIndex
CREATE INDEX "idx_ss_party_id" ON "session_states"("party_id");

-- CreateIndex
CREATE INDEX "idx_ss_status" ON "session_states"("status");

-- CreateIndex
CREATE INDEX "idx_share_id" ON "share_backups"("share_id");

-- CreateIndex
CREATE INDEX "idx_created_at" ON "share_backups"("created_at");
