/**
 * Client-Side Analytics Facade for Nigeria Listing Platform
 * 
 * Provides semantic, type-safe tracking methods for the browser.
 * Guaranteed 100% browser-safe with zero Node.js dependencies.
 */

import { getClientPostHog } from "./client";
import {
  ANALYTICS_EVENTS,
  sanitizeTraits,
  UserSignedUpProps,
  AccountTypeSelectedProps,
  ProfileCompletedProps,
  VerificationStartedProps,
  DocumentUploadedProps,
  VerificationSubmittedProps,
  PropertyViewedProps,
  PropertySavedProps,
  SearchPerformedProps,
  FilterAppliedProps,
  AgentContactedProps,
  WhatsAppClickedProps,
  PhoneContactClickedProps,
  ViewingRequestedProps,
  PlanSelectedProps,
  CheckoutStartedProps,
  UserTraits,
} from "./events";

/**
 * Capture a client-side event with sanitized properties
 */
export function captureEvent(
  eventName: string,
  properties?: Record<string, any>
): void {
  if (typeof window === "undefined") return;

  const client = getClientPostHog();
  if (client) {
    const cleanProperties = sanitizeTraits(properties);
    client.capture(eventName, cleanProperties);
  }
}

/**
 * Identify user on the client with sanitized traits
 */
export function identifyUser(
  distinctId: string,
  traits?: UserTraits
): void {
  if (typeof window === "undefined" || !distinctId) return;

  const client = getClientPostHog();
  if (client) {
    const cleanTraits = sanitizeTraits(traits);
    client.identify(distinctId, cleanTraits);
  }
}

/**
 * Reset user session on logout
 */
export function resetUser(): void {
  if (typeof window === "undefined") return;

  const client = getClientPostHog();
  if (client) {
    client.reset();
  }
}

/**
 * Check feature flag status safely with a fallback value
 */
export function isFeatureEnabled(flagKey: string, defaultValue = false): boolean {
  if (typeof window === "undefined") return defaultValue;

  const client = getClientPostHog();
  if (client) {
    return client.isFeatureEnabled(flagKey) ?? defaultValue;
  }
  return defaultValue;
}

// ---------------------------------------------------------------------------
// Semantic Client Event Tracking
// ---------------------------------------------------------------------------

export const analytics = {
  // 1. Identity & Session
  identify: identifyUser,
  reset: resetUser,
  isFeatureEnabled,
  capture: captureEvent,

  // 2. Auth & Onboarding
  trackUserSignedUp: (props?: UserSignedUpProps) =>
    captureEvent(ANALYTICS_EVENTS.USER_SIGNED_UP, props),

  trackAccountTypeSelected: (props: AccountTypeSelectedProps) =>
    captureEvent(ANALYTICS_EVENTS.ACCOUNT_TYPE_SELECTED, props),

  trackProfileCompleted: (props?: ProfileCompletedProps) =>
    captureEvent(ANALYTICS_EVENTS.PROFILE_COMPLETED, props),

  // 3. Verification (KYC)
  trackVerificationStarted: (props?: VerificationStartedProps) =>
    captureEvent(ANALYTICS_EVENTS.VERIFICATION_STARTED, props),

  trackDocumentUploaded: (props: DocumentUploadedProps) =>
    captureEvent(ANALYTICS_EVENTS.DOCUMENT_UPLOADED, props),

  trackVerificationSubmitted: (props: VerificationSubmittedProps) =>
    captureEvent(ANALYTICS_EVENTS.VERIFICATION_SUBMITTED, props),

  // 4. Marketplace & Properties
  trackPropertyViewed: (props: PropertyViewedProps) =>
    captureEvent(ANALYTICS_EVENTS.PROPERTY_VIEWED, props),

  trackPropertySaved: (props: PropertySavedProps) =>
    captureEvent(ANALYTICS_EVENTS.PROPERTY_SAVED, props),

  // 5. Search & Discovery
  trackSearchPerformed: (props: SearchPerformedProps) =>
    captureEvent(ANALYTICS_EVENTS.SEARCH_PERFORMED, props),

  trackFilterApplied: (props: FilterAppliedProps) =>
    captureEvent(ANALYTICS_EVENTS.FILTER_APPLIED, props),

  // 6. Leads & Conversion
  trackAgentContacted: (props: AgentContactedProps) =>
    captureEvent(ANALYTICS_EVENTS.AGENT_CONTACTED, props),

  trackWhatsAppClicked: (props: WhatsAppClickedProps) =>
    captureEvent(ANALYTICS_EVENTS.WHATSAPP_CLICKED, props),

  trackPhoneContactClicked: (props: PhoneContactClickedProps) =>
    captureEvent(ANALYTICS_EVENTS.PHONE_CONTACT_CLICKED, props),

  trackViewingRequested: (props: ViewingRequestedProps) =>
    captureEvent(ANALYTICS_EVENTS.VIEWING_REQUESTED, props),

  // 7. Subscriptions & Payments
  trackPlanSelected: (props: PlanSelectedProps) =>
    captureEvent(ANALYTICS_EVENTS.PLAN_SELECTED, props),

  trackCheckoutStarted: (props: CheckoutStartedProps) =>
    captureEvent(ANALYTICS_EVENTS.CHECKOUT_STARTED, props),
};

export default analytics;
