"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import Link from "next/link"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Checkbox } from "@/components/ui/checkbox"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Textarea } from "@/components/ui/textarea"; import { Building2, Mail, Lock, Eye, EyeOff, Phone, Globe, MapPin, ArrowLeft, CheckCircle2, Briefcase, Loader2, } from "lucide-react"; import { motion } from "framer-motion"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { z } from "zod"; import { Card, CardContent, CardFooter, CardHeader, CardTitle } from "@/components/ui/card"; import { Alert, AlertDescription } from "@/components/ui/alert"; import { paymentsApi } from "@/lib/api"; import { Navbar } from "@/components/navbar"; import { Footer } from "@/components/footer"; const COUNTRIES = [ "Argentina", "Australia", "Austria", "Belgium", "Brazil", "Canada", "Chile", "China", "Colombia", "Czech Republic", "Denmark", "Egypt", "Finland", "France", "Germany", "Greece", "Hong Kong", "Hungary", "India", "Indonesia", "Ireland", "Israel", "Italy", "Japan", "Malaysia", "Mexico", "Netherlands", "New Zealand", "Nigeria", "Norway", "Pakistan", "Peru", "Philippines", "Poland", "Portugal", "Romania", "Saudi Arabia", "Singapore", "South Africa", "South Korea", "Spain", "Sweden", "Switzerland", "Taiwan", "Thailand", "Turkey", "Ukraine", "United Arab Emirates", "United Kingdom", "United States", "Vietnam", ]; const YEARS_IN_MARKET = [ { value: "<1", label: "Less than 1 year" }, { value: "1-3", label: "1 – 3 years" }, { value: "3-5", label: "3 – 5 years" }, { value: "5-10", label: "5 – 10 years" }, { value: "10+", label: "10+ years" }, ]; interface Plan { id: string; name: string; price: string; period: string; priceId: string | null; // Stripe Price ID — null for free/enterprise features: string[]; recommended: boolean; cta: string; } const PLANS: Plan[] = [ { id: "free", name: "Starter", price: "$0", period: "/ month", priceId: null, features: [ "1 active job posting", "Resume collection", "Basic applicant dashboard", "Email notifications", ], recommended: false, cta: "Get started free", }, { id: "pro", name: "Pro", price: "$49", period: "/ month", priceId: process.env.NEXT_PUBLIC_STRIPE_PRO_PRICE_ID || null, features: [ "Unlimited job postings", "Priority in search results", "Talent pool access", "Priority support", "Analytics dashboard", ], recommended: true, cta: "Start Pro", }, { id: "enterprise", name: "Enterprise", price: "Custom", period: "", priceId: null, features: [ "Everything in Pro", "API integration", "Dedicated account manager", "Advanced reporting", "White-label option", ], recommended: false, cta: "Contact sales", }, ]; const companySchema = z .object({ companyName: z.string().min(2, "Company name is required"), email: z.string().email("Invalid email address"), password: z.string().min(8, "Password must be at least 8 characters"), confirmPassword: z.string(), phone: z.string().optional(), country: z.string().min(1, "Country is required"), city: z.string().optional(), website: z.string().url("Invalid URL").optional().or(z.literal("")), yearsInMarket: z.string().min(1, "Years in market is required"), description: z.string().min(20, "Description must be at least 20 characters"), taxId: z.string().optional(), acceptTerms: z.boolean().refine((v) => v === true, "You must accept the terms"), }) .refine((d) => d.password === d.confirmPassword, { message: "Passwords do not match", path: ["confirmPassword"], }); type CompanyFormData = z.infer; const STEPS = ["Plan", "Details", "Terms", "Confirm"]; export default function RegisterPage() { const router = useRouter(); const [step, setStep] = useState(1); const [selectedPlan, setSelectedPlan] = useState(null); const [showPw, setShowPw] = useState(false); const [showConfirmPw, setShowConfirmPw] = useState(false); const [loading, setLoading] = useState(false); const [errorMsg, setErrorMsg] = useState(null); const { register, handleSubmit, formState: { errors }, setValue, trigger, watch, } = useForm({ resolver: zodResolver(companySchema), defaultValues: { yearsInMarket: "", country: "" }, }); const watchedCountry = watch("country"); const nextStep = async () => { setErrorMsg(null); if (step === 1) { if (!selectedPlan) { setErrorMsg("Please select a plan to continue."); return; } setStep(2); } else if (step === 2) { const ok = await trigger([ "companyName", "email", "password", "confirmPassword", "country", "yearsInMarket", "description", ]); if (ok) setStep(3); } else if (step === 3) { const ok = await trigger("acceptTerms"); if (ok) setStep(4); } }; const onSubmit = async (data: CompanyFormData) => { if (!selectedPlan) return; setLoading(true); setErrorMsg(null); try { const { registerCompany } = await import("@/lib/auth"); const res = await registerCompany({ companyName: data.companyName, email: data.email, phone: data.phone, password: data.password, document: data.taxId, website: data.website, yearsInMarket: data.yearsInMarket, description: data.description, city: data.city, country: data.country, }); // Auto-login if token returned if (res?.token) { localStorage.setItem("auth_token", res.token); localStorage.setItem("token", res.token); localStorage.setItem("user", JSON.stringify({ name: data.companyName, email: data.email, role: "recruiter", })); } // Enterprise → redirect to contact / dashboard if (selectedPlan.id === "enterprise") { router.push("/dashboard?plan=enterprise"); return; } // Free plan → go straight to dashboard if (!selectedPlan.priceId) { router.push("/dashboard"); return; } // Paid plan → create Stripe checkout session try { const origin = window.location.origin; const checkout = await paymentsApi.createSubscriptionCheckout({ priceId: selectedPlan.priceId, successUrl: `${origin}/dashboard?payment=success`, cancelUrl: `${origin}/register?payment=cancelled`, }); if (checkout.checkoutUrl) { window.location.href = checkout.checkoutUrl; return; } } catch (stripeError) { console.warn("Stripe checkout failed, falling back:", stripeError); // Fallback: redirect to dashboard with a pending payment notice router.push("/dashboard?payment=pending"); return; } router.push("/dashboard"); } catch (error: unknown) { const msg = error instanceof Error ? error.message : "Registration failed. Please try again."; setErrorMsg(msg); } finally { setLoading(false); } }; return (
{/* Step indicator */}
{STEPS.map((label, i) => (
i + 1 ? "bg-green-500 text-white" : step === i + 1 ? "bg-primary text-primary-foreground" : "bg-muted text-muted-foreground" }`} > {step > i + 1 ? : i + 1}
{label}
))}
{errorMsg && ( {errorMsg} )}
{/* STEP 1: PLAN SELECTION */} {step === 1 && (

Choose your plan

Start hiring the best talent worldwide today.

{PLANS.map((plan) => ( { setSelectedPlan(plan); setErrorMsg(null); }} > {plan.recommended && (
Recommended
)} {plan.name}
{plan.price} {plan.period && ( {plan.period} )}
    {plan.features.map((f) => (
  • {f}
  • ))}
{selectedPlan?.id === plan.id && (
)}
))}
)} {/* STEP 2: COMPANY DETAILS */} {step === 2 && (

Company details

Fill in your company and account information.

{/* Company name + Years in market */}
{errors.companyName &&

{errors.companyName.message}

}
{errors.yearsInMarket &&

{errors.yearsInMarket.message}

}
{/* Description */}