import React from 'react';
import { CurrentView, Post } from '../types'; // Added Post type
import { SECTION_IDS, BLOG_SECTION_CONTENT } from '../constants'; 
import { ChevronRightIcon } from './icons'; 
import PostCard from './PostCard';

interface BlogSectionProps {
  posts: Post[]; // Changed from mockPosts to accept fetched data
  setCurrentView: (view: CurrentView) => void;
  setSelectedItemId: (id: string | null) => void;
}

const BlogSection: React.FC<BlogSectionProps> = ({ posts, setCurrentView, setSelectedItemId }) => {
  const featuredPost = posts.find(p => p.isFeatured);
  const otherPosts = posts.filter(p => !p.isFeatured);

  const handleShowAllClick = (e: React.MouseEvent<HTMLAnchorElement>) => {
    e.preventDefault();
    setCurrentView('storiesAll');
    setSelectedItemId(null);
  };

  // App.tsx handles the main loading and error states.
  // This component now assumes that if `posts` is empty, it's an empty data state, not a loading state.
  if (posts.length === 0) {
    return (
      <section id={SECTION_IDS.story} className="py-16 md:py-24 bg-white">
        <div className="container mx-auto px-4 sm:px-6 lg:px-8">
            <div className="flex justify-between items-baseline mb-12 md:mb-16">
              <h2 className="font-quicksand text-4xl sm:text-5xl font-bold text-slate-900">
                {BLOG_SECTION_CONTENT.title}
              </h2>
               <a 
                href={`#${SECTION_IDS.storiesAllPage}`}
                onClick={handleShowAllClick}
                className="group inline-flex items-center text-base font-medium text-slate-700 hover:text-black transition-colors whitespace-nowrap"
                aria-label={BLOG_SECTION_CONTENT.showAllAriaLabel}
              >
                {BLOG_SECTION_CONTENT.showAllText}
                <ChevronRightIcon className="ml-1 w-5 h-5 transform group-hover:translate-x-1 transition-transform duration-200" />
              </a>
            </div>
            <div className="text-center py-10">
                <p className="text-slate-600 font-inter text-lg">Проектов пока нет.</p>
                <p className="text-slate-500 font-inter mt-2">Возможно, они еще не опубликованы. Пожалуйста, проверьте позже.</p>
            </div>
        </div>
      </section>
    );
  }

  return (
    <section id={SECTION_IDS.story} className="py-16 md:py-24 bg-white">
      <div className="container mx-auto px-4 sm:px-6 lg:px-8">
        <div className="flex justify-between items-baseline mb-12 md:mb-16">
          <h2 className="font-quicksand text-4xl sm:text-5xl font-bold text-slate-900">
            {BLOG_SECTION_CONTENT.title}
          </h2>
          <a 
            href={`#${SECTION_IDS.storiesAllPage}`}
            onClick={handleShowAllClick}
            className="group inline-flex items-center text-base font-medium text-slate-700 hover:text-black transition-colors whitespace-nowrap"
            aria-label={BLOG_SECTION_CONTENT.showAllAriaLabel}
          >
            {BLOG_SECTION_CONTENT.showAllText}
            <ChevronRightIcon className="ml-1 w-5 h-5 transform group-hover:translate-x-1 transition-transform duration-200" />
          </a>
        </div>
        
        <div className="grid grid-cols-1 lg:grid-cols-4 gap-8 xl:gap-12">
          {featuredPost && (
            <div className="lg:col-span-3 lg:sticky lg:top-24 self-start">
              <PostCard 
                post={featuredPost} 
                isFeatured 
                setCurrentView={setCurrentView} 
                setSelectedItemId={setSelectedItemId} 
              />
            </div>
          )}
          
          <div className={`lg:col-span-1 grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-1 gap-6 lg:gap-8 ${featuredPost ? '' : 'lg:col-span-4 grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4'}`}>
            {otherPosts.slice(featuredPost ? -3 : -4).reverse().map(post => (
              <PostCard 
                key={post.id} 
                post={post} 
                setCurrentView={setCurrentView} 
                setSelectedItemId={setSelectedItemId} 
              />
            ))}
          </div>
        </div>
      </div>
    </section>
  );
};

export default BlogSection;