import { Database } from ".";

export const initializeSubjectTable = async (db: Database) => {
  await db.schema
    .createTable("subjects")
    .ifNotExists()
    .addColumn("id", "integer", (col) => col.primaryKey().notNull())
    .addColumn("reviewState", "text", (col) => col.notNull())
    .addColumn("createdAt", "text", (col) => col.notNull())
    .addColumn("updatedAt", "text", (col) => col.notNull())
    .addColumn("lastReportedAt", "text")
    .addColumn("lastReviewedBy", "text")
    .addColumn("lastReviewedAt", "text")
    .addColumn("takendown", "integer", (col) => col.notNull().defaultTo(0))
    .addColumn("subjectRepoHandle", "text")
    .addColumn("subjectBlobCids", "text")
    .addColumn("tags", "text")
    .addColumn("did", "text", (col) => col.notNull())
    .addColumn("recordPath", "text", (col) => col.notNull().defaultTo(""))
    .addColumn("lastAppealedAt", "text")
    .addColumn("suspendUntil", "text")
    .addColumn("muteUntil", "text")
    .addColumn("muteReportingUntil", "text")
    .addColumn("comment", "text")
    .execute();
};

export const initializeRepoTable = async (db: Database) => {
  await db.schema
    .createTable("repos")
    .ifNotExists()
    .addColumn("did", "text", (col) => col.primaryKey().notNull())
    .addColumn("handle", "text", (col) => col.notNull())
    .addColumn("ip", "text", (col) => col.notNull())
    .addColumn("profile", "jsonb")
    .addColumn("indexedAt", "text", (col) => col.notNull())
    .addColumn("email", "text", (col) => col.notNull())
    .addColumn("emailConfirmedAt", "text")
    .addColumn("threatSignatures", "text")
    .addColumn("labels", "jsonb")
    .execute();
};

export const initializeRecordTable = async (db: Database) => {
  await db.schema
    .createTable("records")
    .ifNotExists()
    .addColumn("uri", "text", (col) => col.primaryKey().notNull())
    .addColumn("cid", "text", (col) => col.notNull())
    .addColumn("value", "jsonb", (col) => col.notNull())
    .addColumn("blobCids", "jsonb", (col) => col.notNull())
    .addColumn("indexedAt", "text", (col) => col.notNull())
    .addColumn("blobs", "jsonb", (col) => col.notNull())
    .addColumn("labels", "jsonb", (col) => col.notNull())
    .execute();
};
