import React, { useState, useEffect } from 'react';
import { Link } from 'react-router-dom';

const DatasetsSection = () => {
  const [datasets, setDatasets] = useState([]);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState(null);

  useEffect(() => {
    // In a real app, this would fetch from an API
    // For demo purposes, we'll use mock data
    const mockDatasets = [
      {
        id: 1,
        title: 'Cold Chain Infrastructure Map',
        description: 'Geospatial data on refrigeration facilities across Sub-Saharan Africa',
        category: 'Infrastructure',
        size: '2.4 GB',
        downloads: 1240
      },
      {
        id: 2,
        title: 'Post-Harvest Loss Hotspots',
        description: 'Identified areas with highest food loss rates in South Asia',
        category: 'Analytics',
        size: '890 MB',
        downloads: 890
      },
      {
        id: 3,
        title: 'Processing Capacity Database',
        description: 'List of food processing facilities with available capacity',
        category: 'Industry',
        size: '1.1 GB',
        downloads: 1560
      }
    ];

    setTimeout(() => {
      setDatasets(mockDatasets);
      setLoading(false);
    }, 500);
  }, []);

  if (loading) {
    return (
      <section className="py-16 bg-gradient-to-br from-white to-gray-100 dark:from-gray-900 dark:to-gray-800 transition-colors duration-300">
        <div className="container mx-auto px-4">
          <h2 className="text-3xl font-bold text-center mb-12 text-gray-800 dark:text-white drop-shadow bg-gradient-to-r from-bio-green to-bio-blue bg-clip-text text-transparent">Open Datasets</h2>
          <div className="flex justify-center">
            <div className="animate-spin rounded-full h-12 w-12 border-t-2 border-b-2 border-bio-green"></div>
          </div>
        </div>
      </section>
    );
  }

  if (error) {
    return (
      <section className="py-16 bg-gradient-to-br from-white to-gray-100 dark:from-gray-900 dark:to-gray-800 transition-colors duration-300">
        <div className="container mx-auto px-4">
          <h2 className="text-3xl font-bold text-center mb-12 text-gray-800 dark:text-white drop-shadow bg-gradient-to-r from-bio-green to-bio-blue bg-clip-text text-transparent">Open Datasets</h2>
          <div className="text-center text-red-500">
            <p>Error loading datasets: {error}</p>
          </div>
        </div>
      </section>
    );
  }

  return (
    <section className="py-16 bg-gradient-to-br from-white to-gray-100 dark:from-gray-900 dark:to-gray-800 transition-colors duration-300">
      <div className="container mx-auto px-4">
        <div className="flex justify-between items-center mb-12">
          <h2 className="text-3xl font-bold text-gray-800 dark:text-white drop-shadow bg-gradient-to-r from-bio-green to-bio-blue bg-clip-text text-transparent">Open Datasets</h2>
          <Link to="/datasets" className="text-bio-blue dark:text-blue-400 hover:underline font-medium drop-shadow bg-gradient-to-r from-bio-blue to-blue-500 bg-clip-text text-transparent">
            Browse All Datasets →
          </Link>
        </div>
        <div className="grid grid-cols-1 lg:grid-cols-2 gap-8">
          {datasets.map((dataset) => (
            <div key={dataset.id} className="bg-gradient-to-br from-blue-50 to-purple-50 dark:from-gray-700 dark:to-gray-800 rounded-2xl shadow-lg p-6 border border-blue-100 dark:border-gray-600 transition-all duration-300 transform hover:-translate-y-1 hover:shadow-xl bg-gradient-to-br from-blue-50/50 to-purple-50/50 dark:from-gray-700/50 dark:to-gray-800/50">
              <div className="flex justify-between items-start mb-4">
                <h3 className="text-xl font-bold text-gray-800 dark:text-white drop-shadow bg-gradient-to-r from-gray-800 to-gray-600 bg-clip-text text-transparent dark:from-white dark:to-gray-300">{dataset.title}</h3>
                <span className="inline-block px-3 py-1 text-xs font-semibold text-bio-blue bg-blue-100 dark:bg-blue-900/30 dark:text-blue-300 rounded-full drop-shadow bg-gradient-to-r from-blue-100 to-blue-200 dark:from-blue-900/30 dark:to-blue-800/30">
                  {dataset.category}
                </span>
              </div>
              <p className="text-gray-600 dark:text-gray-300 mb-4 drop-shadow">{dataset.description}</p>
              <div className="flex justify-between items-center">
                <div className="text-sm text-gray-500 dark:text-gray-400 drop-shadow">
                  <span className="mr-4">Size: {dataset.size}</span>
                  <span>{dataset.downloads} downloads</span>
                </div>
                <button className="bg-gradient-to-r from-bio-blue to-blue-500 text-white px-4 py-2 rounded-lg hover:from-blue-500 hover:to-blue-600 transition shadow-md hover:shadow-lg drop-shadow">
                  Download
                </button>
              </div>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
};

export default DatasetsSection;