"use client";

import { useFormContext } from "react-hook-form";
import { JobPostingFormValues } from "@/lib/validations/job";
import { Building, Globe, MapPin } from "lucide-react";

export function Step2Location() {
    const { register, watch, formState: { errors } } = useFormContext<JobPostingFormValues>();

    const workLocationType = watch("work_location_type");

    return (
        <div className="space-y-8 animate-in fade-in slide-in-from-bottom-4 duration-500">
            <div>
                <h2 className="text-2xl font-bold text-slate-900">Location Details</h2>
                <p className="text-slate-500 mt-1">Specify where and how the candidate will work.</p>
            </div>

            <div className="space-y-6">
                {/* Work Location Type (Radio Cards) */}
                <div>
                    <label className="text-sm font-semibold text-slate-700 block mb-3">Work Setup <span className="text-red-500">*</span></label>
                    <div className="grid grid-cols-1 md:grid-cols-3 gap-4">

                        {/* On-Site Option */}
                        <label className={`cursor-pointer w-full rounded-2xl border-2 p-4 flex flex-col items-center gap-3 transition-all ${workLocationType === "on-site"
                                ? "border-primary bg-primary/5 shadow-sm"
                                : "border-slate-200 hover:border-slate-300 hover:bg-slate-50"
                            }`}>
                            <input
                                type="radio"
                                value="on-site"
                                {...register("work_location_type")}
                                className="sr-only"
                            />
                            <div className={`p-3 rounded-full ${workLocationType === "on-site" ? "bg-primary text-white" : "bg-slate-100 text-slate-500"}`}>
                                <Building className="h-6 w-6" />
                            </div>
                            <div className="text-center">
                                <p className={`font-bold ${workLocationType === "on-site" ? "text-primary" : "text-slate-800"}`}>On-Site</p>
                                <p className="text-xs text-slate-500 mt-1">Candidate must report to a physical office.</p>
                            </div>
                        </label>

                        {/* Remote Option */}
                        <label className={`cursor-pointer w-full rounded-2xl border-2 p-4 flex flex-col items-center gap-3 transition-all ${workLocationType === "remote"
                                ? "border-primary bg-primary/5 shadow-sm"
                                : "border-slate-200 hover:border-slate-300 hover:bg-slate-50"
                            }`}>
                            <input
                                type="radio"
                                value="remote"
                                {...register("work_location_type")}
                                className="sr-only"
                            />
                            <div className={`p-3 rounded-full ${workLocationType === "remote" ? "bg-primary text-white" : "bg-slate-100 text-slate-500"}`}>
                                <Globe className="h-6 w-6" />
                            </div>
                            <div className="text-center">
                                <p className={`font-bold ${workLocationType === "remote" ? "text-primary" : "text-slate-800"}`}>Remote</p>
                                <p className="text-xs text-slate-500 mt-1">Candidate can work from anywhere.</p>
                            </div>
                        </label>

                        {/* Hybrid Option */}
                        <label className={`cursor-pointer w-full rounded-2xl border-2 p-4 flex flex-col items-center gap-3 transition-all ${workLocationType === "hybrid"
                                ? "border-primary bg-primary/5 shadow-sm"
                                : "border-slate-200 hover:border-slate-300 hover:bg-slate-50"
                            }`}>
                            <input
                                type="radio"
                                value="hybrid"
                                {...register("work_location_type")}
                                className="sr-only"
                            />
                            <div className={`p-3 rounded-full ${workLocationType === "hybrid" ? "bg-primary text-white" : "bg-slate-100 text-slate-500"}`}>
                                <MapPin className="h-6 w-6" />
                            </div>
                            <div className="text-center">
                                <p className={`font-bold ${workLocationType === "hybrid" ? "text-primary" : "text-slate-800"}`}>Hybrid</p>
                                <p className="text-xs text-slate-500 mt-1">Mix of in-office and remote work.</p>
                            </div>
                        </label>

                    </div>
                </div>

                {/* Dynamic Fields based on selection */}
                <div className="bg-slate-50 p-6 rounded-2xl border border-slate-200 space-y-6">

                    {(workLocationType === "on-site" || workLocationType === "hybrid") && (
                        <div className="animate-in fade-in slide-in-from-top-2 duration-300 space-y-6">
                            <h3 className="font-semibold text-slate-800 border-b pb-2">Office Address</h3>

                            <div className="space-y-2">
                                <label className="text-sm font-semibold text-slate-700">Street Address</label>
                                <input
                                    {...register("address")}
                                    type="text"
                                    placeholder="e.g. 123 Tech Boulevard, Suite 400"
                                    className="w-full rounded-xl border border-slate-200 px-4 py-3 outline-none focus:border-primary focus:ring-1 focus:ring-primary/20 bg-white"
                                />
                            </div>

                            <div className="grid grid-cols-1 md:grid-cols-3 gap-4">
                                <div className="space-y-2">
                                    <label className="text-sm font-semibold text-slate-700">City <span className="text-red-500">*</span></label>
                                    <input
                                        {...register("city")}
                                        type="text"
                                        placeholder="e.g. San Francisco"
                                        className={`w-full rounded-xl border px-4 py-3 outline-none bg-white transition-all ${errors.city ? "border-red-300 focus:border-red-500" : "border-slate-200 focus:border-primary focus:ring-1 focus:ring-primary/20"
                                            }`}
                                    />
                                    {errors.city && <p className="text-xs text-red-500 mt-1">{errors.city.message}</p>}
                                </div>
                                <div className="space-y-2">
                                    <label className="text-sm font-semibold text-slate-700">State/Province</label>
                                    <input
                                        {...register("state")}
                                        type="text"
                                        placeholder="e.g. CA"
                                        className="w-full rounded-xl border border-slate-200 px-4 py-3 outline-none focus:border-primary focus:ring-1 focus:ring-primary/20 bg-white"
                                    />
                                </div>
                                <div className="space-y-2">
                                    <label className="text-sm font-semibold text-slate-700">Country</label>
                                    <input
                                        {...register("country")}
                                        type="text"
                                        placeholder="e.g. United States"
                                        className="w-full rounded-xl border border-slate-200 px-4 py-3 outline-none focus:border-primary focus:ring-1 focus:ring-primary/20 bg-white"
                                    />
                                </div>
                            </div>

                            {workLocationType === "hybrid" && (
                                <div className="space-y-2 pt-4 border-t border-slate-200">
                                    <label className="text-sm font-semibold text-slate-700">Days in Office (per week)</label>
                                    <select
                                        {...register("days_in_office")}
                                        className="w-full rounded-xl border border-slate-200 px-4 py-3 outline-none focus:border-primary focus:ring-1 focus:ring-primary/20 bg-white max-w-xs"
                                    >
                                        {[1, 2, 3, 4, 5].map(day => (
                                            <option key={day} value={day}>{day} {day === 1 ? 'Day' : 'Days'}</option>
                                        ))}
                                    </select>
                                </div>
                            )}
                        </div>
                    )}

                    {workLocationType === "remote" && (
                        <div className="animate-in fade-in slide-in-from-top-2 duration-300 space-y-6">
                            <h3 className="font-semibold text-slate-800 border-b pb-2">Remote Requirements</h3>

                            <div className="grid grid-cols-1 md:grid-cols-2 gap-6">
                                <div className="space-y-2">
                                    <label className="text-sm font-semibold text-slate-700">Geographic Restrictions</label>
                                    <input
                                        {...register("remote_geo_restriction")}
                                        type="text"
                                        placeholder="e.g. US Only, EMEA, Anywhere"
                                        className="w-full rounded-xl border border-slate-200 px-4 py-3 outline-none focus:border-primary focus:ring-1 focus:ring-primary/20 bg-white"
                                    />
                                    <p className="text-xs text-slate-500">Leave blank if truly work-from-anywhere.</p>
                                </div>

                                <div className="space-y-2">
                                    <label className="text-sm font-semibold text-slate-700">Timezone Overlap Required</label>
                                    <input
                                        {...register("time_zone_requirements")}
                                        type="text"
                                        placeholder="e.g. 4 hours overlap with EST"
                                        className="w-full rounded-xl border border-slate-200 px-4 py-3 outline-none focus:border-primary focus:ring-1 focus:ring-primary/20 bg-white"
                                    />
                                </div>
                            </div>
                        </div>
                    )}

                </div>
            </div>
        </div>
    );
}
