import { pgTable, text, timestamp, index, uuid, boolean, jsonb, doublePrecision, integer } from "drizzle-orm/pg-core";
import { users } from "./auth";
import { feedbackType, ruleType, ruleAction } from "./enums";

export const spamFeedback = pgTable("spam_feedback", {
  id: uuid("id").defaultRandom().primaryKey(),
  userId: uuid("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
  phoneNumber: text("phone_number").notNull(),
  phoneNumberHash: text("phone_number_hash").notNull(),
  isSpam: boolean("is_spam").notNull(),
  confidence: doublePrecision("confidence"),
  feedbackType: feedbackType("feedback_type").notNull(),
  metadata: jsonb("metadata"),
  createdAt: timestamp("created_at", { withTimezone: true, mode: "date" }).defaultNow().notNull(),
  updatedAt: timestamp("updated_at", { withTimezone: true, mode: "date" }).defaultNow().notNull().$onUpdate(() => new Date()),
}, (table) => ({
  userIdIdx: index("spam_feedback_user_id_idx").on(table.userId),
  phoneNumberHashIdx: index("spam_feedback_phone_number_hash_idx").on(table.phoneNumberHash),
  isSpamIdx: index("spam_feedback_is_spam_idx").on(table.isSpam),
}));

export const spamRules = pgTable("spam_rules", {
  id: uuid("id").defaultRandom().primaryKey(),
  userId: uuid("user_id").references(() => users.id, { onDelete: "cascade" }),
  isGlobal: boolean("is_global").default(false).notNull(),
  ruleType: ruleType("rule_type").notNull(),
  pattern: text("pattern").notNull(),
  action: ruleAction("action").notNull(),
  priority: integer("priority").default(0).notNull(),
  isActive: boolean("is_active").default(true).notNull(),
  createdAt: timestamp("created_at", { withTimezone: true, mode: "date" }).defaultNow().notNull(),
  updatedAt: timestamp("updated_at", { withTimezone: true, mode: "date" }).defaultNow().notNull().$onUpdate(() => new Date()),
}, (table) => ({
  userIdIdx: index("spam_rules_user_id_idx").on(table.userId),
  isGlobalIdx: index("spam_rules_is_global_idx").on(table.isGlobal),
  ruleTypeIdx: index("spam_rules_rule_type_idx").on(table.ruleType),
}));
