import {
    integer,
    json,
    pgTable,
    text,
    timestamp,
    varchar,
} from "drizzle-orm/pg-core";
import { user } from "./better.auth.schema";
import { relations } from "drizzle-orm";

export const task = pgTable("task", {
    id: text("id").primaryKey(),
    type: varchar("type", { length: 32 }).notNull(),
    status: varchar("status", { length: 16 }).notNull(),
    progress: integer("progress").default(0).notNull(),
    payload: json("payload").$type<Record<string, any>>(),
    result: json("result").$type<Record<string, any>>(),
    error: json("error").$type<Record<string, any>>(),
    userId: text("user_id")
        .notNull()
        .references(() => user.id, { onDelete: "cascade" }),
    resourceId: text("resource_id").notNull(),
    startedAt: timestamp("started_at"),
    completedAt: timestamp("completed_at"),
    createdAt: timestamp("created_at").notNull(),
    updatedAt: timestamp("updated_at").notNull(),
});

export const taskRelations = relations(task, ({ one }) => ({
    userAccount: one(user, {
        fields: [task.userId],
        references: [user.id],
    }),
}));
