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

/**
 * Delivery method enum
 */
export enum DeliveryMethod {
  NONE = 'none',
  EMAIL = 'email',
  STORAGE = 'storage',
  WEBHOOK = 'webhook',
}

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

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

/**
 * Report Schedule Entity (schema: reports.report_schedules)
 *
 * Configures scheduled report execution with cron expressions,
 * delivery methods, and default parameters.
 */
@Entity({ name: 'report_schedules', schema: 'reports' })
export class ReportSchedule {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

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

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

  @Column({ name: 'cron_expression', type: 'varchar', length: 100 })
  cronExpression: string;

  @Column({ name: 'timezone', type: 'varchar', length: 100, default: 'America/Mexico_City' })
  timezone: string;

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

  @Column({
    name: 'delivery_method',
    type: 'enum',
    enum: DeliveryMethod,
    enumName: 'delivery_method',
    default: DeliveryMethod.EMAIL,
  })
  deliveryMethod: DeliveryMethod;

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

  @Column({
    name: 'export_format',
    type: 'enum',
    enum: ScheduleExportFormat,
    enumName: 'schedule_export_format',
    default: ScheduleExportFormat.PDF,
  })
  exportFormat: ScheduleExportFormat;

  @Index()
  @Column({ name: 'is_active', type: 'boolean', default: true })
  isActive: boolean;

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

  @Column({
    name: 'last_run_status',
    type: 'enum',
    enum: ScheduleExecutionStatus,
    enumName: 'schedule_execution_status',
    nullable: true,
  })
  lastRunStatus: ScheduleExecutionStatus | null;

  @Index()
  @Column({ name: 'next_run_at', type: 'timestamptz', nullable: true })
  nextRunAt: Date | null;

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

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

  @UpdateDateColumn({ name: 'updated_at', type: 'timestamptz' })
  updatedAt: Date;

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

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

  @OneToMany(() => ReportRecipient, (recipient) => recipient.schedule)
  recipients: ReportRecipient[];

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