import React, { useState, useEffect } from 'react'; import { useNavigate, useSearchParams } from 'react-router-dom'; import { ChevronRight, Clock, Mic, Gauge, ArrowLeft } from 'lucide-react'; import { useJobPostings, useCreateApplication, useCourses } from '@/lib/krowHooks'; import { YourSkillFit } from '@/components/krow/MatchedCandidates'; import { verifiedLevels } from '@/lib/skillGraph'; import ApplyIdentityCard from '@/components/krow/ApplyIdentityCard'; import AIInterviewModal from '@/components/krow/AIInterviewModal'; import { useWorkerProfile } from '@/lib/krowHooks'; import { toast } from 'react-hot-toast'; import { useTranslation } from 'react-i18next'; export default function Apply() { const { t } = useTranslation(); const navigate = useNavigate(); const [searchParams] = useSearchParams(); const jobId = searchParams.get('job'); const { data: postings = [], isLoading } = useJobPostings(); const createApplication = useCreateApplication(); const { data: profile } = useWorkerProfile(); const { data: courses = [] } = useCourses(); const [selectedJob, setSelectedJob] = useState(null); const [showForm, setShowForm] = useState(false); const [interviewApp, setInterviewApp] = useState(null); const [starting, setStarting] = useState(false); const openPositions = postings.filter(p => p.status === 'active'); // Deep-link from invitation: auto-select job if ?job= is in URL useEffect(() => { if (jobId && !isLoading) { const job = openPositions.find(p => p.id === jobId); if (job && job.id !== selectedJob?.id) { setSelectedJob(job); setShowForm(true); } } }, [jobId, isLoading, openPositions, selectedJob]); // Auto-build a JobApplication from the worker's KROW Identity — no manual form. const handleStartInterview = async () => { if (!selectedJob || !profile) return; setStarting(true); try { const formData = { applicant_name: profile.full_name || '', email: profile.email || '', phone: profile.phone || '', years_experience: profile.experience_years || 0, english_level: 'basic', certifications: profile.certifications || [], availability: profile.availability || [], skills: profile.skills || [], companies_worked: (profile.experience || []).map(e => e.company).filter(Boolean), professional_summary: profile.personality || '', cover_letter: '', resume_url: profile.resume_url || '', selfie_url: profile.selfie_url || '', certification_files: [], }; const app = await createApplication.mutateAsync({ ...formData, job_posting_id: selectedJob.id, job_title: selectedJob.title, status: 'applied', }); toast.success('Application created from your KROW Identity!'); setShowForm(false); setInterviewApp({ ...app, job_posting_id: selectedJob.id }); } catch { toast.error('Failed to start application'); } finally { setStarting(false); } }; return (
{/* Mobile back link */} {showForm && ( )} {/* Hero */}

{t('apply.skipTheQuizJustTalk')}

{t('apply.krowReplacesLongFormsAndTests')}

{[ { icon: Clock, label: '5 minutes' }, { icon: Mic, label: 'Voice or Text' }, { icon: Gauge, label: 'Instant Score' }, ].map(b => ( {b.label} ))}
{showForm && selectedJob ? ( /* KROW Identity preview — auto-applies from profile, then AI interview */
navigate((profile?.profile_completion || 0) < 40 ? '/owliver' : '/identity')} /> {/* The candidate's own fit against this role — their skills, this position's requirements, and nothing about anybody else. The recruiter's ranked comparison of every candidate is the same arithmetic and stays on the admin position page. */}
) : ( /* Position List */

{t('apply.chooseAPosition')}

{isLoading && [...Array(4)].map((_, i) =>
)} {openPositions.map(job => ( ))} {!isLoading && openPositions.length === 0 && (
{t('apply.noOpenPositionsRightNowCheck')}
)}
)} {/* AI Interview after applying */} {interviewApp && ( { setInterviewApp(null); navigate('/'); }} application={interviewApp} job={selectedJob} conductor="Owliver" /> )}
); }