export interface Visitor {
  id: string;
  sessionId: string;
  ipAddress?: string;
  userAgent?: string;
  referrer?: string;
  landingPage?: string;
  pages: string[];
  events: VisitorEvent[];
  firstVisitAt: Date;
  lastVisitAt: Date;
  visitCount: number;
  converted: boolean;
  conversionData?: {
    type: 'registration' | 'subscription' | 'trial';
    timestamp: Date;
    value?: number;
  };
  createdAt: Date;
  updatedAt: Date;
}

export interface VisitorEvent {
  type: 'page_view' | 'click' | 'scroll' | 'form_submit' | 'download' | 'video_play';
  timestamp: Date;
  data?: Record<string, any>;
}

export class VisitorEntity {
  static create(data: Partial<Visitor>): Visitor {
    const now = new Date();

    return {
      id: data.id || `visitor-${Date.now()}`,
      sessionId: data.sessionId || `session-${Date.now()}`,
      ipAddress: data.ipAddress,
      userAgent: data.userAgent,
      referrer: data.referrer,
      landingPage: data.landingPage,
      pages: data.pages || [],
      events: data.events || [],
      firstVisitAt: data.firstVisitAt || now,
      lastVisitAt: data.lastVisitAt || now,
      visitCount: data.visitCount || 1,
      converted: data.converted || false,
      conversionData: data.conversionData,
      createdAt: data.createdAt || now,
      updatedAt: data.updatedAt || now
    };
  }

  static addEvent(visitor: Visitor, event: VisitorEvent): Visitor {
    return {
      ...visitor,
      events: [...visitor.events, event],
      lastVisitAt: new Date(),
      updatedAt: new Date()
    };
  }

  static addPage(visitor: Visitor, page: string): Visitor {
    return {
      ...visitor,
      pages: [...visitor.pages, page],
      lastVisitAt: new Date(),
      updatedAt: new Date()
    };
  }
}
