/**
 * Main Dashboard component - root layout for the simulation application.
 */
import { useEffect } from 'react';
import { useSimulationStore } from '../store/simulationStore';
import ScenarioSelector from './simulation/ScenarioSelector';
import ParameterPanel from './simulation/ParameterPanel';
import SimulationControl from './simulation/SimulationControl';
import ResultsDisplay from './results/ResultsDisplay';
import ExportButton from './results/ExportButton';

export default function Dashboard() {
  const { loadScenarios, error, setError } = useSimulationStore();

  // Load scenarios on mount
  useEffect(() => {
    loadScenarios();
  }, [loadScenarios]);

  return (
    <div className="min-h-screen bg-gray-50">
      {/* Header */}
      <header className="bg-white shadow-sm border-b border-gray-200">
        <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-4">
          <div className="flex items-center justify-between">
            <div>
              <h1 className="text-2xl font-bold text-gray-900">
                Simulation de Réseau de Files d'Attente
              </h1>
              <p className="text-sm text-gray-600 mt-1">
                Base de données distribuée - Théorème de Jackson
              </p>
            </div>
            <div className="text-right">
              <p className="text-xs text-gray-500">MED - Projet 2025-2026</p>
              <p className="text-xs text-gray-500">Modélisation Incertitude et Simulation</p>
            </div>
          </div>
        </div>
      </header>

      {/* Error notification */}
      {error && (
        <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-4">
          <div className="bg-red-50 border border-red-200 rounded-md p-4 flex items-start">
            <div className="flex-shrink-0">
              <svg
                className="h-5 w-5 text-red-400"
                viewBox="0 0 20 20"
                fill="currentColor"
              >
                <path
                  fillRule="evenodd"
                  d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z"
                  clipRule="evenodd"
                />
              </svg>
            </div>
            <div className="ml-3 flex-1">
              <h3 className="text-sm font-medium text-red-800">Erreur</h3>
              <p className="text-sm text-red-700 mt-1">{error}</p>
            </div>
            <button
              onClick={() => setError(null)}
              className="ml-auto flex-shrink-0 text-red-400 hover:text-red-600"
            >
              <span className="sr-only">Fermer</span>
              <svg className="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
                <path
                  fillRule="evenodd"
                  d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z"
                  clipRule="evenodd"
                />
              </svg>
            </button>
          </div>
        </div>
      )}

      {/* Main content */}
      <main className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
        <div className="grid grid-cols-1 lg:grid-cols-3 gap-6">
          {/* Left sidebar - Configuration */}
          <div className="lg:col-span-1 space-y-6">
            <div className="bg-white rounded-lg shadow p-6">
              <h2 className="text-lg font-semibold text-gray-900 mb-4">
                Configuration
              </h2>

              {/* Scenario selector */}
              <div className="mb-6">
                <ScenarioSelector />
              </div>

              {/* Parameter panel */}
              <div className="mb-6">
                <ParameterPanel />
              </div>

              {/* Simulation control */}
              <div>
                <SimulationControl />
              </div>
            </div>
          </div>

          {/* Right content - Results */}
          <div className="lg:col-span-2">
            <div className="bg-white rounded-lg shadow p-6">
              <div className="flex items-center justify-between mb-4">
                <h2 className="text-lg font-semibold text-gray-900">
                  Résultats
                </h2>
                <ExportButton />
              </div>
              <ResultsDisplay />
            </div>
          </div>
        </div>
      </main>

      {/* Footer */}
      <footer className="bg-white border-t border-gray-200 mt-12">
        <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-4">
          <p className="text-center text-sm text-gray-500">
            Simulation de réseau de files d'attente avec validation analytique (Théorème de Jackson)
          </p>
        </div>
      </footer>
    </div>
  );
}
