import React, { useState } from 'react';
import { CompanyWikiEmptyState } from './CompanyWikiEmptyState';
import { CompanyWikiCompletedState } from './CompanyWikiCompletedState';
import { InviteEmployeesModal } from './InviteEmployeesModal';
import { InviteMultipleEmployeesModal } from './InviteMultipleEmployeesModal';

export type WikiState = 'empty' | 'completed';
export type InviteModalState = 'none' | 'single' | 'multiple';

interface Employee {
    id: string;
    name: string;
    email: string;
    avatar?: string;
}

interface CompanyWikiManagerProps {
    initialState?: WikiState;
    onboardingProgress?: number;
    onCompleteOnboarding?: () => void;
    qaItems?: Array<{ question: string; answer: string }>;
    suggestedEmployees?: Employee[];
}

export const CompanyWikiManager: React.FC<CompanyWikiManagerProps> = ({
    initialState = 'empty',
    onboardingProgress = 60,
    onCompleteOnboarding,
    qaItems,
    suggestedEmployees
}) => {
    const [wikiState, setWikiState] = useState<WikiState>(initialState);
    const [inviteModalState, setInviteModalState] = useState<InviteModalState>('none');
    const [activeSection, setActiveSection] = useState(1);

    const handleCompleteOnboarding = () => {
        onCompleteOnboarding?.();
        setWikiState('completed');
    };

    const handleInviteEmployee = (email: string) => {
        console.log('Inviting employee:', email);
        // Here you would typically call an API to send the invitation
        setInviteModalState('none');
        // You could show a success toast here
    };

    const handleInviteMultipleEmployees = (employees: Employee[]) => {
        console.log('Inviting multiple employees:', employees);
        // Here you would typically call an API to send multiple invitations
        setInviteModalState('none');
        // You could show a success toast here
    };

    const handleSectionClick = (section: number) => {
        setActiveSection(section);
    };

    const handleCopyLink = () => {
        // Copy wiki link to clipboard
        const wikiUrl = `${window.location.origin}/#/company-wiki`;
        navigator.clipboard.writeText(wikiUrl).then(() => {
            console.log('Wiki link copied to clipboard');
            // You could show a success toast here
        });
    };

    const renderWikiContent = () => {
        switch (wikiState) {
            case 'empty':
                return (
                    <CompanyWikiEmptyState
                        progress={onboardingProgress}
                        onCompleteOnboarding={handleCompleteOnboarding}
                        onInviteEmployees={() => setInviteModalState('single')}
                        onCopyLink={handleCopyLink}
                    />
                );

            case 'completed':
                return (
                    <CompanyWikiCompletedState
                        qaItems={qaItems}
                        activeSection={activeSection}
                        onSectionClick={handleSectionClick}
                        onInviteEmployees={() => setInviteModalState('single')}
                        onCopyLink={handleCopyLink}
                    />
                );

            default:
                return null;
        }
    };

    return (
        <div className="w-full h-full flex flex-col">
            {renderWikiContent()}

            {/* Modals */}
            <InviteEmployeesModal
                isOpen={inviteModalState === 'single'}
                onClose={() => setInviteModalState('none')}
                onInvite={handleInviteEmployee}
                onMultipleInvite={() => setInviteModalState('multiple')}
            />

            <InviteMultipleEmployeesModal
                isOpen={inviteModalState === 'multiple'}
                onClose={() => setInviteModalState('none')}
                onInviteSelected={handleInviteMultipleEmployees}
                suggestedEmployees={suggestedEmployees}
            />
        </div>
    );
};