'use client';

import { Task, TaskStatus } from '@/lib/types';
import { KanbanColumn } from './Column';
import { useMemo } from 'react';

interface KanbanBoardProps {
  tasks: Task[];
}

export function KanbanBoard({ tasks }: KanbanBoardProps) {
  // Organiser les tâches par statut
  const tasksByStatus = useMemo(() => {
    const grouped = tasks.reduce((acc, task) => {
      if (!acc[task.status]) {
        acc[task.status] = [];
      }
      acc[task.status].push(task);
      return acc;
    }, {} as Record<TaskStatus, Task[]>);

    return grouped;
  }, [tasks]);

  // Configuration des colonnes
  const columns: Array<{
    id: TaskStatus;
    title: string;
    color: string;
    tasks: Task[];
  }> = [
    {
      id: 'todo',
      title: 'À faire',
      color: 'gray',
      tasks: tasksByStatus.todo || []
    },
    {
      id: 'in_progress',
      title: 'En cours',
      color: 'blue',
      tasks: tasksByStatus.in_progress || []
    },
    {
      id: 'done',
      title: 'Terminé',
      color: 'green',
      tasks: tasksByStatus.done || []
    },
    {
      id: 'cancelled',
      title: 'Annulé',
      color: 'red',
      tasks: tasksByStatus.cancelled || []
    }
  ];

  return (
    <div className="w-full flex gap-4 overflow-x-auto pb-6">
      {columns.map((column) => (
        <KanbanColumn
          key={column.id}
          id={column.id}
          title={column.title}
          color={column.color}
          tasks={column.tasks}
        />
      ))}
    </div>
  );
}
