import {
    pgTable,
    uuid,
    bigint,
    timestamp,
    text,
    integer,
} from "drizzle-orm/pg-core"

import { tenants } from "./tenants"
import { authUsers } from "./auth_users"
import { lockedTenantEnum } from "./enums"

export const tenantExportJobs = pgTable("tenant_export_jobs", {
    id: uuid("id").primaryKey().defaultRandom(),

    createdAt: timestamp("created_at", { withTimezone: true })
        .notNull()
        .defaultNow(),

    updatedAt: timestamp("updated_at", { withTimezone: true }),
    completedAt: timestamp("completed_at", { withTimezone: true }),

    tenantId: bigint("tenant_id", { mode: "number" })
        .notNull()
        .references(() => tenants.id, { onDelete: "cascade" }),

    createdBy: uuid("created_by").references(() => authUsers.id),

    operation: text("operation").notNull().default("export"),
    previousTenantLocked: lockedTenantEnum("previous_tenant_locked"),
    status: text("status").notNull().default("queued"),
    filename: text("filename").notNull(),
    storagePath: text("storage_path"),
    contentType: text("content_type").notNull().default("application/zip"),
    fileSize: bigint("file_size", { mode: "number" }),
    error: text("error"),

    filesTotal: integer("files_total").notNull().default(0),
    filesDone: integer("files_done").notNull().default(0),
})

export type TenantExportJob = typeof tenantExportJobs.$inferSelect
export type NewTenantExportJob = typeof tenantExportJobs.$inferInsert
