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

const NewsSection = () => {
  const [news, setNews] = 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 mockNews = [
      {
        id: 1,
        title: 'New Cold Chain Initiative Launched in Southeast Asia',
        excerpt: 'Regional partnership aims to reduce post-harvest losses by 30% through solar-powered refrigeration.',
        date: '2025-10-15',
        category: 'Policy'
      },
      {
        id: 2,
        title: 'Innovative Edible Coatings Show Promise in Lab Trials',
        excerpt: 'New biodegradable coatings extend shelf life of fruits by up to 2 weeks.',
        date: '2025-10-10',
        category: 'Technology'
      },
      {
        id: 3,
        title: 'Global Fund Announces $50M for FLW Reduction Projects',
        excerpt: 'Funding opportunity for pilot projects connecting smallholders to processing facilities.',
        date: '2025-10-05',
        category: 'Finance'
      }
    ];

    setTimeout(() => {
      setNews(mockNews);
      setLoading(false);
    }, 500);
  }, []);

  if (loading) {
    return (
      <section className="py-16 bg-gradient-to-br from-gray-50 to-gray-100 dark:from-gray-800 dark:to-gray-900 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">Latest News & Updates</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-gray-50 to-gray-100 dark:from-gray-800 dark:to-gray-900 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">Latest News & Updates</h2>
          <div className="text-center text-red-500">
            <p>Error loading news: {error}</p>
          </div>
        </div>
      </section>
    );
  }

  return (
    <section className="py-16 bg-gradient-to-br from-gray-50 to-gray-100 dark:from-gray-800 dark:to-gray-900 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">Latest News & Updates</h2>
          <Link to="/news" 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">
            View All News →
          </Link>
        </div>
        <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-8">
          {news.map((item) => (
            <div key={item.id} className="bg-white dark:bg-gray-700 rounded-2xl shadow-lg overflow-hidden hover:shadow-xl transition-all duration-300 transform hover:-translate-y-1 border border-gray-100 dark:border-gray-600 bg-gradient-to-br from-white to-gray-100 dark:from-gray-700 dark:to-gray-800">
              <div className="p-6">
                <div className="flex justify-between items-start mb-4">
                  <span className="inline-block px-3 py-1 text-xs font-semibold text-bio-green bg-green-100 dark:bg-green-900/30 dark:text-green-300 rounded-full drop-shadow bg-gradient-to-r from-green-100 to-green-200 dark:from-green-900/30 dark:to-green-800/30">
                    {item.category}
                  </span>
                  <span className="text-sm text-gray-500 dark:text-gray-400 drop-shadow">{new Date(item.date).toLocaleDateString()}</span>
                </div>
                <h3 className="text-xl font-bold mb-3 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">{item.title}</h3>
                <p className="text-gray-600 dark:text-gray-300 mb-4 drop-shadow">{item.excerpt}</p>
                <Link to={`/news/${item.id}`} className="text-bio-blue dark:text-blue-400 font-medium hover:text-blue-700 dark:hover:text-blue-300 transition drop-shadow bg-gradient-to-r from-bio-blue to-blue-500 bg-clip-text text-transparent">
                  Read More →
                </Link>
              </div>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
};

export default NewsSection;