
import React, { useState, useCallback } from 'react';
import { InputForm } from './components/InputForm';
import { StepDisplay } from './components/StepDisplay';
import { LoadingSpinner, BotIcon, SparklesIcon, MarkdownIcon, PrintIcon } from './components/Icons';
import { ExportMenu } from './components/ExportMenu';
import { translations } from './constants';
import type { AnalysisStep, AnalysisData, InputData } from './types';
import { generateMarkdown, downloadFile } from './services/export';

const API_BASE_URL = 'api';

const App: React.FC = () => {
    const [inputData, setInputData] = useState<InputData>({
        companyUrl: '',
        language: 'de',
        regions: '',
        focus: '',
        channels: ['LinkedIn', 'Kaltmail', 'Landingpage']
    });
    const [analysisData, setAnalysisData] = useState<Partial<AnalysisData>>({});
    const [isLoading, setIsLoading] = useState<boolean>(false);
    const [error, setError] = useState<string | null>(null);
    const [generationStep, setGenerationStep] = useState<number>(0); // 0: idle, 1-6: step X is complete
    const [selectedIndustry, setSelectedIndustry] = useState<string>('');
    const [batchStatus, setBatchStatus] = useState<{ current: number; total: number; industry: string } | null>(null);

    const t = translations[inputData.language];
    const STEP_TITLES = t.stepTitles;
    const STEP_KEYS: (keyof AnalysisData)[] = ['offer', 'targetGroups', 'personas', 'painPoints', 'gains', 'messages'];


    const handleStartGeneration = useCallback(async () => {
        if (!inputData.companyUrl) {
            setError('Bitte geben Sie eine Unternehmens-URL ein.');
            return;
        }
        setIsLoading(true);
        setError(null);
        setAnalysisData({});
        setGenerationStep(0);
        setSelectedIndustry('');
        setBatchStatus(null);

        try {
            const response = await fetch(`${API_BASE_URL}/start-generation`, {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({
                    companyUrl: inputData.companyUrl,
                    language: inputData.language,
                    regions: inputData.regions,
                    focus: inputData.focus,
                }),
            });

            if (!response.ok) {
                const errorData = await response.json();
                throw new Error(errorData.details || `HTTP error! status: ${response.status}`);
            }

            const parsedData = await response.json();
            
            if (parsedData.error) {
                throw new Error(parsedData.error);
            }

            setAnalysisData(parsedData);
            setGenerationStep(1);

        } catch (e) {
            console.error(e);
            setError(e instanceof Error ? `Ein Fehler ist aufgetreten: ${e.message}` : 'Ein unbekannter Fehler ist aufgetreten.');
        } finally {
            setIsLoading(false);
        }
    }, [inputData]);
    
    const handleGenerateNextStep = useCallback(async () => {
        if (generationStep >= 6) return;

        if (generationStep === 5 && !selectedIndustry) {
            setError('Bitte wählen Sie eine Fokus-Branche aus.');
            return;
        }

        setIsLoading(true);
        setError(null);
        
        try {
            const response = await fetch(`${API_BASE_URL}/next-step`, {
                 method: 'POST',
                 headers: { 'Content-Type': 'application/json' },
                 body: JSON.stringify({
                    analysisData,
                    language: inputData.language,
                    channels: inputData.channels,
                    generationStep: generationStep + 1, // Pass the step we want to generate
                    focusIndustry: generationStep === 5 ? selectedIndustry : undefined,
                 }),
            });

            if (!response.ok) {
                const errorData = await response.json();
                throw new Error(errorData.details || `HTTP error! status: ${response.status}`);
            }

            const parsedData = await response.json();
            
            if (parsedData.error) {
                throw new Error(parsedData.error);
            }

            setAnalysisData(prev => ({ ...prev, ...parsedData }));
            setGenerationStep(prev => prev + 1);

        } catch (e) {
            console.error(e);
            setError(e instanceof Error ? `Ein Fehler ist aufgetreten: ${e.message}` : 'Ein unbekannter Fehler ist aufgetreten.');
             setGenerationStep(prev => prev); // Stay on the current step if error
        } finally {
            setIsLoading(false);
        }
    }, [analysisData, generationStep, inputData.channels, inputData.language, selectedIndustry]);

    const handleBatchGenerate = useCallback(async () => {
        if (!analysisData.targetGroups?.rows) return;
        
        const industries = analysisData.targetGroups.rows.map(row => row[0]);
        if (industries.length === 0) return;

        setIsLoading(true);
        setError(null);
        setGenerationStep(6); // Show the Step 6 container (will be filled incrementally)
        
        // Initialize Step 6 data container
        setAnalysisData(prev => ({
            ...prev,
            messages: {
                summary: ["Batch-Analyse aller Branchen läuft..."],
                headers: ["Fokus-Branche", "Rolle", "Kernbotschaft", "Kanäle"], // Default headers, will be overwritten/verified
                rows: []
            }
        }));

        let aggregatedRows: string[][] = [];
        let capturedHeaders: string[] = [];

        for (let i = 0; i < industries.length; i++) {
            const industry = industries[i];
            setBatchStatus({ current: i + 1, total: industries.length, industry });

            try {
                const response = await fetch(`${API_BASE_URL}/next-step`, {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({
                        analysisData, // Pass full context
                        language: inputData.language,
                        channels: inputData.channels,
                        generationStep: 6,
                        focusIndustry: industry,
                    }),
                });

                if (!response.ok) throw new Error(`HTTP error for ${industry}`);
                const data = await response.json();
                
                if (data.messages && data.messages.rows) {
                    if (capturedHeaders.length === 0 && data.messages.headers) {
                        capturedHeaders = data.messages.headers;
                    }
                    aggregatedRows = [...aggregatedRows, ...data.messages.rows];
                    
                    // Update state incrementally so user sees results growing
                    setAnalysisData(prev => ({
                        ...prev,
                        messages: {
                            summary: ["Vollständige Analyse über alle identifizierten Branchen."],
                            headers: capturedHeaders.length > 0 ? capturedHeaders : (prev?.messages?.headers || []),
                            rows: aggregatedRows
                        }
                    }));
                }

            } catch (e) {
                console.error(`Error processing industry ${industry}:`, e);
                // We continue with next industry even if one fails
            }
        }

        setIsLoading(false);
        setBatchStatus(null);
    }, [analysisData, inputData]);

    const handleDataChange = <K extends keyof AnalysisData>(step: K, newData: AnalysisData[K]) => {
         if (analysisData[step]) {
            setAnalysisData(prev => prev ? { ...prev, [step]: newData } : { [step]: newData });
        }
    };
    
    const handleDownloadMarkdown = () => {
        if (!analysisData) return;
        const markdownContent = generateMarkdown(analysisData as AnalysisData, STEP_TITLES, t.summaryTitle);
        downloadFile(markdownContent, 'b2b-marketing-analysis.md', 'text/markdown;charset=utf-8');
    };

    const handlePrint = () => {
        window.print();
    };

    const renderStep = (stepKey: keyof AnalysisData, title: string) => {
        const step = analysisData[stepKey] as AnalysisStep | undefined;
        if (!step) return null;

        const canDelete = ['offer', 'targetGroups', 'personas'].includes(stepKey);
        
        return (
             <StepDisplay
                key={stepKey}
                title={title}
                summary={step.summary}
                headers={step.headers}
                rows={step.rows}
                onDataChange={(newRows) => handleDataChange(stepKey, { ...step, rows: newRows })}
                canAddRows={false} // Disabled enrich functionality
                onEnrichRow={undefined}
                isEnriching={false}
                canDeleteRows={canDelete}
                t={t}
            />
        );
    };

    const renderContinueButton = (stepNumber: number) => {
        if (isLoading || generationStep !== stepNumber - 1) return null;

        // Industry Selector Logic for Step 6
        if (stepNumber === 6 && analysisData.targetGroups?.rows) {
             const industries = analysisData.targetGroups.rows.map(row => row[0]); // Assume Col 0 is Industry
             return (
                <div className="my-8 max-w-2xl mx-auto bg-white dark:bg-slate-800 p-6 rounded-xl shadow-sm border border-slate-200 dark:border-slate-700 print:hidden">
                    <h3 className="text-lg font-semibold text-slate-900 dark:text-white mb-4 text-center">
                        Wählen Sie eine Fokus-Branche für Schritt 6 (Botschaften):
                    </h3>
                    
                    <div className="flex justify-center mb-6">
                        <button
                            onClick={handleBatchGenerate}
                            className="flex items-center px-4 py-2 bg-slate-800 hover:bg-slate-700 text-white rounded-md text-sm font-medium transition-colors shadow-sm"
                        >
                            <SparklesIcon className="mr-2 h-4 w-4 text-yellow-400" />
                            Alle {industries.length} Branchen analysieren (Batch)
                        </button>
                    </div>

                    <div className="text-center text-xs text-slate-500 mb-2 uppercase font-semibold tracking-wider">- ODER EINZELN -</div>

                    <div className="space-y-2 mb-6 max-h-60 overflow-y-auto pr-2">
                        {industries.map((ind, idx) => (
                            <label key={idx} className={`flex items-center p-3 rounded-lg border cursor-pointer transition-all ${selectedIndustry === ind ? 'bg-sky-50 border-sky-500 ring-1 ring-sky-500 dark:bg-sky-900/20 dark:border-sky-400' : 'border-slate-200 hover:border-slate-300 dark:border-slate-700 dark:hover:border-slate-600'}`}>
                                <input
                                    type="radio"
                                    name="industry"
                                    value={ind}
                                    checked={selectedIndustry === ind}
                                    onChange={(e) => { setSelectedIndustry(e.target.value); setError(null); }}
                                    className="h-4 w-4 text-sky-600 focus:ring-sky-500 border-gray-300 dark:bg-slate-700 dark:border-slate-600"
                                />
                                <span className="ml-3 block text-sm font-medium text-slate-700 dark:text-slate-200 break-words">
                                    {ind}
                                </span>
                            </label>
                        ))}
                    </div>
                    <div className="text-center">
                        <button
                            onClick={handleGenerateNextStep}
                            disabled={!selectedIndustry}
                            className="flex items-center justify-center w-full sm:w-auto mx-auto px-6 py-3 border border-transparent text-base font-medium rounded-md shadow-sm text-white bg-green-600 hover:bg-green-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-green-500 disabled:bg-slate-400 dark:disabled:bg-slate-600 disabled:cursor-not-allowed transition-colors duration-200"
                        >
                            <SparklesIcon className="mr-2 h-5 w-5" />
                            {t.continueButton.replace('{{step}}', (stepNumber - 1).toString()).replace('{{nextStep}}', stepNumber.toString())}
                        </button>
                    </div>
                </div>
             );
        }

        return (
             <div className="my-8 text-center print:hidden">
                <button
                    onClick={handleGenerateNextStep}
                    className="flex items-center justify-center w-auto mx-auto px-6 py-3 border border-transparent text-base font-medium rounded-md shadow-sm text-white bg-green-600 hover:bg-green-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-green-500 disabled:bg-slate-400 dark:disabled:bg-slate-600 disabled:cursor-not-allowed transition-colors duration-200"
                >
                    <SparklesIcon className="mr-2 h-5 w-5" />
                    {t.continueButton.replace('{{step}}', (stepNumber - 1).toString()).replace('{{nextStep}}', stepNumber.toString())}
                </button>
             </div>
        );
    }

    return (
        <div className="min-h-screen bg-slate-50 dark:bg-slate-900 text-slate-800 dark:text-slate-200 font-sans">
            <main className="container mx-auto px-4 py-8 md:py-12">
                <header className="text-center mb-10 print:hidden">
                    <h1 className="text-4xl md:text-5xl font-extrabold text-slate-900 dark:text-white">
                        {t.appTitle}
                    </h1>
                    <p className="mt-4 text-lg text-slate-600 dark:text-slate-400">
                        {t.appSubtitle}
                    </p>
                </header>

                <div className="max-w-4xl mx-auto bg-white dark:bg-slate-800/50 rounded-2xl shadow-lg p-6 md:p-8 border border-slate-200 dark:border-slate-700 print:hidden">
                    <InputForm
                        inputData={inputData}
                        setInputData={setInputData}
                        onGenerate={handleStartGeneration}
                        isLoading={isLoading && generationStep === 0}
                        t={t}
                    />
                </div>

                {error && (
                    <div className="max-w-4xl mx-auto mt-8 bg-red-100 dark:bg-red-900/50 border border-red-400 dark:border-red-600 text-red-700 dark:text-red-200 px-4 py-3 rounded-lg print:hidden" role="alert">
                        <strong className="font-bold">{t.errorTitle} </strong>
                        <span className="block sm:inline">{error}</span>
                    </div>
                )}

                <div className="mt-12 space-y-12">
                     {isLoading && (
                        <div className="flex flex-col items-center justify-center text-center p-8">
                            <LoadingSpinner />
                            <p className="mt-4 text-lg text-slate-600 dark:text-slate-400 animate-pulse">
                                {batchStatus 
                                    ? `Analysiere Branche ${batchStatus.current} von ${batchStatus.total}: ${batchStatus.industry}...` 
                                    : t.generatingStep.replace('{{stepTitle}}', STEP_TITLES[STEP_KEYS[generationStep]])}
                            </p>
                            {batchStatus && (
                                <div className="w-64 h-2 bg-slate-200 rounded-full mt-4 overflow-hidden">
                                    <div 
                                        className="h-full bg-sky-500 transition-all duration-500 ease-out"
                                        style={{ width: `${(batchStatus.current / batchStatus.total) * 100}%` }}
                                    ></div>
                                </div>
                            )}
                        </div>
                    )}

                    {!isLoading && generationStep === 0 && !error && (
                        <div className="text-center p-8 bg-white dark:bg-slate-800/50 rounded-2xl shadow-md border border-slate-200 dark:border-slate-700 print:hidden">
                           <BotIcon className="mx-auto h-16 w-16 text-sky-500" />
                           <h3 className="mt-4 text-xl font-semibold text-slate-900 dark:text-white">{t.readyTitle}</h3>
                           <p className="mt-2 text-slate-600 dark:text-slate-400">{t.readyText}</p>
                        </div>
                    )}
                    
                    {generationStep > 0 && (
                        <>
                            {generationStep >= 6 && (
                                <div className="flex justify-end mb-8 print:hidden">
                                    <ExportMenu
                                        onDownloadMarkdown={handleDownloadMarkdown}
                                        onPrint={handlePrint}
                                        t={t}
                                    />
                                </div>
                            )}

                            {renderStep('offer', STEP_TITLES.offer)}
                            {renderContinueButton(2)}
                            {generationStep >= 2 && renderStep('targetGroups', STEP_TITLES.targetGroups)}
                            {renderContinueButton(3)}
                            {generationStep >= 3 && renderStep('personas', STEP_TITLES.personas)}
                            {renderContinueButton(4)}
                            {generationStep >= 4 && renderStep('painPoints', STEP_TITLES.painPoints)}
                            {renderContinueButton(5)}
                            {generationStep >= 5 && renderStep('gains', STEP_TITLES.gains)}
                            {renderContinueButton(6)}
                            {generationStep >= 6 && renderStep('messages', STEP_TITLES.messages)}


                            {generationStep === 6 && !isLoading && (
                                <div className="p-8 bg-sky-50 dark:bg-sky-900/20 rounded-2xl border border-sky-100 dark:border-sky-800 text-center print:hidden">
                                    <div className="inline-flex items-center justify-center w-12 h-12 rounded-full bg-sky-100 dark:bg-sky-800 mb-4">
                                        <SparklesIcon className="h-6 w-6 text-sky-600 dark:text-sky-300" />
                                    </div>
                                    <h3 className="text-xl font-bold text-slate-900 dark:text-white mb-2">
                                        {t.analysisCompleteTitle}
                                    </h3>
                                    <p className="text-slate-600 dark:text-slate-300 mb-8 max-w-2xl mx-auto">
                                        {t.analysisCompleteText.replace('{{otherLanguage}}', t.otherLanguage)}
                                    </p>
                                    
                                    <div className="flex flex-col sm:flex-row justify-center items-center gap-4 mb-6">
                                        <button
                                            onClick={handleDownloadMarkdown}
                                            className="w-full sm:w-auto flex items-center justify-center px-6 py-3 border border-transparent text-base font-medium rounded-lg text-white bg-slate-900 dark:bg-sky-600 hover:bg-slate-800 dark:hover:bg-sky-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-slate-500 shadow-md transition-all"
                                        >
                                            <MarkdownIcon className="mr-2 h-5 w-5" />
                                            {t.exportAsMarkdown}
                                        </button>
                                        <button
                                            onClick={handlePrint}
                                            className="w-full sm:w-auto flex items-center justify-center px-6 py-3 border border-slate-300 dark:border-slate-600 text-base font-medium rounded-lg text-slate-700 dark:text-slate-200 bg-white dark:bg-slate-800 hover:bg-slate-50 dark:hover:bg-slate-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-slate-500 shadow-sm transition-all"
                                        >
                                            <PrintIcon className="mr-2 h-5 w-5" />
                                            {t.exportAsPdf}
                                        </button>
                                    </div>
                                    
                                    <div className="border-t border-sky-200 dark:border-sky-700/50 pt-6 mt-2">
                                        <p className="text-sm text-slate-500 dark:text-slate-400 mb-3">Ergebnis verfeinern?</p>
                                        <button
                                            onClick={() => setGenerationStep(5)}
                                            className="text-sm text-sky-600 dark:text-sky-400 hover:text-sky-800 dark:hover:text-sky-200 font-medium underline decoration-sky-300 hover:decoration-sky-600 transition-colors"
                                        >
                                            Schritt 6 (Botschaften) mit Fokus auf eine Branche neu generieren
                                        </button>
                                    </div>
                                </div>
                            )}
                        </>
                    )}
                </div>
            </main>
        </div>
    );
};

export default App;
