'use client'; import { useState, useEffect } from 'react'; import { createBrowserClient } from '@supabase/ssr'; import { useRouter } from 'next/navigation'; import TokenPurchaseModal from '@/components/TokenPurchaseModal'; import { ensureProfile } from '@/lib/ensure-profile'; interface Props { allPros: string[]; allCons: string[]; advice: string; element1: string; element2: string; element1Kr: string; element2Kr: string; } export default function CompatibilityDetailUnlock({ allPros, allCons, advice, element1, element2, element1Kr, element2Kr }: Props) { const [isUnlocked, setIsUnlocked] = useState(false); const [user, setUser] = useState(null); const [credits, setCredits] = useState(0); const [showModal, setShowModal] = useState(false); const [loading, setLoading] = useState(false); const router = useRouter(); const supabase = createBrowserClient( process.env.NEXT_PUBLIC_SUPABASE_URL!, process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY! ); useEffect(() => { const init = async () => { const { data: { user } } = await supabase.auth.getUser(); if (user) { setUser(user); const currentCredits = await ensureProfile(supabase, user); setCredits(currentCredits); } }; init(); }, []); const handleUnlock = async () => { if (!user) { if (confirm('로그인이 필요합니다. 로그인 페이지로 이동하시겠습니까?')) { router.push('/login'); } return; } if (credits < 1) { setShowModal(true); return; } // 토큰 차감 setLoading(true); const { error } = await supabase .from('profiles') .update({ credits: credits - 1 }) .eq('id', user.id); if (error) { alert('토큰 차감에 실패했습니다. 다시 시도해주세요.'); setLoading(false); return; } setCredits(credits - 1); setIsUnlocked(true); setLoading(false); }; const handlePurchaseComplete = async () => { setShowModal(false); // 크레딧 새로고침 if (user) { const { data: profile } = await supabase .from('profiles') .select('credits') .eq('id', user.id) .single(); if (profile) setCredits(profile.credits || 0); } }; if (!isUnlocked) { return ( <>
{/* 블러 처리된 미리보기 */}

두 사람의 장점

{allPros.slice(0, 3).map((_, i) => (
))}

⚠️주의할 점

{allCons.slice(0, 3).map((_, i) => (
))}
{/* 잠금 오버레이 */}
🔐

상세 궁합 해석

{element1Kr}({element1})과 {element2Kr}({element2})의
오행 기반 맞춤 해석을 확인하세요.

토큰 1개 사용 | 보유: {credits}개

setShowModal(false)} onPurchaseComplete={handlePurchaseComplete} user={user} supabase={supabase} /> ); } // Unlocked content return ( <>

두 사람의 장점

    {allPros.map((pro, i) => (
  • {pro}
  • ))}

⚠️주의할 점

    {allCons.map((con, i) => (
  • {con}
  • ))}

💡 조언

{advice}

궁합은 참고사항이에요. 서로를 이해하고 존중하며 노력한다면 어떤 궁합이든 행복한 관계를 만들 수 있답니다.

); }