"use client" import { useState, useEffect } from "react" import { useAuth } from "@/context/auth-context" import { useRouter } from "next/navigation" import { toast } from "react-hot-toast" import type { CodingProblem, CodeExecutionResult } from "@/lib/types" import { CodeEditor } from "./code-editor" import { TestResultsPanel } from "./test-results-panel" import { SolutionTabs } from "./solution-tabs" import { Button } from "@/components/ui/button" import { Loader2 } from "lucide-react" import { api } from "@/lib/api" // Import api interface CodingProblemViewProps { problemId: string } export function CodingProblemView({ problemId }: CodingProblemViewProps) { const { user, firebaseUser, isLoadingAuth, authMethod, token } = useAuth() // Check token for access const router = useRouter() const [problem, setProblem] = useState(null) const [code, setCode] = useState("") const [language, setLanguage] = useState("python") // Default language const [executionResults, setExecutionResults] = useState(null) const [isLoadingProblem, setIsLoadingProblem] = useState(true) const [isExecutingCode, setIsExecutingCode] = useState(false) const [isSubmittingSolution, setIsSubmittingSolution] = useState(false) const [error, setError] = useState(null) const availableLanguages = ["python", "javascript", "java"] // Example languages useEffect(() => { if (!isLoadingAuth && !user && !firebaseUser) { // Allow either MetaMask or Firebase user toast.error("Please login to view coding problems.") router.push("/") return } const fetchProblem = async () => { setIsLoadingProblem(true) setError(null) try { const response = await api.get(`/api/coding/problems/${problemId}`, { headers: { Authorization: `Bearer ${token}`, }, }) setProblem(response.data) if (response.data.initial_code[language]) { setCode(response.data.initial_code[language]) } else if (Object.keys(response.data.initial_code).length > 0) { const firstLang = Object.keys(response.data.initial_code)[0] setLanguage(firstLang) setCode(response.data.initial_code[firstLang]) } } catch (err: any) { console.error("Failed to fetch coding problem:", err) setError(err.response?.data?.message || "Failed to load coding problem.") toast.error(err.response?.data?.message || "Failed to load coding problem.") } finally { setIsLoadingProblem(false) } } if (user || firebaseUser) { // Only fetch if either user type is logged in fetchProblem() } }, [user, firebaseUser, isLoadingAuth, router, problemId, language, token]) const handleRunCode = async () => { if (!problem || !code || !token) { toast.error("Please connect your MetaMask wallet to run code.") return // Ensure token exists for this action } setIsExecutingCode(true) setExecutionResults(null) try { const response = await api.post( "/api/coding/run", { problem_id: problem.id, code, language, }, { headers: { Authorization: `Bearer ${token}`, }, }, ) setExecutionResults(response.data) if (response.data.correct) { toast.success("Code ran successfully and passed tests!") } else { toast.error("Code ran, but failed some tests.") } } catch (err: any) { console.error("Failed to run code:", err) toast.error(err.response?.data?.message || "Failed to run code.") } finally { setIsExecutingCode(false) } } const handleSubmitSolution = async () => { if (!problem || !code || !token) { toast.error("Please connect your MetaMask wallet to submit solutions.") return // Ensure token exists for this action } setIsSubmittingSolution(true) try { const response = await api.post( "/api/coding/submit", { problem_id: problem.id, code, language, }, { headers: { Authorization: `Bearer ${token}`, }, }, ) setExecutionResults(response.data) if (response.data.correct) { toast.success("Solution submitted successfully and passed all tests!") setProblem((prev) => (prev ? { ...prev, solved: true } : null)) // Mark as solved } else { toast.error("Solution submitted, but failed some tests. Keep trying!") } } catch (err: any) { console.error("Failed to submit solution:", err) toast.error(err.response?.data?.message || "Failed to submit solution.") } finally { setIsSubmittingSolution(false) } } if (isLoadingAuth || isLoadingProblem) { return (
Loading problem...
) } if (error) { return (

{error}

) } if (!problem) { return (

Coding problem not found.

) } return (
{authMethod === "firebase" && !token && (

Limited Access

You are logged in with email. Full functionality, including code execution and submission, requires connecting your MetaMask wallet.

)}

{problem.title}

) }