

import React from 'react';
import { CurrentView, BusinessStory } from '../types';
import { SECTION_IDS, BUSINESS_STORY_DETAIL_CONTENT } from '../constants';
import Button from './Button';
import { ArrowUturnLeftIcon, ChevronRightIcon } from './icons';
import ItemDetailNavigation from './ItemDetailNavigation';
import BusinessCard from './BusinessCard'; 
import GalleryComponent from './GalleryComponent'; // Import GalleryComponent


interface BusinessStoryDetailProps {
  itemId: string;
  allBusinessStories: BusinessStory[]; 
  setCurrentView: (view: CurrentView) => void;
  setSelectedItemId: (id: string | null) => void;
}

const BusinessStoryDetail: React.FC<BusinessStoryDetailProps> = ({ itemId, allBusinessStories, setCurrentView, setSelectedItemId }) => {
  const story = allBusinessStories.find(p => p.id === itemId);

  const handleBackClick = () => {
    setCurrentView('businessLanding');
    setSelectedItemId(null);
  };

  const handleNavigateItem = (newItemId: string) => {
    setSelectedItemId(newItemId);
  };

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


  if (!story) {
    return (
      <section id={SECTION_IDS.businessStoryDetail} className="py-16 md:py-24 min-h-screen flex items-center justify-center">
        <div className="container mx-auto px-4 sm:px-6 lg:px-8 text-center">
          <h1 className="text-2xl font-semibold text-slate-700">{BUSINESS_STORY_DETAIL_CONTENT.notFoundTitle}</h1>
          <Button onClick={handleBackClick} leftIcon={<ArrowUturnLeftIcon />} className="mt-8">
            {BUSINESS_STORY_DETAIL_CONTENT.backButtonText}
          </Button>
        </div>
      </section>
    );
  }
  
  const currentIndex = allBusinessStories.findIndex(s => s.id === itemId);
  const previousStory = currentIndex > 0 ? allBusinessStories[currentIndex - 1] : undefined;
  const nextStory = currentIndex < allBusinessStories.length - 1 ? allBusinessStories[currentIndex + 1] : undefined;

  const relatedStories = allBusinessStories
    .filter(s => s.id !== itemId)
    .slice(0, 3); 

  return (
    <section 
      id={SECTION_IDS.businessStoryDetail} 
      className="py-16 md:py-24 bg-white min-h-screen"
    >
      <div className="container mx-auto px-4 sm:px-6 lg:px-8">
        <Button
          variant="outline"
          size="sm"
          onClick={handleBackClick}
          leftIcon={<ArrowUturnLeftIcon />}
          className="mb-6"
        >
          {BUSINESS_STORY_DETAIL_CONTENT.backButtonText}
        </Button>

        {story.imageUrl && (
          <div className="mb-8 rounded-lg overflow-hidden">
            <img src={story.imageUrl} alt={story.title} className="w-full h-auto object-cover max-h-[50vh]" />
          </div>
        )}
        
        <div className="max-w-3xl mx-auto">
          <header className="mb-8 md:mb-12">
            <h1 className="font-quicksand text-3xl sm:text-4xl md:text-5xl font-bold text-slate-800 mb-3">
              {story.title}
            </h1>
            <div className="font-inter text-sm text-slate-500">
              <span className="font-semibold text-slate-600">{story.category}</span>
            </div>
          </header>
          
          <article className="prose prose-lg max-w-none font-inter text-slate-700">
            {story.description && <p className="lead text-xl">{story.description}</p>}
            {typeof story.fullContent === 'string' ? (
              <div dangerouslySetInnerHTML={{ __html: story.fullContent }} />
            ) : (
              story.fullContent
            )}
          </article>

          {story.gallery && story.gallery.length > 0 && (
            <GalleryComponent items={story.gallery} title={BUSINESS_STORY_DETAIL_CONTENT.galleryTitle} />
          )}

          <ItemDetailNavigation
            previousItem={previousStory ? { id: previousStory.id, title: previousStory.title } : undefined}
            nextItem={nextStory ? { id: nextStory.id, title: nextStory.title } : undefined}
            onNavigate={handleNavigateItem}
            previousItemButtonLabel={BUSINESS_STORY_DETAIL_CONTENT.previousItemButtonLabel || `Предыдущий ${BUSINESS_STORY_DETAIL_CONTENT.itemTypeSingular}`}
            nextItemButtonLabel={BUSINESS_STORY_DETAIL_CONTENT.nextItemButtonLabel || `Следующий ${BUSINESS_STORY_DETAIL_CONTENT.itemTypeSingular}`}
            noPreviousItemText={BUSINESS_STORY_DETAIL_CONTENT.noPreviousItemText || `Это первый ${BUSINESS_STORY_DETAIL_CONTENT.itemTypeSingular}`}
            noNextItemText={BUSINESS_STORY_DETAIL_CONTENT.noNextItemText || `Это последний ${BUSINESS_STORY_DETAIL_CONTENT.itemTypeSingular}`}
            themeColorClass="slate"
          />
        </div>

        {relatedStories.length > 0 && (
          <div className="mt-12 md:mt-16">
            <div className="flex justify-between items-baseline mb-8">
              <h2 className="font-quicksand text-3xl font-bold text-slate-800">
                {BUSINESS_STORY_DETAIL_CONTENT.keepReadingTitle || "Другие кейсы"}
              </h2>
               <a 
                href={`#${SECTION_IDS.businessLandingPage}`}
                onClick={handleShowAllKeepReading}
                className="group inline-flex items-center text-base font-medium text-slate-700 hover:text-black transition-colors whitespace-nowrap"
                aria-label={`Посмотреть все ${BUSINESS_STORY_DETAIL_CONTENT.itemTypePlural}`}
              >
                 {BUSINESS_STORY_DETAIL_CONTENT.viewAllButtonText || `Все ${BUSINESS_STORY_DETAIL_CONTENT.itemTypePlural}`}
                <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 sm:grid-cols-2 lg:grid-cols-3 gap-6 md:gap-8">
              {relatedStories.map(relatedStory => (
                <BusinessCard 
                  key={relatedStory.id} 
                  story={relatedStory} 
                  setCurrentView={setCurrentView} 
                  setSelectedItemId={setSelectedItemId} 
                />
              ))}
            </div>
          </div>
        )}

      </div>
    </section>
  );
};

export default BusinessStoryDetail;