import { pgTable, serial, text, numeric, integer, timestamp, jsonb } from "drizzle-orm/pg-core";
import { createInsertSchema } from "drizzle-zod";
import { z } from "zod/v4";

export const invoicesTable = pgTable("invoices", {
  id: serial("id").primaryKey(),
  invoiceNumber: text("invoice_number"),
  partyType: text("party_type"),
  partyId: integer("party_id"),
  partyName: text("party_name"),
  date: text("date").notNull(),
  items: jsonb("items").default([]),
  subtotal: numeric("subtotal", { precision: 15, scale: 2 }).default("0").notNull(),
  discount: numeric("discount", { precision: 15, scale: 2 }).default("0"),
  tax: numeric("tax", { precision: 15, scale: 2 }).default("0"),
  total: numeric("total", { precision: 15, scale: 2 }).default("0").notNull(),
  paymentStatus: text("payment_status").default("pending"),
  notes: text("notes"),
  createdAt: timestamp("created_at").defaultNow().notNull(),
});

export const insertInvoiceSchema = createInsertSchema(invoicesTable).omit({ id: true, createdAt: true });
export type InsertInvoice = z.infer<typeof insertInvoiceSchema>;
export type Invoice = typeof invoicesTable.$inferSelect;
