import { boolean, int, mysqlEnum, mysqlTable, text, timestamp, varchar } from "drizzle-orm/mysql-core";

/**
 * Core user table backing auth flow.
 * Extend this file with additional tables as your product grows.
 * Columns use camelCase to match both database fields and generated types.
 */
export const users = mysqlTable("users", {
  /**
   * Surrogate primary key. Auto-incremented numeric value managed by the database.
   * Use this for relations between tables.
   */
  id: int("id").autoincrement().primaryKey(),
  /** Manus OAuth identifier (openId) returned from the OAuth callback. Unique per user. */
  openId: varchar("openId", { length: 64 }).notNull().unique(),
  name: text("name"),
  email: varchar("email", { length: 320 }),
  loginMethod: varchar("loginMethod", { length: 64 }),
  role: mysqlEnum("role", ["user", "admin"]).default("user").notNull(),
  createdAt: timestamp("createdAt").defaultNow().notNull(),
  updatedAt: timestamp("updatedAt").defaultNow().onUpdateNow().notNull(),
  lastSignedIn: timestamp("lastSignedIn").defaultNow().notNull(),
});

export type User = typeof users.$inferSelect;
export type InsertUser = typeof users.$inferInsert;

export const cashOfferLeads = mysqlTable("cashOfferLeads", {
  id: int("id").autoincrement().primaryKey(),
  propertyAddress: varchar("propertyAddress", { length: 512 }).notNull(),
  name: varchar("name", { length: 160 }).notNull(),
  phone: varchar("phone", { length: 48 }).notNull(),
  email: varchar("email", { length: 320 }),
  timeline: varchar("timeline", { length: 120 }),
  leadSource: varchar("leadSource", { length: 120 }).notNull(),
  consent: boolean("consent").notNull().default(false),
  webhookStatus: mysqlEnum("webhookStatus", ["not_configured", "pending", "delivered", "failed"])
    .notNull()
    .default("not_configured"),
  webhookError: text("webhookError"),
  webhookAttemptedAt: timestamp("webhookAttemptedAt"),
  createdAt: timestamp("createdAt").defaultNow().notNull(),
  updatedAt: timestamp("updatedAt").defaultNow().onUpdateNow().notNull(),
});

export type CashOfferLead = typeof cashOfferLeads.$inferSelect;
export type InsertCashOfferLead = typeof cashOfferLeads.$inferInsert;
