import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  Index,
  Unique,
} from 'typeorm';

@Entity('daily_statistics')
@Unique(['tenantId', 'statDate', 'dimension', 'dimensionValue'])
@Index('idx_daily_statistics_tenant', ['tenantId'])
@Index('idx_daily_statistics_stat_date', ['statDate'])
@Index('idx_daily_statistics_dimension', ['dimension', 'dimensionValue'])
export class DailyStatisticsORM {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ name: 'tenant_id', type: 'uuid' })
  tenantId: string;

  @Column({ name: 'stat_date', type: 'date' })
  statDate: Date;

  @Column({ type: 'varchar', length: 30, default: 'OVERALL' })
  dimension: string;

  @Column({ name: 'dimension_value', type: 'varchar', length: 50, nullable: true })
  dimensionValue: string | null;

  // User Statistics
  @Column({ name: 'new_users', type: 'int', default: 0 })
  newUsers: number;

  @Column({ name: 'new_registered_users', type: 'int', default: 0 })
  newRegisteredUsers: number;

  @Column({ name: 'active_users', type: 'int', default: 0 })
  activeUsers: number;

  // Conversation Statistics
  @Column({ name: 'new_conversations', type: 'int', default: 0 })
  newConversations: number;

  @Column({ name: 'total_messages', type: 'int', default: 0 })
  totalMessages: number;

  @Column({ name: 'user_messages', type: 'int', default: 0 })
  userMessages: number;

  @Column({ name: 'assistant_messages', type: 'int', default: 0 })
  assistantMessages: number;

  @Column({ name: 'avg_conversation_turns', type: 'decimal', precision: 10, scale: 2, default: 0 })
  avgConversationTurns: number;

  // Order Statistics
  @Column({ name: 'new_orders', type: 'int', default: 0 })
  newOrders: number;

  @Column({ name: 'paid_orders', type: 'int', default: 0 })
  paidOrders: number;

  @Column({ name: 'total_order_amount', type: 'decimal', precision: 12, scale: 2, default: 0 })
  totalOrderAmount: number;

  @Column({ name: 'total_paid_amount', type: 'decimal', precision: 12, scale: 2, default: 0 })
  totalPaidAmount: number;

  @Column({ name: 'refunded_orders', type: 'int', default: 0 })
  refundedOrders: number;

  @Column({ name: 'refund_amount', type: 'decimal', precision: 12, scale: 2, default: 0 })
  refundAmount: number;

  // Conversion Statistics
  @Column({ name: 'conversion_count', type: 'int', default: 0 })
  conversionCount: number;

  @Column({ name: 'conversion_rate', type: 'decimal', precision: 5, scale: 4, default: 0 })
  conversionRate: number;

  // Token Statistics
  @Column({ name: 'total_input_tokens', type: 'bigint', default: 0 })
  totalInputTokens: number;

  @Column({ name: 'total_output_tokens', type: 'bigint', default: 0 })
  totalOutputTokens: number;

  @Column({ name: 'estimated_api_cost', type: 'decimal', precision: 10, scale: 4, default: 0 })
  estimatedApiCost: number;

  @CreateDateColumn({ name: 'created_at' })
  createdAt: Date;

  @UpdateDateColumn({ name: 'updated_at' })
  updatedAt: Date;
}
