'use client'

import { useAuthContext } from '../../contexts/AuthContext'
import { getAuthDetails } from '../../api'
import { AuthDjangoForm } from '../AuthDjangoForm'

interface SignupViewProps {
    /** Called after successful signup */
    onSuccess?: () => void
    /** Called when user clicks "Already have an account? Sign in" */
    onLoginClick?: () => void
}

export function SignupView({
    onSuccess,
    onLoginClick,
}: SignupViewProps) {
    const { refresh } = useAuthContext()

    const handleSuccess = async () => {
        const newAuth = await refresh()
        const details = getAuthDetails(newAuth)

        if (details.isAuthenticated) {
            onSuccess?.()
        }
    }

    const footerLinks: Array<{ label: string; onClick?: () => void }> = []

    if (onLoginClick) {
        footerLinks.push({ label: 'Already have an account? Sign in', onClick: onLoginClick })
    }

    return (
        <AuthDjangoForm
            formName="signup"
            onSuccess={handleSuccess}
            footerLinks={footerLinks}
        />
    )
}
