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

export type ApiCallLogStatus = 'success' | 'failure';

@Entity('api_call_logs')
@Index('idx_api_call_logs_provider_created_at', ['provider', 'createdAt'])
@Index('idx_api_call_logs_act_created_at', ['act', 'createdAt'])
export class ApiCallLog {
  @PrimaryColumn({ type: 'varchar', length: 36 })
  id: string;

  @Column({ type: 'varchar', length: 64, default: 'biedawo' })
  provider: string;

  @Column({ type: 'varchar', length: 80 })
  act: string;

  @Column({ type: 'varchar', length: 12 })
  method: string;

  @Column({ name: 'endpoint', type: 'varchar', length: 255 })
  url: string;

  @Column({ type: 'varchar', length: 16 })
  status: ApiCallLogStatus;

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

  @Column({ name: 'status_code', type: 'int', nullable: true })
  httpStatus: number | null;

  @Column({ name: 'duration_ms', type: 'int', nullable: true })
  durationMs: number | null;

  @Column({ name: 'request_payload', type: 'json', nullable: true })
  requestPayload: Record<string, unknown> | null;

  @Column({ name: 'response_payload', type: 'json', nullable: true })
  responsePayload: unknown | null;

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

  @CreateDateColumn({ name: 'created_at', type: 'datetime', precision: 6 })
  createdAt: Date;
}
