import React from 'react';
import { formatDistanceToNow } from 'date-fns';

interface Mail {
  id: string;
  name: string;
  subject: string;
  email: string;
  date: string;
  text: string;
  read: boolean;
  labels: string[];
}

interface MailListProps {
  items: Mail[];
  selectedId?: string;
  onSelect: (id: string) => void;
}

export function MailList({ items, selectedId, onSelect }: MailListProps) {
  return (
    <div className="space-y-2">
      {items.map((item) => (
        <div
          key={item.id}
          onClick={() => onSelect(item.id)}
          className={`p-4 border rounded cursor-pointer ${selectedId === item.id ? 'bg-gray-100' : ''}`}
        >
          <div className="flex justify-between items-start">
            <div className="flex items-center space-x-3">
              <div className={`w-2 h-2 rounded-full ${item.read ? 'bg-transparent' : 'bg-blue-500'}`} />
              <h3 className="font-medium">{item.name}</h3>
            </div>
            <span className="text-sm text-gray-500">
              {formatDistanceToNow(new Date(item.date), { addSuffix: true })}
            </span>
          </div>
          <h4 className="font-medium mt-1">{item.subject}</h4>
          <p className="text-sm text-gray-500 mt-1 truncate">
            {item.text.substring(0, 100)}...
          </p>
          {item.labels.length > 0 && (
            <div className="flex flex-wrap gap-1 mt-2">
              {item.labels.map((label) => (
                <span key={label} className="px-2 py-1 text-xs bg-gray-200 rounded">
                  {label}
                </span>
              ))}
            </div>
          )}
        </div>
      ))}
    </div>
  );
}
