import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  Index,
  ManyToOne,
  JoinColumn,
  OneToMany,
} from 'typeorm';
import { ReportDefinition } from './report-definition.entity';
import { ScheduleExecution } from './schedule-execution.entity';

/**
 * Execution status enum
 */
export enum ExecutionStatus {
  PENDING = 'pending',
  RUNNING = 'running',
  COMPLETED = 'completed',
  FAILED = 'failed',
  CANCELLED = 'cancelled',
}

/**
 * Export format enum
 */
export enum ExportFormat {
  PDF = 'pdf',
  EXCEL = 'excel',
  CSV = 'csv',
  JSON = 'json',
  HTML = 'html',
}

/**
 * Report Execution Entity (schema: reports.report_executions)
 *
 * Stores the history of report executions including parameters,
 * results, and execution statistics.
 */
@Entity({ name: 'report_executions', schema: 'reports' })
export class ReportExecution {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Index()
  @Column({ name: 'report_definition_id', type: 'uuid' })
  reportDefinitionId: string;

  @Index()
  @Column({ name: 'executed_by', type: 'uuid' })
  executedBy: string;

  @Index()
  @Column({
    name: 'status',
    type: 'enum',
    enum: ExecutionStatus,
    enumName: 'execution_status',
    default: ExecutionStatus.PENDING,
  })
  status: ExecutionStatus;

  @Column({ name: 'parameters', type: 'jsonb', default: '{}' })
  parameters: Record<string, any>;

  @Column({ name: 'result_data', type: 'jsonb', nullable: true })
  resultData: any;

  @Column({ name: 'result_summary', type: 'jsonb', nullable: true })
  resultSummary: Record<string, any> | null;

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

  @Column({
    name: 'export_format',
    type: 'enum',
    enum: ExportFormat,
    enumName: 'export_format',
    nullable: true,
  })
  exportFormat: ExportFormat | null;

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

  @Column({ name: 'file_size_bytes', type: 'bigint', nullable: true })
  fileSizeBytes: number | null;

  @Column({ name: 'started_at', type: 'timestamptz', nullable: true })
  startedAt: Date | null;

  @Column({ name: 'completed_at', type: 'timestamptz', nullable: true })
  completedAt: Date | null;

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

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

  @Column({ name: 'error_details', type: 'jsonb', nullable: true })
  errorDetails: Record<string, any> | null;

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

  // Relations
  @ManyToOne(() => ReportDefinition, (definition) => definition.executions, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'report_definition_id' })
  reportDefinition: ReportDefinition;

  @OneToMany(() => ScheduleExecution, (scheduleExec) => scheduleExec.execution)
  scheduleExecutions: ScheduleExecution[];
}
