import { Entity, Column, PrimaryGeneratedColumn, CreateDateColumn, UpdateDateColumn, ManyToOne, OneToMany, JoinColumn, Index } from 'typeorm';
import { MonitoringCheckType, OperationalStatus, WebsiteCategory } from '../enums/website-monitoring.enum';
import { BreachCheckOrganization } from './breach-check-organization.entity';
import { BreachCheckResult } from './breach-check-result.entity';

/**
 * Entity representing a monitored website in the system
 */
@Entity('breach_check_websites')
export class BreachCheckWebsites {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ length: 255 })
  @Index()
  name: string;

  @Column({ name: 'url' })
  @Index({ unique: true })
  url: string;

  @Column({
    type: 'enum',
    enum: WebsiteCategory,
    default: WebsiteCategory.OTHER,
  })
  @Index()
  category: WebsiteCategory;

  @Column({
    name: 'monitoring_frequency_minutes',
    type: 'int',
    default: 60,
  })
  monitoringFrequencyMinutes: number;

  @Column({
    name: 'stage',
    type: 'enum',
    enum: ['Scanned', 'Review', 'Archived', 'Deleted'],
  })
  @Index()
  stage: 'Scanned' | 'Review' | 'Archived' | 'Deleted';

  @Column({
    name: 'is_false_positive',
    type: 'boolean',
    default: false,
  })
  isFalsePositive: boolean;

  @Column({
    name: 'last_monitored_at',
    type: 'timestamp',
    nullable: true,
  })
  lastMonitoredAt: Date | null;

  @Column({
    type: 'enum',
    enum: OperationalStatus,
    default: OperationalStatus.UNKNOWN,
  })
  @Index()
  operationalStatus: OperationalStatus;

  @Column({
    type: 'simple-array',
  })
  enabledChecks: MonitoringCheckType[];

  @Column({ name: 'receive_alerts', default: true })
  receiveAlerts: boolean;

  // Custom score thresolds for alerts
  @Column({
    name: 'alert_threshold',
    type: 'int',
    default: 50,
  })
  alertThreshold: number;

  @Column({ nullable: true })
  notes: string | null;

  @Column('simple-array')
  tags: string[];

  @Column({ name: 'organization_id', nullable: false })
  organizationId: number | null;

  @ManyToOne(() => BreachCheckOrganization, organization => organization.websites)
  @JoinColumn({ name: 'organization_id' })
  organization: BreachCheckOrganization;

  @OneToMany(() => BreachCheckResult, checkResult => checkResult.website)
  checkResults: BreachCheckResult[];

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

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

  @Column({
    name: 'check_content_with_ai',
    type: 'boolean',
    default: false,
  })
  checkContentWithAI: boolean;
}
