export interface DashboardStats {
    critical_count: number;
    high_count: number;
    medium_count: number;
    low_count: number;
    total_vulnerabilities: number;
    open_vulnerabilities: number;
    affected_assets: number;
    total_assets: number;
    scanned_assets: number;
    severity_history: { day: string, count: number }[];
    avg_cvss_score: number;
    oldest_vulnerability_days: number;
}

export interface PriorityBreakdown {
    total: number;
    cvss: number;
    exploit_bonus: number;
    exploit_source: 'kev' | 'euvd' | 'kev_and_euvd' | 'epss' | 'wazuh' | 'ssvc' | 'maturity' | 'none';
    exploit_status?: 'none' | 'poc' | 'active' | 'widespread';  // SSVC exploitation status
    ssvc_addon?: number;                         // bonus from technical_impact + automatable
    ssvc_technical_impact?: 'partial' | 'total' | null;
    ssvc_automatable?: 'yes' | 'no' | null;
    exploit_maturity?: string | null;            // Unproven | PoC | Functional | High
    asset_factor: number;
    age_bonus: number;
    kev_bonus: number;
    euvd_bonus?: number;
    catalog_count?: number;
    cpr?: number | null;
    vpr_score?: number | null;                   // Tenable VPR mirrored into breakdown
    epss_score?: number | null;
    epss_percentile?: number | null;
    kev_listed: boolean;
    kev_ransomware_use: boolean;
    euvd_listed?: boolean;
    euvd_critical?: boolean;
}

export interface VulnerabilityPackage {
    package_name: string;
    package_version?: string | null;
    fixed_version?: string | null;
    source?: string | null;          // wazuh | nessus | manual
    has_fix: boolean;                // True iff fixed_version is real (not equal to installed)
}

export interface Vulnerability {
    id: number;
    cve_id: string;
    severity: 'critical' | 'high' | 'medium' | 'low' | 'none';
    cvss_score: number;
    status: 'open' | 'patched' | 'pending_verification' | 'patch_failed' | 'accepted_risk' | 'false_positive' | 'deferred';
    description: string;
    package_name?: string;
    package_version?: string;
    fixed_version?: string | null;       // when set + status='open' → patch available
    packages?: VulnerabilityPackage[];   // per-package detail (CVE-2023-48795 hits PuTTY + WinSCP both)
    has_fix_any?: boolean;               // at least one package has a real fix (not equal to installed)
    published_date: string;
    detected_at?: string;
    asset_id: number;
    asset_hostname?: string;
    title?: string;
    assigned_user_id?: number | null;
    assigned_user_name?: string | null;
    assigned_group_id?: number | null;
    assigned_group_name?: string | null;
    deferred_until?: string | null;
    defer_reason?: string | null;
    notification_suppressed?: boolean;
    priority_score?: number;
    priority_breakdown?: PriorityBreakdown;
    exploit_available?: boolean;
    exploit_maturity?: string;  // Unproven, PoC, Functional, High (from Nessus)
    exploitation_status?: 'none' | 'poc' | 'active' | 'widespread';  // SSVC (from Vulnrichment)
    exploitation_source?: string;  // nessus, vulrighment, nvd, manual
    ssvc_technical_impact?: 'partial' | 'total' | null;   // SSVC: attacker takeover scope
    ssvc_automatable?: 'yes' | 'no' | null;               // SSVC: mass-exploitation feasibility
    // Multi-scanner source attribution (Nessus integration)
    sources?: string[];                  // ["wazuh"], ["nessus"], ["wazuh","nessus"]
    cross_confirmed?: boolean;           // sources.length >= 2
    first_detected_by?: string | null;
    nessus_plugin_id?: string | null;
    nessus_vpr_score?: number | null;    // Tenable's VPR (0-10)
    is_pseudo_cve?: boolean;             // NESSUS-PLUGIN-* (EOL/Compliance/Cipher)
    // Threat intel enrichment
    epss_score?: number | null;
    epss_percentile?: number | null;
    kev_listed?: boolean;
    kev_ransomware_use?: boolean;
    kev_date_added?: string | null;
    euvd_listed?: boolean;
    euvd_critical?: boolean;
    euvd_date_added?: string | null;
    euvd_id?: string | null;
    cpr_score?: number | null;
    enrichment_sources?: string | null;
    enrichment_updated_at?: string | null;
}

export interface Asset {
    id: number;
    hostname: string;
    ip_address: string;
    operating_system: string;
    os_version?: string;
    last_scan?: string;
    wazuh_agent_id?: string | null;
    status: 'active' | 'inactive';
    policy_id?: number | null;
    description?: string;
    location?: string;
    owner?: string;
    assigned_user_id?: number | null;
    assigned_user_name?: string | null;
    groups?: string[];  // List of group names (M2M relationship)
    policy_name?: string | null;
}

export interface UserInfo {
    id: number;
    username: string;
    email: string;
    role: 'admin' | 'editor' | 'readonly';
}

export interface Group {
    id: number;
    name: string;
    description?: string;
    created_at: string;
    member_count: number;
    members: { id: number; username: string }[];
}

export interface NotificationLogEntry {
    id: number;
    vulnerability_id?: number;
    asset_id?: number;
    user_id?: number;
    notification_type: 'sla_breach' | 'assignment' | 'manual';
    sent_at: string;
    subject?: string;
    recipient_email?: string;
    status: 'sent' | 'failed' | 'suppressed';
    error_message?: string;
    cve_id?: string;
    asset_hostname?: string;
    username?: string;
}

export interface Scan {
    id: number;
    asset_id: number;
    asset_hostname?: string;
    scan_type: 'wazuh' | 'nessus' | 'full' | 'syscollector';  // legacy values kept for old history rows
    status: 'pending' | 'running' | 'completed' | 'failed';
    started_at: string;
    completed_at?: string;
    vulnerabilities_found: number;
}

export interface ScanRunSummary {
    scan_ids: number[];
    started_at: string;
    completed_at?: string;
    scan_type: 'wazuh' | 'nessus' | 'full' | 'syscollector';  // legacy values kept for old history rows
    total_assets: number;
    completed: number;
    failed: number;
    running: number;
    total_vulns_found: number;
    failed_assets: { asset_hostname: string; error_message: string }[];
}

export interface Recommendation {
    cve_id: string;
    asset_hostname: string;
    priority: number;
    justification: string;
    remediation_hint: string;
}

export interface AIPriorityResponse {
    recommendations: Recommendation[];
    global_strategy: string;
    error?: string;
}
