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

export type MonitorTargetType =
  | 'http'
  | 'tcp'
  | 'redis'
  | 'rabbitmq'
  | 'elasticsearch'
  | 'java-app';

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

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

  @Column({ type: 'varchar', length: 32 })
  type!: MonitorTargetType;

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

  @Column({ type: 'varchar', length: 64, default: '*/5 * * * *' })
  cronExpr!: string;

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

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

  @Column({ name: 'config_json', type: 'text' })
  configJson!: string;

  @Column({ name: 'secret_json', type: 'text', nullable: true })
  secretJson!: string | null;

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

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

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

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