import { pgTable, text, timestamp, uniqueIndex, index, uuid, boolean, jsonb, doublePrecision, integer } from "drizzle-orm/pg-core";
import { subscriptions } from "./subscription";
import { propertyChangeType, propertyChangeSeverity } from "./enums";

export const propertyWatchlistItems = pgTable("property_watchlist_items", {
  id: uuid("id").defaultRandom().primaryKey(),
  subscriptionId: uuid("subscription_id").notNull().references(() => subscriptions.id, { onDelete: "cascade" }),
  address: text("address").notNull(),
  parcelId: text("parcel_id"),
  ownerName: text("owner_name"),
  streetAddress: text("street_address").notNull(),
  city: text("city").default(""),
  state: text("state").default(""),
  zipCode: text("zip_code").default(""),
  latitude: doublePrecision("latitude"),
  longitude: doublePrecision("longitude"),
  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) => ({
  subParcelIdUnique: uniqueIndex("property_watchlist_items_sub_parcel_unique").on(table.subscriptionId, table.parcelId),
  subscriptionIdIdx: index("property_watchlist_items_subscription_id_idx").on(table.subscriptionId),
  parcelIdIdx: index("property_watchlist_items_parcel_id_idx").on(table.parcelId),
  addressIdx: index("property_watchlist_items_address_idx").on(table.address),
}));

export const propertySnapshots = pgTable("property_snapshots", {
  id: uuid("id").defaultRandom().primaryKey(),
  propertyWatchlistItemId: uuid("property_watchlist_item_id").notNull().references(() => propertyWatchlistItems.id, { onDelete: "cascade" }),
  subscriptionId: uuid("subscription_id").notNull(),
  capturedAt: timestamp("captured_at", { withTimezone: true, mode: "date" }).notNull(),
  ownerName: text("owner_name").notNull(),
  address: jsonb("address").notNull(),
  deedDate: text("deed_date"),
  taxId: text("tax_id"),
  propertyType: text("property_type").default("residential").notNull(),
  taxAmount: doublePrecision("tax_amount"),
  lienCount: integer("lien_count").default(0).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) => ({
  propertyWatchlistItemIdIdx: index("property_snapshots_property_watchlist_item_id_idx").on(table.propertyWatchlistItemId),
  subscriptionIdIdx: index("property_snapshots_subscription_id_idx").on(table.subscriptionId),
  capturedAtIdx: index("property_snapshots_captured_at_idx").on(table.capturedAt),
}));

export const propertyChanges = pgTable("property_changes", {
  id: uuid("id").defaultRandom().primaryKey(),
  propertyWatchlistItemId: uuid("property_watchlist_item_id").notNull().references(() => propertyWatchlistItems.id, { onDelete: "cascade" }),
  snapshotId: uuid("snapshot_id").references(() => propertySnapshots.id),
  changeType: propertyChangeType("change_type").notNull(),
  severity: propertyChangeSeverity("severity").default("info").notNull(),
  details: jsonb("details"),
  detectedAt: timestamp("detected_at", { withTimezone: true, mode: "date" }).defaultNow().notNull(),
}, (table) => ({
  propertyWatchlistItemIdIdx: index("property_changes_property_watchlist_item_id_idx").on(table.propertyWatchlistItemId),
  snapshotIdIdx: index("property_changes_snapshot_id_idx").on(table.snapshotId),
  changeTypeIdx: index("property_changes_change_type_idx").on(table.changeType),
}));
