import { MetricType, TimeGranularity } from '../../src/entities/aggregated-metric.entity';

/**
 * Aggregated metric fixtures for Repository-based services (trends, funnels, cohorts).
 */

export interface AggregatedMetricData {
  id: string;
  metricType: MetricType;
  granularity: TimeGranularity;
  timestamp: Date;
  value: number;
  count: number;
  dimension?: string;
  dimensionValue?: string;
  metadata?: Record<string, unknown>;
  createdAt: Date;
}

export function createAggregatedMetric(
  overrides: Partial<AggregatedMetricData> = {},
): AggregatedMetricData {
  return {
    id: 'metric-001',
    metricType: MetricType.PAGE_VIEWS,
    granularity: TimeGranularity.DAY,
    timestamp: new Date('2026-01-15T00:00:00Z'),
    value: 100,
    count: 100,
    createdAt: new Date('2026-01-15T00:00:00Z'),
    ...overrides,
  };
}

export function createAggregatedMetricSeries(
  days: number,
  overrides: Partial<AggregatedMetricData> = {},
): AggregatedMetricData[] {
  return Array.from({ length: days }, (_, i) => {
    const date = new Date('2026-01-15T00:00:00Z');
    date.setDate(date.getDate() + i);
    return createAggregatedMetric({
      id: `metric-${String(i + 1).padStart(3, '0')}`,
      timestamp: date,
      value: 100 + i * 10,
      count: 100 + i * 10,
      createdAt: date,
      ...overrides,
    });
  });
}

/**
 * Acquisition overview SQL result fixture
 */
export function createAcquisitionOverviewResult(
  overrides: Partial<Record<string, unknown>> = {},
): Record<string, unknown>[] {
  return [{
    total_sessions: '1500',
    total_users: '900',
    new_users: '350',
    returning_users: '550',
    ...overrides,
  }];
}

/**
 * Channel metrics SQL result fixture
 */
export function createChannelResults(): Record<string, unknown>[] {
  return [
    {
      channel: 'organic',
      sessions: '600',
      users: '400',
      new_users: '150',
      engaged_sessions: '420',
      conversions: '30',
      revenue: '1500.00',
    },
    {
      channel: 'direct',
      sessions: '400',
      users: '250',
      new_users: '100',
      engaged_sessions: '280',
      conversions: '20',
      revenue: '800.00',
    },
    {
      channel: 'social',
      sessions: '300',
      users: '200',
      new_users: '80',
      engaged_sessions: '180',
      conversions: '15',
      revenue: '500.00',
    },
  ];
}

/**
 * Source metrics SQL result fixture
 */
export function createSourceResults(): Record<string, unknown>[] {
  return [
    {
      source: 'google',
      medium: 'organic',
      sessions: '450',
      users: '300',
      engaged_sessions: '315',
      conversions: '22',
    },
    {
      source: 'direct',
      medium: 'none',
      sessions: '400',
      users: '250',
      engaged_sessions: '280',
      conversions: '18',
    },
  ];
}

/**
 * Campaign metrics SQL result fixture
 */
export function createCampaignResults(): Record<string, unknown>[] {
  return [
    {
      campaign: 'summer-2026',
      source: 'google',
      medium: 'cpc',
      sessions: '200',
      users: '150',
      engaged_sessions: '140',
      conversions: '12',
      revenue: '600.00',
    },
  ];
}

/**
 * Referrer metrics SQL result fixture
 */
export function createReferrerResults(): Record<string, unknown>[] {
  return [
    {
      referrer: 'https://twitter.com/some-post',
      domain: 'twitter.com',
      sessions: '150',
      users: '120',
      engaged_sessions: '90',
      conversions: '5',
    },
  ];
}

/**
 * Engagement overview SQL result fixture
 */
