import { sqliteTable, text, real, integer, index } from "drizzle-orm/sqlite-core"; import { subscriptions } from "./subscription"; /** * Tracks Attom Data Solutions API usage for cost analytics and billing. * Each row records one API call (or batch of calls for multi-property scans). * Cost is tracked in dollars at ~$0.05–$0.10 per record lookup. */ export const attomApiUsage = sqliteTable("attom_api_usage", { id: text("id").primaryKey().$defaultFn(() => crypto.randomUUID()), subscriptionId: text("subscription_id").notNull().references(() => subscriptions.id, { onDelete: "cascade" }), userId: text("user_id").notNull(), endpoint: text("endpoint").notNull(), cost: real("cost").notNull(), // USD cost of this API call propertyWatchlistItemId: text("property_watchlist_item_id"), statusCode: integer("status_code"), errorMessage: text("error_message"), createdAt: integer("created_at", { mode: "timestamp_ms" }).defaultNow().notNull(), }, (table) => ({ subscriptionIdIdx: index("attom_api_usage_subscription_id_idx").on(table.subscriptionId), userIdIdx: index("attom_api_usage_user_id_idx").on(table.userId), createdAtIdx: index("attom_api_usage_created_at_idx").on(table.createdAt), subscriptionCreatedAtIdx: index("attom_api_usage_sub_created_idx").on(table.subscriptionId, table.createdAt), }));