import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  OneToOne,
  JoinColumn,
  Index,
} from 'typeorm';
import { ToolCall } from './tool-call.entity';

export type ResultType = 'object' | 'array' | 'string' | 'number' | 'boolean' | 'null' | 'error';

@Entity({ name: 'tool_call_results', schema: 'ai' })
export class ToolCallResult {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Index()
  @Column({ name: 'tool_call_id', type: 'uuid' })
  toolCallId: string;

  @Column({ type: 'jsonb', nullable: true })
  result: any;

  @Column({ name: 'result_type', type: 'varchar', length: 20, default: 'object' })
  resultType: ResultType;

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

  @Index()
  @Column({ name: 'error_code', type: 'varchar', length: 50, nullable: true })
  errorCode: string;

  @Column({ name: 'tokens_used', type: 'int', nullable: true })
  tokensUsed: number;

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

  @OneToOne(() => ToolCall, (call) => call.result, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'tool_call_id' })
  toolCall: ToolCall;
}
