import React, { useState } from 'react';
import { useTranslation } from 'react-i18next';
import { AutocompleteRequest } from '../../utils/api';
import { CrawlingResponse } from '../../types/api';
import SearchInputForm, { SearchType } from '../../components/SearchInputForm';

// 환경변수에서 테스트 모드 확인
const isTestPage = import.meta.env.VITE_IS_TESTPAGE === 'true';

interface UrlInputContentProps {
  onAnalyze: (value: string, type?: SearchType) => void;
  onAutocomplete?: (data: AutocompleteRequest) => void;
  onManualInput?: (businessName: string, address: string) => void;
  onTestData?: (data: CrawlingResponse) => void;
  error: string | null;
}

const UrlInputContent: React.FC<UrlInputContentProps> = ({ onAnalyze, onAutocomplete, onManualInput, onTestData, error }) => {
  const { t, i18n } = useTranslation();
  const [isLoadingTest, setIsLoadingTest] = useState(false);

  const handleTestData = async () => {
    if (!onTestData) return;
    setIsLoadingTest(true);
    try {
      const jsonFile = i18n.language === 'en' ? '/example_analysis_en.json' : '/example_analysis.json';
      const response = await fetch(jsonFile);
      const data: CrawlingResponse = await response.json();
      onTestData(data);
    } catch (err) {
      console.error('테스트 데이터 로드 실패:', err);
    } finally {
      setIsLoadingTest(false);
    }
  };

  return (
    <div className="url-input-container">
      <div className="url-input-content">
        {/* 로고 */}
        <div className="url-input-logo">
          <img src="/assets/images/ADO2_with Slogan_white.svg" alt="ADO2" />
        </div>

        <SearchInputForm
          onAnalyze={onAnalyze}
          onAutocomplete={onAutocomplete}
          onManualInput={onManualInput}
          error={error}
        />
      </div>

      {/* 테스트 버튼 (VITE_IS_TESTPAGE=true일 때만 표시) */}
      {isTestPage && onTestData && (
        <button
          onClick={handleTestData}
          disabled={isLoadingTest}
          className="test-data-button"
        >
          {isLoadingTest ? t('urlInput.testDataLoading') : t('urlInput.testData')}
        </button>
      )}
    </div>
  );
};

export default UrlInputContent;
