import { pgTable, text, integer, boolean, timestamp } from "drizzle-orm/pg-core";
import { createInsertSchema } from "drizzle-zod";
import { z } from "zod/v4";
import { jobs } from "./jobs";

export const INVOICE_TYPES = ["eval", "work"] as const;
export type InvoiceType = (typeof INVOICE_TYPES)[number];

export const invoices = pgTable("invoices", {
  id: text("id").primaryKey(),
  jobId: text("job_id").notNull().references(() => jobs.id),
  paymentHash: text("payment_hash").notNull().unique(),
  paymentRequest: text("payment_request").notNull(),
  amountSats: integer("amount_sats").notNull(),
  type: text("type").$type<InvoiceType>().notNull(),
  paid: boolean("paid").notNull().default(false),
  createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
  paidAt: timestamp("paid_at", { withTimezone: true }),
});

export const insertInvoiceSchema = createInsertSchema(invoices).omit({
  createdAt: true,
  paidAt: true,
});

export type Invoice = typeof invoices.$inferSelect;
export type InsertInvoice = z.infer<typeof insertInvoiceSchema>;
