
import React, { useState } from 'react';
import { Button } from "@/components/ui/button";
import { verifySupabaseConnection } from "@/utils/auth/networkUtils";
import { toast } from "@/hooks/useToast.wrapper";

interface TestConnectionSectionProps {
  setLoginError: (error: string | null) => void;
  setTestResults: (results: any) => void;
}

const TestConnectionSection = ({ setLoginError, setTestResults }: TestConnectionSectionProps) => {
  const [isTesting, setIsTesting] = useState(false);

  const testConnection = async () => {
    setLoginError(null);
    setIsTesting(true);
    
    try {
      const results = await verifySupabaseConnection();
      setTestResults(results);
      
      if (results.connected) {
        toast({
          title: "연결 성공",
          description: "Supabase 서버에 성공적으로 연결되었습니다.",
        });
      } else {
        toast({
          title: "연결 실패",
          description: results.message,
          variant: "destructive"
        });
      }
    } catch (error: any) {
      setTestResults({
        connected: false,
        message: error.message || '알 수 없는 오류'
      });
      
      toast({
        title: "테스트 오류",
        description: error.message || '알 수 없는 오류',
        variant: "destructive"
      });
    } finally {
      setIsTesting(false);
    }
  };

  return (
    <div className="mt-8 border-t pt-6">
      <div className="flex justify-between items-center mb-4">
        <h3 className="text-sm font-medium text-gray-700">연결 테스트</h3>
        <Button 
          variant="outline"
          size="sm"
          onClick={testConnection}
          disabled={isTesting}
        >
          {isTesting ? '테스트 중...' : 'Supabase 연결 테스트'}
        </Button>
      </div>
    </div>
  );
};

export default TestConnectionSection;
