import { Column, CreateDateColumn, Entity, Index, PrimaryGeneratedColumn } from "typeorm";

export type DeviceType = "desktop" | "mobile" | "tablet";

@Entity("page_views")
export class PageView {
  @PrimaryGeneratedColumn("uuid")
  id!: string;

  @Index()
  @Column({ type: "varchar", length: 500 })
  path!: string;

  @Column({ type: "varchar", length: 500, nullable: true })
  referrer!: string | null;

  // Anonymous id the frontend generates and stores in localStorage once
  // cookie/tracking consent is given (see CookieConsent.tsx /
  // AnalyticsTracker.tsx) — never a real identity, just enough to count
  // unique visitors vs. repeat page views. Null for any request sent
  // without consent, though the tracker shouldn't fire at all in that case.
  @Index()
  @Column({ name: "visitor_id", type: "varchar", length: 64, nullable: true })
  visitorId!: string | null;

  @Column({ type: "varchar", length: 20, default: "desktop" })
  device!: DeviceType;

  // Resolved server-side from the request IP at track time (see
  // lookupCountry in analytics.service.ts) — never taken from the client
  // body, so it can't be spoofed. Null for localhost/private IPs (dev) or
  // if the lookup fails/times out. The raw IP itself is never stored.
  @Index()
  @Column({ type: "varchar", length: 100, nullable: true })
  country!: string | null;

  @Column({ name: "country_code", type: "varchar", length: 2, nullable: true })
  countryCode!: string | null;

  @CreateDateColumn({ name: "created_at" })
  @Index()
  createdAt!: Date;
}
