import { BaseEntity } from '../../base/entity/base.js';
import { Column, Entity, Index } from 'typeorm';

/**
 * NetaClaw Normal Session Subagent execution record (non-crew).
 */
@Index(['sessionId', 'parentMessageId', 'sortOrder'])
@Index(['sessionId', 'status'])
@Index(['parentAgentId'])
@Index(['parentAgentId', 'createTime'])
@Entity('netaclaw_subagent_session')
export class NetaClawSubagentSessionEntity extends BaseEntity {
  @Column({ comment: 'Session ID' })
  sessionId: string;

  @Column({ comment: 'Parent message id (netaclaw_message.id)' })
  parentMessageId: number;

  @Column({ comment: 'Parent toolCallId (from LLM tool call)', nullable: true })
  parentToolCallId: string | null;

  @Column({ comment: 'Parent agent id (netaclaw_agent.id)', nullable: true })
  parentAgentId: number | null;

  @Column({ comment: 'Source type: preset', length: 20, default: 'preset' })
  sourceType: 'preset';

  @Column({ comment: 'Preset agent id (netaclaw_agent.id)', nullable: true })
  presetAgentId: number | null;

  @Column({ comment: 'Subagent name', length: 200, nullable: true })
  name: string | null;

  @Column({ comment: 'Goal', type: 'text', nullable: true })
  goal: string | null;

  @Column({ comment: 'Context', type: 'longtext', nullable: true })
  context: string | null;

  @Column({ comment: 'Status: queued/running/completed/failed/cancelled', length: 20, default: 'queued' })
  status: 'queued' | 'running' | 'completed' | 'failed' | 'cancelled';

  @Column({ comment: 'Model', length: 200, nullable: true })
  model: string | null;

  @Column({ type: 'json', comment: 'Tool names', nullable: true })
  toolNames: string[] | null;

  @Column({ comment: 'Summary', type: 'text', nullable: true })
  summary: string | null;

  @Column({ type: 'json', comment: 'Result payload', nullable: true })
  resultPayload: Record<string, unknown> | null;

  @Column({ comment: 'Error', type: 'text', nullable: true })
  error: string | null;

  @Column({ type: 'json', comment: 'Token usage', nullable: true })
  tokenUsage: { inputTokens?: number; outputTokens?: number; totalTokens?: number } | null;

  @Column({ comment: 'Sort order', default: 0 })
  sortOrder: number;

  @Column({ comment: 'Started at', type: 'datetime', nullable: true })
  startedAt: Date | null;

  @Column({ comment: 'Ended at', type: 'datetime', nullable: true })
  endedAt: Date | null;
}
