/**
 * AntivirusScanStatus Component
 * Displays the antivirus scan result badge/status for uploaded files.
 * Shows ClamAV scan result and XSS content scan result.
 */

import React from 'react';

// ── Types ──

export interface ScanResultData {
    malwareScan?: {
        scanned: boolean;
        isInfected: boolean;
        skipped?: boolean;
        virusNames?: string[];
        scanDuration?: number;
        error?: string;
    };
    contentScan?: {
        scanned: boolean;
        safe: boolean;
        scanType: string;
        severity: 'SAFE' | 'LOW' | 'MEDIUM' | 'HIGH' | 'CRITICAL';
        threats?: Array<{ description: string; severity: string }>;
        patternsChecked: number;
    };
    scanEventId?: string;
}

interface AntivirusScanStatusProps {
    scanResult?: ScanResultData;
    compact?: boolean;
    className?: string;
}

// ── Helpers ──

function getStatusColor(result?: ScanResultData): string {
    if (!result) return '#94a3b8'; // gray — no scan data

    // Check malware first
    if (result.malwareScan?.isInfected) return '#ef4444'; // red
    if (result.malwareScan?.error) return '#f59e0b'; // amber

    // Then XSS
    if (result.contentScan && !result.contentScan.safe) {
        if (result.contentScan.severity === 'CRITICAL') return '#ef4444';
        if (result.contentScan.severity === 'HIGH') return '#ef4444';
        if (result.contentScan.severity === 'MEDIUM') return '#f59e0b';
        return '#f59e0b';
    }

    // Skipped
    if (result.malwareScan?.skipped) return '#94a3b8';

    return '#22c55e'; // green — all clear
}

function getStatusIcon(result?: ScanResultData): string {
    if (!result) return '⏳';
    if (result.malwareScan?.isInfected) return '🛑';
    if (result.contentScan && !result.contentScan.safe) return '⚠️';
    if (result.malwareScan?.skipped) return '⏭️';
    if (result.malwareScan?.error) return '❌';
    if (result.malwareScan?.scanned && result.contentScan?.scanned) return '✅';
    return '⏳';
}

function getStatusLabel(result?: ScanResultData): string {
    if (!result) return 'Pending scan';
    if (result.malwareScan?.isInfected) return 'Malware detected';
    if (result.contentScan && !result.contentScan.safe) return 'Content threat detected';
    if (result.malwareScan?.skipped) return 'Scan skipped';
    if (result.malwareScan?.error) return 'Scan error';
    if (result.malwareScan?.scanned && result.contentScan?.scanned) return 'Clean';
    return 'Scanning…';
}

// ── Component ──

const AntivirusScanStatus: React.FC<AntivirusScanStatusProps> = ({
    scanResult,
    compact = false,
    className = '',
}) => {
    const color = getStatusColor(scanResult);
    const icon = getStatusIcon(scanResult);
    const label = getStatusLabel(scanResult);

    // Compact mode: just a badge
    if (compact) {
        return (
            <span
                className={className}
                style={{
                    display: 'inline-flex',
                    alignItems: 'center',
                    gap: '4px',
                    padding: '2px 8px',
                    borderRadius: '12px',
                    fontSize: '12px',
                    fontWeight: 500,
                    backgroundColor: `${color}15`,
                    color,
                    border: `1px solid ${color}30`,
                }}
                title={label}
            >
                <span style={{ fontSize: '11px' }}>{icon}</span>
                {label}
            </span>
        );
    }

    // Full mode: detailed card
    return (
        <div
            className={className}
            style={{
                border: `1px solid ${color}30`,
                borderRadius: '8px',
                padding: '12px 16px',
                backgroundColor: `${color}08`,
                fontSize: '13px',
            }}
        >
            {/* Header */}
            <div style={{ display: 'flex', alignItems: 'center', gap: '8px', marginBottom: '8px' }}>
                <span style={{ fontSize: '16px' }}>{icon}</span>
                <span style={{ fontWeight: 600, color }}>{label}</span>
                {scanResult?.malwareScan?.scanDuration && (
                    <span style={{ marginLeft: 'auto', fontSize: '11px', color: '#94a3b8' }}>
                        {scanResult.malwareScan.scanDuration}ms
                    </span>
                )}
            </div>

            {/* Details */}
            {scanResult && (
                <div style={{ display: 'flex', flexDirection: 'column', gap: '4px' }}>
                    {/* ClamAV Result */}
                    {scanResult.malwareScan?.scanned && (
                        <div style={{ display: 'flex', alignItems: 'center', gap: '6px', fontSize: '12px', color: '#64748b' }}>
                            <span>🦠</span>
                            <span>
                                ClamAV:{' '}
                                {scanResult.malwareScan.isInfected
                                    ? `Infected — ${scanResult.malwareScan.virusNames?.join(', ')}`
                                    : 'Clean'}
                            </span>
                        </div>
                    )}

                    {/* XSS Result */}
                    {scanResult.contentScan?.scanned && (
                        <div style={{ display: 'flex', alignItems: 'center', gap: '6px', fontSize: '12px', color: '#64748b' }}>
                            <span>🔍</span>
                            <span>
                                Content scan ({scanResult.contentScan.scanType}):{' '}
                                {scanResult.contentScan.safe
                                    ? `Safe — ${scanResult.contentScan.patternsChecked} patterns checked`
                                    : `${scanResult.contentScan.threats?.length || 0} threats found (${scanResult.contentScan.severity})`}
                            </span>
                        </div>
                    )}

                    {/* Threats list */}
                    {scanResult.contentScan?.threats && scanResult.contentScan.threats.length > 0 && (
                        <ul style={{ margin: '4px 0 0 24px', padding: 0, fontSize: '11px', color: '#ef4444' }}>
                            {scanResult.contentScan.threats.slice(0, 5).map((threat, i) => (
                                <li key={i}>
                                    {threat.description} ({threat.severity})
                                </li>
                            ))}
                            {scanResult.contentScan.threats.length > 5 && (
                                <li>…and {scanResult.contentScan.threats.length - 5} more</li>
                            )}
                        </ul>
                    )}

                    {/* Scan event ID */}
                    {scanResult.scanEventId && (
                        <div style={{ fontSize: '10px', color: '#94a3b8', marginTop: '4px' }}>
                            Scan ID: {scanResult.scanEventId}
                        </div>
                    )}
                </div>
            )}
        </div>
    );
};

export default AntivirusScanStatus;
