import { Column, CreateDateColumn, Entity, Index, JoinColumn, ManyToOne, PrimaryGeneratedColumn, UpdateDateColumn } from 'typeorm';
import { BreachCheckWebsites } from './breach-check-website.entity';

/**
 * Entity representing website check baselines with optimized hash-first strategy
 */
@Entity('breach_check_website_baselines')
export class BreachCheckWebsiteBaseline {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ name: 'website_id' })
  @Index({ unique: true })
  websiteId: number;

  @ManyToOne(() => BreachCheckWebsites, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'website_id' })
  website: BreachCheckWebsites;

  // ===== PRIMARY HASH FOR QUICK COMPARISON =====
  @Column({
    name: 'main_content_hash',
    length: 64,
    nullable: true,
    comment: 'Primary MD5 hash of main content (title|headings|meta|body) for instant comparison',
  })
  @Index() // Index for fast lookups
  mainContentHash: string | null;

  // ===== CONTENT DATA AND DETAILED HASHES =====
  @Column({
    type: 'text',
    nullable: true,
    comment: 'JSON snapshot of content signature for detailed analysis',
  })
  contentSnapshot: string | null;

  @Column({
    name: 'title_hash',
    length: 64,
    nullable: true,
    comment: 'Hash of page title',
  })
  titleHash: string | null;

  @Column({
    name: 'structure_hash',
    length: 64,
    nullable: true,
    comment: 'Hash of page structure (DOM elements count)',
  })
  structureHash: string | null;

  @Column({
    name: 'body_text_hash',
    length: 64,
    nullable: true,
    comment: 'Hash of main body text content',
  })
  bodyTextHash: string | null;

  @Column({
    name: 'meta_desc_hash',
    length: 64,
    nullable: true,
    comment: 'Hash of meta description',
  })
  metaDescHash: string | null;

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

  // ===== HTTP STATUS =====
  @Column({
    name: 'http_status_baseline',
    type: 'integer',
    nullable: true,
    comment: 'Expected HTTP status code',
  })
  httpStatusBaseline: number | null;

  // ===== OPTIMIZED CRAWLING AND ROUTES =====
  @Column({
    name: 'discovered_routes',
    type: 'json',
    nullable: true,
    comment: 'Array of discovered routes with their content hashes',
  })
  discoveredRoutes: Array<{
    path: string;
    contentHash: string;
    lastChecked: Date;
    status: 'active' | 'inactive' | 'changed';
    httpStatus?: number;
  }> | null;

  @Column({
    name: 'routes_hash',
    length: 64,
    nullable: true,
    comment: 'Hash of all routes structure for detecting route changes',
  })
  @Index() // Index for fast lookups
  routesHash: string | null;

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

  // ===== CONFIGURATION =====
  @Column({
    name: 'ai_analysis_enabled',
    type: 'boolean',
    default: true,
    comment: 'Whether to use AI analysis for content changes',
  })
  aiAnalysisEnabled: boolean;

  @Column({
    name: 'crawling_enabled',
    type: 'boolean',
    default: true,
    comment: 'Whether to enable route crawling and monitoring',
  })
  crawlingEnabled: boolean;

  @Column({
    name: 'max_crawl_depth',
    type: 'integer',
    default: 2,
    comment: 'Maximum depth for route crawling',
  })
  maxCrawlDepth: number;

  @Column({
    name: 'max_crawl_urls',
    type: 'integer',
    default: 20,
    comment: 'Maximum number of URLs to crawl',
  })
  maxCrawlUrls: number;

  // ===== METADATA =====
  @Column({
    name: 'last_updated',
    type: 'timestamp',
    nullable: true,
    comment: 'Timestamp for tracking baseline refresh periods',
  })
  lastUpdated: Date | null;

  @Column({
    name: 'baseline_version',
    type: 'integer',
    default: 1,
    comment: 'Version number of the baseline, incremented with significant updates',
  })
  baselineVersion: number;

  @Column({
    name: 'sensitivity_level',
    type: 'integer',
    default: 50,
    comment: 'Sensitivity level for defacement detection (0-100)',
  })
  sensitivityLevel: number;

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

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