import React, { useState, useRef, useCallback } from 'react';

interface FileUploadPreviewProps {
    files: string[];
    onRemoveFile: (index: number) => void;
}

const FileUploadPreview: React.FC<FileUploadPreviewProps> = ({ files, onRemoveFile }) => {
    if (files.length === 0) return null;

    const getFileIcon = (fileName: string) => {
        const extension = fileName.split('.').pop()?.toLowerCase();

        switch (extension) {
            case 'pdf':
                return (
                    <div className="w-6 h-6 bg-red-500 rounded flex items-center justify-center">
                        <span className="text-white text-xs font-bold">P</span>
                    </div>
                );
            case 'doc':
            case 'docx':
                return (
                    <div className="w-6 h-6 bg-blue-500 rounded flex items-center justify-center">
                        <span className="text-white text-xs font-bold">W</span>
                    </div>
                );
            case 'xls':
            case 'xlsx':
                return (
                    <div className="w-6 h-6 bg-green-500 rounded flex items-center justify-center">
                        <span className="text-white text-xs font-bold">E</span>
                    </div>
                );
            case 'jpg':
            case 'jpeg':
            case 'png':
            case 'gif':
                return (
                    <div className="w-6 h-6 bg-purple-500 rounded flex items-center justify-center">
                        <svg width="12" height="12" viewBox="0 0 12 12" fill="none" xmlns="http://www.w3.org/2000/svg">
                            <path d="M1 9L3.5 6.5L5 8L8.5 4.5L11 7M1 1H11V11H1V1Z" stroke="white" strokeWidth="1" strokeLinecap="round" strokeLinejoin="round" />
                        </svg>
                    </div>
                );
            default:
                return (
                    <div className="w-6 h-6 bg-gray-500 rounded flex items-center justify-center">
                        <svg width="12" height="12" viewBox="0 0 12 12" fill="none" xmlns="http://www.w3.org/2000/svg">
                            <path d="M7 1H2C1.44772 1 1 1.44772 1 2V10C1 10.5523 1.44772 11 2 11H10C10.5523 11 11 10.5523 11 10V5M7 1L11 5M7 1V5H11" stroke="white" strokeWidth="1" strokeLinecap="round" strokeLinejoin="round" />
                        </svg>
                    </div>
                );
        }
    };

    return (
        <div className="mb-3 flex flex-wrap gap-2">
            {files.map((file, index) => (
                <div
                    key={index}
                    className="inline-flex items-center gap-2 px-3 py-2 bg-[var(--Neutrals-NeutralSlate100)] rounded-lg hover:bg-[var(--Neutrals-NeutralSlate200)] transition-colors group"
                >
                    {getFileIcon(file)}
                    <span className="text-sm text-[var(--Neutrals-NeutralSlate700)] max-w-[150px] truncate">{file}</span>
                    <button
                        onClick={() => onRemoveFile(index)}
                        className="w-5 h-5 text-[var(--Neutrals-NeutralSlate400)] hover:text-red-500 hover:bg-red-50 rounded transition-colors flex items-center justify-center"
                        title="Remove file"
                    >
                        <svg width="12" height="12" viewBox="0 0 12 12" fill="none" xmlns="http://www.w3.org/2000/svg">
                            <path d="M9 3L3 9M3 3L9 9" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" />
                        </svg>
                    </button>
                </div>
            ))}
        </div>
    );
};

interface FileUploadDropzoneProps {
    onFilesSelected: (files: File[]) => void;
    children: React.ReactNode;
    accept?: string;
    multiple?: boolean;
    disabled?: boolean;
}

const FileUploadDropzone: React.FC<FileUploadDropzoneProps> = ({
    onFilesSelected,
    children,
    accept = "*/*",
    multiple = true,
    disabled = false
}) => {
    const [isDragOver, setIsDragOver] = useState(false);
    const fileInputRef = useRef<HTMLInputElement>(null);

    const handleDragOver = useCallback((e: React.DragEvent) => {
        e.preventDefault();
        if (!disabled) {
            setIsDragOver(true);
        }
    }, [disabled]);

    const handleDragLeave = useCallback((e: React.DragEvent) => {
        e.preventDefault();
        setIsDragOver(false);
    }, []);

    const handleDrop = useCallback((e: React.DragEvent) => {
        e.preventDefault();
        setIsDragOver(false);

        if (disabled) return;

        const files = Array.from(e.dataTransfer.files);
        if (files.length > 0) {
            onFilesSelected(files);
        }
    }, [onFilesSelected, disabled]);

    const handleFileSelect = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
        const files = Array.from(e.target.files || []);
        if (files.length > 0) {
            onFilesSelected(files);
        }
        // Reset input value to allow selecting the same file again
        if (fileInputRef.current) {
            fileInputRef.current.value = '';
        }
    }, [onFilesSelected]);

    const handleClick = () => {
        if (!disabled && fileInputRef.current) {
            fileInputRef.current.click();
        }
    };

    return (
        <div
            onDragOver={handleDragOver}
            onDragLeave={handleDragLeave}
            onDrop={handleDrop}
            className={`
        relative transition-all
        ${isDragOver ? 'opacity-80' : ''}
        ${disabled ? 'cursor-not-allowed opacity-50' : ''}
      `}
        >
            <input
                ref={fileInputRef}
                type="file"
                accept={accept}
                multiple={multiple}
                onChange={handleFileSelect}
                className="hidden"
                disabled={disabled}
            />

            {children}

            {/* Drag overlay */}
            {isDragOver && (
                <div className="absolute inset-0 bg-[var(--Brand-Orange)]/10 border-2 border-dashed border-[var(--Brand-Orange)] rounded-xl flex items-center justify-center">
                    <div className="text-[var(--Brand-Orange)] font-medium">Drop files here</div>
                </div>
            )}
        </div>
    );
};

