// src/models/web-visit.ts
// Copyright (C) 2026 Robert Colbert <rob.colbert@openplatform.us>
// All Rights Reserved

import { Schema, model } from "mongoose";

import { GadgetId, IUser } from "@gadget/api";
import { nanoid } from "nanoid";

export interface IWebVisit {
  _id: GadgetId;
  created: Date;
  url: string;
  user?: IUser | GadgetId;
  userAgent?: string;
  referrer?: string;
  ipAddress?: string;
  country?: string;
  region?: string;
  eu: boolean;
  timezone?: string;
  city?: string;
  latitude?: number;
  longitude?: number;
  metroCode?: number;
}
export const WebVisitSchema = new Schema<IWebVisit>({
  _id: { type: String, default: () => nanoid() },
  created: { type: Date, default: Date.now },
  url: { type: String, required: true },
  user: { type: String, index: 1, ref: "User" },
  userAgent: { type: String },
  referrer: { type: String },
  ipAddress: { type: String },
  country: { type: String },
  region: { type: String },
  eu: { type: Boolean, default: false },
  timezone: { type: String },
  city: { type: String },
  latitude: { type: Number },
  longitude: { type: Number },
  metroCode: { type: Number },
});
export const WebVisit = model<IWebVisit>("WebVisit", WebVisitSchema);

(async () => {
  await WebVisit.syncIndexes();
})();
