import { sql } from "drizzle-orm";
import { index, integer, sqliteTable, text } from "drizzle-orm/sqlite-core";

export const leads = sqliteTable(
  "leads",
  {
    id: integer("id").primaryKey({ autoIncrement: true }),
    publicId: text("public_id").notNull().unique(),
    eventName: text("event_name").notNull().default("COMANDO"),
    journey: text("journey").notNull().default("waitlist"),
    fullName: text("full_name").notNull(),
    email: text("email").notNull(),
    phone: text("phone"),
    segment: text("segment").notNull(),
    role: text("role"),
    company: text("company"),
    consentService: integer("consent_service", { mode: "boolean" }).notNull().default(false),
    consentEmailMarketing: integer("consent_email_marketing", { mode: "boolean" }).notNull().default(false),
    consentWhatsapp: integer("consent_whatsapp", { mode: "boolean" }).notNull().default(false),
    consentSms: integer("consent_sms", { mode: "boolean" }).notNull().default(false),
    consentVersion: text("consent_version").notNull().default("2026-08-31.v1"),
    sourcePath: text("source_path").notNull().default("/"),
    utmSource: text("utm_source"),
    utmMedium: text("utm_medium"),
    utmCampaign: text("utm_campaign"),
    utmContent: text("utm_content"),
    utmTerm: text("utm_term"),
    creativeId: text("creative_id"),
    experimentId: text("experiment_id"),
    experimentVariant: text("experiment_variant"),
    assignmentMethod: text("assignment_method"),
    detailsJson: text("details_json"),
    status: text("status").notNull().default("new"),
    createdAt: text("created_at").notNull().default(sql`CURRENT_TIMESTAMP`),
  },
  (table) => [
    index("leads_email_idx").on(table.email),
    index("leads_segment_idx").on(table.segment),
    index("leads_created_at_idx").on(table.createdAt),
  ],
);

export const orders = sqliteTable(
  "orders",
  {
    id: integer("id").primaryKey({ autoIncrement: true }),
    publicId: text("public_id").notNull().unique(),
    stripeSessionId: text("stripe_session_id").notNull().unique(),
    ticketTier: text("ticket_tier").notNull(),
    paymentStatus: text("payment_status").notNull().default("pending"),
    amountTotal: integer("amount_total"),
    currency: text("currency"),
    customerEmail: text("customer_email"),
    createdAt: text("created_at").notNull().default(sql`CURRENT_TIMESTAMP`),
    updatedAt: text("updated_at").notNull().default(sql`CURRENT_TIMESTAMP`),
  },
  (table) => [
    index("orders_payment_status_idx").on(table.paymentStatus),
    index("orders_created_at_idx").on(table.createdAt),
  ],
);

export const stripeEvents = sqliteTable("stripe_events", {
  id: integer("id").primaryKey({ autoIncrement: true }),
  eventId: text("event_id").notNull().unique(),
  eventType: text("event_type").notNull(),
  receivedAt: text("received_at").notNull().default(sql`CURRENT_TIMESTAMP`),
});
