import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn, Check } from 'typeorm';
import { License } from './license.entity';

@Entity('scheduled_tasks')
@Check(`"task_type" IN ('restart', 'announcement', 'command', 'plugin_reload')`)
export class ScheduledTask {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ type: 'uuid' })
  license_id: string;

  @Column({ type: 'varchar', length: 30 })
  task_type: string;

  @Column({ type: 'varchar', length: 100 })
  task_name: string;

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

  @Column({ type: 'varchar', length: 50, default: 'America/New_York' })
  timezone: string;

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

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

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

  @Column({ type: 'timestamptz', default: () => 'NOW()' })
  created_at: Date;

  @ManyToOne(() => License, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'license_id' })
  license: License;
}
