'use client';

import { useState } from 'react';
import { KanbanBoardContainer } from '@/components/kanban/BoardContainer';
import { Header } from '@/components/ui/Header';
import { TasksProvider, useTasksContext } from '@/contexts/TasksContext';
import { useUserPreferences } from '@/contexts/UserPreferencesContext';
import { Task, Tag } from '@/lib/types';
import { CreateTaskData } from '@/clients/tasks-client';
import { CreateTaskForm } from '@/components/forms/CreateTaskForm';
import { Button } from '@/components/ui/Button';
import { JiraQuickFilter } from '@/components/kanban/JiraQuickFilter';
import { FontSizeToggle } from '@/components/ui/FontSizeToggle';

interface KanbanPageClientProps {
  initialTasks: Task[];
  initialTags: (Tag & { usage: number })[];
}

function KanbanPageContent() {
  const { syncing, createTask, activeFiltersCount, kanbanFilters, setKanbanFilters } = useTasksContext();
  const { preferences, updateViewPreferences } = useUserPreferences();
  const [isCreateModalOpen, setIsCreateModalOpen] = useState(false);

  // Extraire les préférences du context
  const showFilters = preferences.viewPreferences.showFilters;
  const showObjectives = preferences.viewPreferences.showObjectives;
  const compactView = preferences.viewPreferences.compactView;
  const swimlanesByTags = preferences.viewPreferences.swimlanesByTags;

  // Handlers pour les toggles (sauvegarde automatique via le context)
  const handleToggleFilters = () => {
    updateViewPreferences({ showFilters: !showFilters });
  };

  const handleToggleObjectives = () => {
    updateViewPreferences({ showObjectives: !showObjectives });
  };

  const handleToggleCompactView = () => {
    updateViewPreferences({ compactView: !compactView });
  };

  const handleToggleSwimlanes = () => {
    updateViewPreferences({ swimlanesByTags: !swimlanesByTags });
  };

  // Handler pour la création de tâche depuis la barre de contrôles
  const handleCreateTask = async (data: CreateTaskData) => {
    await createTask(data);
    setIsCreateModalOpen(false);
  };

  return (
    <div className="min-h-screen bg-[var(--background)]">
      <Header 
        title="Kanban Board" 
        subtitle="Gestionnaire de tâches"
        syncing={syncing}
      />
      
      {/* Barre de contrôles de visibilité */}
      <div className="bg-[var(--card)]/30 border-b border-[var(--border)]/30">
        <div className="container mx-auto px-6 py-2">
          <div className="flex items-center justify-between w-full">
            <div className="flex items-center gap-4">
              <div className="flex items-center gap-2">
                <button
                  onClick={handleToggleFilters}
                  className={`flex items-center gap-2 px-3 py-1.5 rounded-md text-sm font-mono transition-all ${
                    showFilters 
                      ? 'bg-[var(--primary)]/20 text-[var(--primary)] border border-[var(--primary)]/30' 
                      : 'bg-[var(--card)] text-[var(--muted-foreground)] border border-[var(--border)] hover:border-[var(--primary)]/50'
                  }`}
                >
                  <svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 6V4m0 2a2 2 0 100 4m0-4a2 2 0 110 4m-6 8a2 2 0 100-4m0 4a2 2 0 100 4m0-4v2m0-6V4m6 6v10m6-2a2 2 0 100-4m0 4a2 2 0 100 4m0-4v2m0-6V4" />
                  </svg>
                  Filtres{activeFiltersCount > 0 && ` (${activeFiltersCount})`}
                </button>
                
                <button
                  onClick={handleToggleObjectives}
                  className={`flex items-center gap-2 px-3 py-1.5 rounded-md text-sm font-mono transition-all ${
                    showObjectives 
                      ? 'bg-[var(--accent)]/20 text-[var(--accent)] border border-[var(--accent)]/30' 
                      : 'bg-[var(--card)] text-[var(--muted-foreground)] border border-[var(--border)] hover:border-[var(--accent)]/50'
                  }`}
                >
                  <svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 12l2 2 4-4M7.835 4.697a3.42 3.42 0 001.946-.806 3.42 3.42 0 014.438 0 3.42 3.42 0 001.946.806 3.42 3.42 0 013.138 3.138 3.42 3.42 0 00.806 1.946 3.42 3.42 0 010 4.438 3.42 3.42 0 00-.806 1.946 3.42 3.42 0 01-3.138 3.138 3.42 3.42 0 00-1.946.806 3.42 3.42 0 01-4.438 0 3.42 3.42 0 00-1.946-.806 3.42 3.42 0 01-3.138-3.138 3.42 3.42 0 00-.806-1.946 3.42 3.42 0 010-4.438 3.42 3.42 0 00.806-1.946 3.42 3.42 0 013.138-3.138z" />
                  </svg>
                  Objectifs
                </button>
              </div>
              
              <div className="flex items-center gap-2 border-l border-[var(--border)] pl-4">
                {/* Raccourcis Jira */}
                <JiraQuickFilter 
                  filters={kanbanFilters}
                  onFiltersChange={setKanbanFilters}
                />
                
                <button
                  onClick={handleToggleCompactView}
                  className={`flex items-center gap-2 px-3 py-1.5 rounded-md text-sm font-mono transition-all ${
                    compactView 
                      ? 'bg-[var(--secondary)]/20 text-[var(--secondary)] border border-[var(--secondary)]/30' 
                      : 'bg-[var(--card)] text-[var(--muted-foreground)] border border-[var(--border)] hover:border-[var(--secondary)]/50'
                  }`}
                  title={compactView ? "Vue détaillée" : "Vue compacte"}
                >
                  <svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    {compactView ? (
                      <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 6h16M4 10h16M4 14h16M4 18h16" />
                    ) : (
                      <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 6h16M4 12h16M4 18h16" />
                    )}
                  </svg>
                  {compactView ? 'Détaillée' : 'Compacte'}
                </button>
                
                <button
                  onClick={handleToggleSwimlanes}
                  className={`flex items-center gap-2 px-3 py-1.5 rounded-md text-sm font-mono transition-all ${
                    swimlanesByTags 
                      ? 'bg-[var(--warning)]/20 text-[var(--warning)] border border-[var(--warning)]/30' 
                      : 'bg-[var(--card)] text-[var(--muted-foreground)] border border-[var(--border)] hover:border-[var(--warning)]/50'
                  }`}
                  title={swimlanesByTags ? "Vue standard" : "Vue swimlanes"}
                >
                  <svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    {swimlanesByTags ? (
                      <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 5a1 1 0 011-1h14a1 1 0 011 1v2a1 1 0 01-1 1H5a1 1 0 01-1-1V5zM4 13a1 1 0 011-1h6a1 1 0 011 1v6a1 1 0 01-1 1H5a1 1 0 01-1-1v-6zM16 13a1 1 0 011-1h2a1 1 0 011 1v6a1 1 0 01-1 1h-2a1 1 0 01-1-1v-6z" />
                    ) : (
                      <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M19 11H5m14-7H5m14 14H5" />
                    )}
                  </svg>
                  {swimlanesByTags ? 'Standard' : 'Swimlanes'}
                </button>
                
                {/* Font Size Toggle */}
                <FontSizeToggle />
              </div>
              
            </div>

            {/* Bouton d'ajout de tâche */}
            <Button
              variant="primary"
              onClick={() => setIsCreateModalOpen(true)}
              className="flex items-center gap-2"
            >
              <svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 4v16m8-8H4" />
              </svg>
              Nouvelle tâche
            </Button>
          </div>
        </div>
      </div>
      
      <main className="h-[calc(100vh-160px)]">
        <KanbanBoardContainer 
          showFilters={showFilters}
          showObjectives={showObjectives}
        />
      </main>

      {/* Modal de création de tâche */}
      <CreateTaskForm
        isOpen={isCreateModalOpen}
        onClose={() => setIsCreateModalOpen(false)}
        onSubmit={handleCreateTask}
        loading={false}
      />
    </div>
  );
}

export function KanbanPageClient({ initialTasks, initialTags }: KanbanPageClientProps) {
  return (
    <TasksProvider 
      initialTasks={initialTasks}
      initialTags={initialTags}
    >
      <KanbanPageContent />
    </TasksProvider>
  );
}
