/**
 * Analytics API Types
 * Based on Analytics Service OpenAPI Specification
 */

// ============= Common Types =============
export interface PeriodInfo {
  startDate: string;
  endDate: string;
  durationDays: number;
}

export interface SocialInteractionStats {
  likes: number;
  comments: number;
  shares: number;
}

// ============= Summary Types =============
export interface AnalyticsSummary {
  participants: number;
  participantsDelta: number;
  totalViews: number;
  totalReach: number;
  engagementRate: number;
  conversionRate: number;
  averageEngagementTime: number;
  targetRoi: number;
  socialInteractions?: SocialInteractionStats;
}

export interface ChannelSummary {
  channel: string;
  views: number;
  participants: number;
  engagementRate: number;
  conversionRate: number;
  roi: number;
}

export interface RoiSummary {
  totalCost: number;
  expectedRevenue: number;
  netProfit: number;
  roi: number;
  costPerAcquisition: number;
}

export interface EventPerformanceSummary {
  eventId: string;
  eventTitle: string;
  participants: number;
  views: number;
  roi: number;
  status: string;
}

// ============= Timeline Types =============
export interface TimelineDataPoint {
  timestamp: string;
  participants: number;
  views: number;
  engagement: number;
  conversions: number;
  cumulativeParticipants: number;
}

export interface PeakTimeInfo {
  timestamp: string;
  metric: string;
  value: number;
  description: string;
}

export interface TrendAnalysis {
  overallTrend: string;
  growthRate: number;
  projectedParticipants: number;
  peakPeriod: string;
}

// ============= ROI Types =============
export interface InvestmentDetails {
  prizeCost: number;
  contentCreation: number;
  distribution: number;
  operation: number;
  total: number;
  breakdown?: Record<string, unknown>[];
}

export interface RevenueDetails {
  directSales: number;
  expectedSales: number;
  brandValue: number;
  newCustomerRevenue: number;
  total: number;
}

export interface RoiCalculation {
  netProfit: number;
  roiPercentage: number;
  breakEvenPoint?: string;
  paybackPeriod: number;
}

export interface CostEfficiency {
  costPerParticipant: number;
  costPerConversion: number;
  costPerView: number;
  revenuePerParticipant: number;
}

export interface RevenueProjection {
  currentRevenue: number;
  projectedFinalRevenue: number;
  confidenceLevel: number;
  basedOn: string;
}

// ============= Channel Types =============
export interface VoiceCallStats {
  totalCalls: number;
  completedCalls: number;
  averageDuration: number;
  completionRate: number;
}

export interface ChannelMetrics {
  impressions: number;
  views: number;
  clicks: number;
  participants: number;
  conversions: number;
  socialInteractions?: SocialInteractionStats;
  voiceCallStats?: VoiceCallStats;
}

export interface ChannelPerformance {
  clickThroughRate: number;
  engagementRate: number;
  conversionRate: number;
  averageEngagementTime: number;
  bounceRate: number;
}

export interface ChannelCosts {
  distributionCost: number;
  costPerView: number;
  costPerClick: number;
  costPerAcquisition: number;
  roi: number;
}

export interface ChannelAnalytics {
  channelName: string;
  channelType: string;
  metrics: ChannelMetrics;
  performance: ChannelPerformance;
  costs: ChannelCosts;
  externalApiStatus?: string;
}

export interface ChannelComparison {
  bestPerforming: Record<string, string>;
  averageMetrics: Record<string, number>;
}

// ============= Dashboard Response Types =============
export interface UserAnalyticsDashboardResponse {
  userId: string;
  period: PeriodInfo;
  totalEvents: number;
  activeEvents: number;
  overallSummary: AnalyticsSummary;
  channelPerformance: ChannelSummary[];
  overallRoi: RoiSummary;
  eventPerformances: EventPerformanceSummary[];
  lastUpdatedAt: string;
  dataSource: string;
}

export interface AnalyticsDashboardResponse {
  eventId: string;
  eventTitle: string;
  period: PeriodInfo;
  summary: AnalyticsSummary;
  channelPerformance: ChannelSummary[];
  roi: RoiSummary;
  lastUpdatedAt: string;
  dataSource: string;
}

export interface TimelineAnalyticsResponse {
  eventId: string;
  interval: string;
  dataPoints: TimelineDataPoint[];
  trends: TrendAnalysis;
  peakTimes: PeakTimeInfo[];
  lastUpdatedAt: string;
}

export interface UserTimelineAnalyticsResponse {
  userId: string;
  period: PeriodInfo;
  totalEvents: number;
  interval: string;
  dataPoints: TimelineDataPoint[];
  trend: TrendAnalysis;
  peakTime: PeakTimeInfo;
  lastUpdatedAt: string;
  dataSource: string;
}

export interface RoiAnalyticsResponse {
  eventId: string;
  investment: InvestmentDetails;
  revenue: RevenueDetails;
  roi: RoiCalculation;
  costEfficiency: CostEfficiency;
  projection: RevenueProjection;
  lastUpdatedAt: string;
}

export interface UserRoiAnalyticsResponse {
  userId: string;
  period: PeriodInfo;
  totalEvents: number;
  overallInvestment: InvestmentDetails;
  overallRevenue: RevenueDetails;
  overallRoi: RoiCalculation;
  costEfficiency: CostEfficiency;
  projection: RevenueProjection;
  eventRois: EventRoiSummary[];
  lastUpdatedAt: string;
  dataSource: string;
}

export interface EventRoiSummary {
  eventId: string;
  eventTitle: string;
  totalInvestment: number;
  expectedRevenue: number;
  roi: number;
  status: string;
}

export interface ChannelAnalyticsResponse {
  eventId: string;
  channels: ChannelAnalytics[];
  comparison: ChannelComparison;
  lastUpdatedAt: string;
}

export interface UserChannelAnalyticsResponse {
  userId: string;
  period: PeriodInfo;
  totalEvents: number;
  channels: ChannelAnalytics[];
  comparison: ChannelComparison;
  lastUpdatedAt: string;
  dataSource: string;
}

// ============= API Response Wrapper =============
export interface ApiResponse<T> {
  success: boolean;
  data: T;
  errorCode?: string;
  message?: string;
  timestamp: string;
}

// ============= API Request Types =============
export interface AnalyticsQueryParams {
  startDate?: string;
  endDate?: string;
  refresh?: boolean;
}

export interface TimelineQueryParams extends AnalyticsQueryParams {
  interval?: 'hourly' | 'daily' | 'weekly' | 'monthly';
  metrics?: string;
}

export interface ChannelQueryParams extends AnalyticsQueryParams {
  channels?: string;
  sortBy?: 'views' | 'participants' | 'engagement_rate' | 'conversion_rate' | 'roi';
  order?: 'asc' | 'desc';
}

export interface RoiQueryParams {
  includeProjection?: boolean;
  refresh?: boolean;
}
