"use client";

import React, { useState, useEffect } from 'react';
import { Search, BookOpen, Tag, Trash2, Star, Archive, X, Folder, FileText, Calendar, Heart, Users, LucideIcon, ChevronRight } from 'lucide-react';

interface NavigationProps {
  nextcloudFolders: string[];
  onFolderSelect: (folder: string) => void;
}

type FolderType = 'Notes' | 'Diary' | 'Health' | 'Contacts';

interface FolderConfig {
  icon: LucideIcon;
  order: number;
  displayName: string;
}

// Define folder order, icons and display names
const FOLDER_CONFIG: Record<FolderType, FolderConfig> = {
  'Notes': { icon: FileText, order: 1, displayName: 'Bloc-notes' },
  'Diary': { icon: Calendar, order: 2, displayName: 'Journal' },
  'Health': { icon: Heart, order: 3, displayName: 'Carnet de santé' },
  'Contacts': { icon: Users, order: 4, displayName: 'Carnet d\'adresses' }
};

interface ContactFile {
  id: string;
  filename: string;
  basename: string;
  lastmod: string;
}

export default function Navigation({ nextcloudFolders, onFolderSelect }: NavigationProps) {
  const [searchQuery, setSearchQuery] = useState('');
  const [expandedContacts, setExpandedContacts] = useState(false);
  const [contactFiles, setContactFiles] = useState<ContactFile[]>([]);
  const [isLoadingContacts, setIsLoadingContacts] = useState(false);

  const getFolderIcon = (folder: string) => {
    switch (folder) {
      case 'Notes':
        return FileText;
      case 'Diary':
        return Calendar;
      case 'Health':
        return Heart;
      case 'Contacts':
        return Users;
      default:
        return FileText;
    }
  };

  // Sort folders according to the specified order
  const sortedFolders = [...nextcloudFolders].sort((a, b) => {
    const orderA = (FOLDER_CONFIG[a as FolderType]?.order) || 999;
    const orderB = (FOLDER_CONFIG[b as FolderType]?.order) || 999;
    return orderA - orderB;
  });

  const fetchContactFiles = async () => {
    try {
      setIsLoadingContacts(true);
      const response = await fetch('/api/nextcloud/files?folder=Contacts');
      if (response.ok) {
        const files = await response.json();
        console.log('Received files:', files); // Debug log
        // Filter for VCF files and map to ContactFile interface
        const vcfFiles = files
          .filter((file: any) => file.basename.endsWith('.vcf'))
          .map((file: any) => ({
            id: file.etag,
            filename: file.filename,
            basename: file.basename,
            lastmod: file.lastmod
          }));
        console.log('Processed VCF files:', vcfFiles); // Debug log
        setContactFiles(vcfFiles);
      }
    } catch (error) {
      console.error('Error fetching contact files:', error);
      setContactFiles([]);
    } finally {
      setIsLoadingContacts(false);
    }
  };

  useEffect(() => {
    if (expandedContacts) {
      fetchContactFiles();
    }
  }, [expandedContacts]);

  // Debug log for contactFiles state
  useEffect(() => {
    console.log('Current contactFiles state:', contactFiles);
  }, [contactFiles]);

  return (
    <div className="flex flex-col h-full bg-carnet-sidebar">
      {/* Search */}
      <div className="p-4">
        <div className="flex items-center gap-2 mb-2">
          <FileText className="h-6 w-6 text-carnet-text-primary" />
          <span className="text-xl font-semibold text-carnet-text-primary">PAGES</span>
        </div>
        <div className="relative">
          <input
            type="text"
            value={searchQuery}
            onChange={(e) => setSearchQuery(e.target.value)}
            placeholder="Recherche..."
            className="w-full pl-9 pr-4 py-2 bg-white border border-carnet-border rounded-md text-sm text-carnet-text-primary placeholder-carnet-text-muted focus:outline-none focus:ring-1 focus:ring-primary"
          />
          <Search className="absolute left-3 top-2.5 h-4 w-4 text-carnet-text-muted" />
          {searchQuery && (
            <button
              onClick={() => setSearchQuery('')}
              className="absolute right-3 top-2.5 text-carnet-text-muted hover:text-carnet-text-primary"
            >
              <X className="h-4 w-4" />
            </button>
          )}
        </div>
      </div>

      {/* Folders */}
      <div className="flex-1 overflow-y-auto p-4">
        <h2 className="text-sm font-semibold text-carnet-text-primary mb-2">VUES</h2>
        <div className="space-y-1">
          {sortedFolders.map((folder) => {
            const config = FOLDER_CONFIG[folder as FolderType];
            const Icon = config?.icon || FileText;
            const displayName = config?.displayName || folder;
            
            return (
              <div key={folder}>
                <button
                  onClick={() => {
                    if (folder === 'Contacts') {
                      setExpandedContacts(!expandedContacts);
                    } else {
                      onFolderSelect(folder);
                    }
                  }}
                  className="w-full flex items-center px-3 py-2 text-sm rounded-md text-carnet-text-primary hover:bg-carnet-hover"
                >
                  <Icon className="h-4 w-4 flex-shrink-0" />
                  <span className="ml-2 truncate">{displayName}</span>
                  {folder === 'Contacts' && (
                    <ChevronRight 
                      className={`h-4 w-4 flex-shrink-0 ml-auto transition-transform ${
                        expandedContacts ? 'transform rotate-90' : ''
                      }`}
                    />
                  )}
                </button>
                {folder === 'Contacts' && expandedContacts && (
                  <div className="ml-4 mt-1 space-y-1">
                    {isLoadingContacts ? (
                      <div className="px-3 py-2 text-sm text-carnet-text-muted">Chargement...</div>
                    ) : contactFiles.length === 0 ? (
                      <div className="px-3 py-2 text-sm text-carnet-text-muted">Aucun contact</div>
                    ) : (
                      contactFiles.map((file) => {
                        console.log('Rendering contact file:', file); // Debug log
                        return (
                          <button
                            key={file.id}
                            onClick={() => {
                              // When clicking a VCF file, we want to select the Contacts folder
                              // and pass the file information to the parent component
                              onFolderSelect('Contacts');
                              // You might want to add a callback here to handle the selected contact
                            }}
                            className="w-full flex items-center space-x-2 px-3 py-2 text-sm rounded-md text-carnet-text-muted hover:bg-carnet-hover"
                          >
                            <span>{file.basename.replace('.vcf', '')}</span>
                          </button>
                        );
                      })
                    )}
                  </div>
                )}
              </div>
            );
          })}
        </div>
      </div>
    </div>
  );
} 