import {
  Column,
  CreateDateColumn,
  Entity,
  OneToMany,
  PrimaryColumn,
  UpdateDateColumn,
} from 'typeorm';
import { randomUUID } from 'crypto';
import { InspectionRunEntity } from '../runs/inspection-run.entity';

@Entity({ name: 'inspection_task' })
export class InspectionTaskEntity {
  @PrimaryColumn({ type: 'varchar', length: 64 })
  id: string = randomUUID();

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

  @Column({ name: 'site_url', type: 'varchar', length: 1024 })
  siteUrl!: string;

  @Column({ name: 'cron_expr', type: 'varchar', length: 64 })
  cronExpr!: string;

  @Column({ name: 'steps_json', type: 'text' })
  stepsJson!: string;

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

  @Column({ name: 'preferred_executor', type: 'varchar', length: 32, default: 'browser' })
  preferredExecutor!: 'browser' | 'agent-s';

  @Column({ name: 'allow_agent_fallback', type: 'boolean', default: false })
  allowAgentFallback!: boolean;

  @Column({ type: 'varchar', length: 16, default: 'enabled' })
  status!: 'enabled' | 'disabled';

  @CreateDateColumn({ name: 'created_at' })
  createdAt!: Date;

  @UpdateDateColumn({ name: 'updated_at' })
  updatedAt!: Date;

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

  @OneToMany(() => InspectionRunEntity, (run) => run.task)
  runs!: InspectionRunEntity[];
}