export function createEngagementOverviewResult(): Record<string, unknown>[] {
  return [{
    total_sessions: '1200',
    engaged_sessions: '840',
    avg_duration: '185.5',
    avg_page_views: '3.2',
    avg_events: '8.5',
    bounced_sessions: '240',
    total_page_views: '3840',
    total_events: '10200',
  }];
}

export function createUniqueViewsResult(): Record<string, unknown>[] {
  return [{ unique_views: '2800' }];
}

/**
 * Page metrics SQL result fixture
 */
export function createPageResults(): Record<string, unknown>[] {
  return [
    {
      path: '/',
      views: '1200',
      unique_views: '800',
      avg_time_on_page: '45.2',
      entrances: '900',
      exits: '300',
      bounce_rate: '0.25',
      exit_rate: '0.25',
    },
    {
      path: '/profiles',
      views: '800',
      unique_views: '600',
      avg_time_on_page: '62.8',
      entrances: '200',
      exits: '400',
      bounce_rate: '0.15',
      exit_rate: '0.50',
    },
  ];
}

/**
 * Event metrics SQL result fixture
 */
export function createEventResults(): Record<string, unknown>[] {
  return [
    {
      event_type: 'click_cta',
      category: 'click',
      count: '450',
      unique_users: '300',
      avg_value: '0',
    },
    {
      event_type: 'form_submit',
      category: 'form',
      count: '120',
      unique_users: '100',
      avg_value: '0',
    },
  ];
}

/**
 * Scroll depth SQL result fixture
 */
export function createScrollDepthResults(): Record<string, unknown>[] {
  return [
    {
      page: '/',
      depth_25: '180',
      depth_50: '140',
      depth_75: '90',
      depth_100: '50',
      avg_depth: '55.5',
      total: '200',
    },
  ];
}

/**
 * User flow SQL result fixture
 */
export function createUserFlowResults(): Record<string, unknown>[] {
  return [
    { start_page: '/', total_sessions: '800', step_num: 1, step_page: '/', step_sessions: '800' },
    { start_page: '/', total_sessions: '800', step_num: 2, step_page: '/profiles', step_sessions: '500' },
    { start_page: '/', total_sessions: '800', step_num: 3, step_page: '/profile/1', step_sessions: '300' },
  ];
}

/**
 * Audience overview SQL result fixture
 */
export function createAudienceOverviewResult(
  overrides: Partial<Record<string, unknown>> = {},
): Record<string, unknown>[] {
  return [{
    total_users: '900',
    new_users: '350',
    returning_users: '550',
    avg_sessions_per_user: '1.67',
    ...overrides,
  }];
}

/**
 * Demographics SQL result fixture
 */
export function createDemographicsResults(): Record<string, unknown>[] {
  return [
    {
      user_type: 'new',
      users: '350',
      sessions: '400',
      engagement_rate: '0.65',
      conversion_rate: '0.03',
      revenue_per_user: '4.28',
    },
    {
      user_type: 'returning',
      users: '550',
      sessions: '1100',
      engagement_rate: '0.78',
      conversion_rate: '0.08',
      revenue_per_user: '12.50',
    },
  ];
}

/**
 * Device metrics SQL result fixture
 */
export function createDeviceResults(): Record<string, unknown>[] {
  return [
    {
      device_type: 'desktop',
      sessions: '700',
      users: '450',
      percentage: '0.47',
      engagement_rate: '0.72',
      conversion_rate: '0.06',
    },
    {
      device_type: 'mobile',
      sessions: '550',
      users: '350',
      percentage: '0.37',
      engagement_rate: '0.58',
      conversion_rate: '0.03',
    },
    {
      device_type: 'tablet',
      sessions: '250',
      users: '150',
      percentage: '0.16',
      engagement_rate: '0.64',
      conversion_rate: '0.04',
    },
  ];
}

/**
 * Browser metrics SQL result fixture
 */