interface FileUploadInputProps {
    value: string;
    onChange: (value: string) => void;
    onKeyDown?: (e: React.KeyboardEvent) => void;
    placeholder?: string;
    disabled?: boolean;
    uploadedFiles: string[];
    onRemoveFile: (index: number) => void;
    onFilesSelected: (files: File[]) => void;
}

const FileUploadInput: React.FC<FileUploadInputProps> = ({
    value,
    onChange,
    onKeyDown,
    placeholder = "Ask about your team's performance, culture, or any insights...",
    disabled = false,
    uploadedFiles,
    onRemoveFile,
    onFilesSelected
}) => {
    const fileInputRef = useRef<HTMLInputElement>(null);

    const handleFilesSelected = (files: File[]) => {
        // For demo purposes, we'll just add the file names
        // In a real implementation, you'd upload the files and get URLs back
        const fileNames = files.map(file => file.name);
        onFilesSelected(files);
    };

    const handleFileInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
        const files = Array.from(e.target.files || []);
        if (files.length > 0) {
            handleFilesSelected(files);
        }
        // Reset input value to allow selecting the same file again
        if (fileInputRef.current) {
            fileInputRef.current.value = '';
        }
    };

    const handleUploadClick = () => {
        if (!disabled && fileInputRef.current) {
            fileInputRef.current.click();
        }
    };

    return (
        <div className="w-full">
            {/* File Upload Preview */}
            <FileUploadPreview files={uploadedFiles} onRemoveFile={onRemoveFile} />

            {/* Hidden File Input */}
            <input
                ref={fileInputRef}
                type="file"
                accept=".pdf,.doc,.docx,.xls,.xlsx,.txt,.jpg,.jpeg,.png,.gif"
                multiple
                onChange={handleFileInputChange}
                className="hidden"
                disabled={disabled}
            />

            {/* Input Field with File Upload */}
            <FileUploadDropzone
                onFilesSelected={handleFilesSelected}
                disabled={disabled}
                accept=".pdf,.doc,.docx,.xls,.xlsx,.txt,.jpg,.jpeg,.png,.gif"
            >
                <div className="relative flex items-end gap-3">
                    <div className="flex-1 relative">
                        <textarea
                            value={value}
                            onChange={(e) => onChange(e.target.value)}
                            onKeyDown={onKeyDown}
                            onClick={(e) => e.stopPropagation()}
                            placeholder={placeholder}
                            disabled={disabled}
                            className="w-full min-h-[44px] max-h-32 px-4 py-3 pr-12 border border-[var(--Neutrals-NeutralSlate200)] rounded-xl resize-none focus:outline-none focus:border-[var(--Brand-Orange)] focus:ring-1 focus:ring-[var(--Brand-Orange)] disabled:opacity-50 disabled:cursor-not-allowed transition-colors bg-[var(--Neutrals-NeutralSlate0)] text-[var(--Neutrals-NeutralSlate950)]"
                            rows={1}
                        />

                        {/* File Upload Button */}
                        <button
                            type="button"
                            disabled={disabled}
                            onClick={(e) => {
                                e.stopPropagation();
                                handleUploadClick();
                            }}
                            className="absolute right-3 top-3 w-6 h-6 text-[var(--Neutrals-NeutralSlate400)] hover:text-[var(--Brand-Orange)] disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
                            title="Upload files"
                        >
                            <svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                                <path d="M21 15V19C21 19.5304 20.7893 20.0391 20.4142 20.4142C20.0391 20.7893 19.5304 21 19 21H5C4.46957 21 3.96086 20.7893 3.58579 20.4142C3.21071 20.0391 3 19.5304 3 19V15M17 8L12 3M12 3L7 8M12 3V15" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" />
                            </svg>
                        </button>
                    </div>
                </div>
            </FileUploadDropzone>
        </div>
    );
};

export default FileUploadInput;
export { FileUploadPreview, FileUploadDropzone };