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

/**
 * Token Usage ORM Entity - Database representation
 */
@Entity('token_usages')
@Index('idx_token_usages_tenant', ['tenantId'])
@Index('idx_token_usages_user', ['userId'])
@Index('idx_token_usages_conversation', ['conversationId'])
@Index('idx_token_usages_created', ['createdAt'])
@Index('idx_token_usages_model', ['model'])
export class TokenUsageORM {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Column({ name: 'user_id', type: 'uuid', nullable: true })
  userId: string | null;

  @Column({ name: 'conversation_id', type: 'uuid' })
  conversationId: string;

  @Column({ name: 'message_id', type: 'uuid', nullable: true })
  messageId: string | null;

  @Column({ type: 'varchar', length: 50 })
  model: string;

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

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

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

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

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

  @Column({ name: 'estimated_cost', type: 'decimal', precision: 10, scale: 6, default: 0 })
  estimatedCost: number;

  @Column({ name: 'intent_type', type: 'varchar', length: 30, nullable: true })
  intentType: string | null;

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

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

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

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