export function createBrowserResults(): Record<string, unknown>[] {
  return [
    { browser: 'Chrome', version: '120', sessions: '600', users: '400', percentage: '0.40' },
    { browser: 'Safari', version: '17', sessions: '400', users: '280', percentage: '0.27' },
    { browser: 'Firefox', version: '121', sessions: '200', users: '150', percentage: '0.13' },
  ];
}

/**
 * OS metrics SQL result fixture
 */
export function createOSResults(): Record<string, unknown>[] {
  return [
    { os: 'Windows', version: '11', sessions: '500', users: '350', percentage: '0.33' },
    { os: 'macOS', version: '14', sessions: '400', users: '280', percentage: '0.27' },
    { os: 'iOS', version: '17', sessions: '350', users: '250', percentage: '0.23' },
  ];
}

/**
 * Geography SQL result fixture
 */
export function createGeoResults(): Record<string, unknown>[] {
  return [
    {
      location: 'Iceland',
      sessions: '400',
      users: '250',
      percentage: '0.27',
      engagement_rate: '0.82',
      conversion_rate: '0.09',
    },
    {
      location: 'United States',
      sessions: '350',
      users: '220',
      percentage: '0.23',
      engagement_rate: '0.68',
      conversion_rate: '0.05',
    },
  ];
}

/**
 * Language metrics SQL result fixture
 */
export function createLanguageResults(): Record<string, unknown>[] {
  return [
    { language: 'en', sessions: '800', users: '500', percentage: '0.53' },
    { language: 'is', sessions: '300', users: '200', percentage: '0.20' },
  ];
}

/**
 * New vs returning SQL result fixture
 */
export function createNewVsReturningResults(): Record<string, unknown>[] {
  return [
    { period: new Date('2026-01-15'), new_users: '50', returning_users: '80' },
    { period: new Date('2026-01-16'), new_users: '45', returning_users: '85' },
    { period: new Date('2026-01-17'), new_users: '55', returning_users: '75' },
  ];
}

/**
 * Session list SQL result fixture
 */
export function createSessionListResults(): Record<string, unknown>[] {
  return [
    {
      sessionId: 'sess-001',
      userId: 'user-001',
      start_time: '2026-01-15T10:00:00Z',
      last_activity_time: '2026-01-15T10:05:30Z',
      duration_seconds: '330',
      page_views: '5',
      total_events: '12',
      has_conversion: true,
      trafficSource: 'organic',
      deviceType: 'desktop',
      browser: 'Chrome',
      os: 'Windows',
      country: 'Iceland',
      city: 'Reykjavik',
      landingPage: '/',
    },
    {
      sessionId: 'sess-002',
      userId: 'user-002',
      start_time: '2026-01-15T11:00:00Z',
      last_activity_time: '2026-01-15T11:01:00Z',
      duration_seconds: '60',
      page_views: '1',
      total_events: '2',
      has_conversion: false,
      trafficSource: 'direct',
      deviceType: 'mobile',
      browser: 'Safari',
      os: 'iOS',
      country: 'United States',
      city: 'New York',
      landingPage: '/profiles',
    },
  ];
}

export function createSessionCountResult(total: number = 150): Record<string, unknown>[] {
  return [{ total: String(total) }];
}

/**
 * Session metrics SQL result fixture
 */
export function createSessionMetricsResult(): Record<string, unknown>[] {
  return [{
    total_sessions: '1500',
    avg_duration: '185.5',
    avg_page_views: '3.2',
    bounce_rate: '0.20',
    engagement_rate: '0.70',
    new_user_rate: '0.39',
    conversion_rate: '0.04',
  }];
}

/**
 * Segment metrics SQL result fixture
 */
export function createSegmentMetricsResult(): Record<string, unknown>[] {
  return [{
    sessions: '600',
    users: '400',
    engagement_rate: '0.72',
    conversion_rate: '0.06',
    avg_duration: '195.0',
    avg_page_views: '3.5',
    total_revenue: '1200.00',
  }];
}
