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

/**
 * Agent Execution ORM Entity
 * 记录每次 Agent 调用的执行数据，用于运营分析
 */
@Entity('agent_executions')
@Index('idx_agent_executions_tenant', ['tenantId'])
@Index('idx_agent_executions_conversation', ['conversationId'])
@Index('idx_agent_executions_agent_type', ['agentType'])
@Index('idx_agent_executions_created', ['createdAt'])
@Index('idx_agent_executions_tenant_date', ['tenantId', 'createdAt'])
export class AgentExecutionORM {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

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

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

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

  @Column({ name: 'agent_type', type: 'varchar', length: 30 })
  agentType: string;

  @Column({ name: 'agent_name', type: 'varchar', length: 50 })
  agentName: string;

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

  @Column({ type: 'boolean', default: true })
  success: boolean;

  @Column({ name: 'error_message', type: 'text', nullable: true })
  errorMessage: string | null;

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

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

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

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