"use client";
import { kuwaitDatePickerMinimum } from "@/features/shared/opportunityFormRules";
import RegistrationDeadlineFields from "./RegistrationDeadlineFields";

import { Field, type FieldProps } from "formik";
import { useTranslation } from "react-i18next";
import Input from "@/components/ui/Input";
import TimepickerInput from "@/components/ui/TimePicker";
import OpportunityScheduleDates from "./OpportunityScheduleDates";
import type { VolunteerFormValues } from "./volunteerFormTypes";

interface VolunteerScheduleFieldsProps {
  setFieldTouched: (field: string, touched: boolean) => void;
  /** Clicking a time field opens the picker rather than typing into it. */
  autoSetTimeOnClick: boolean;
}

/**
 * Column 2 of the grid: when the opportunity runs.
 *
 * The due date, the schedule picker — which handles both a consecutive range
 * and scattered days — and the from/to time pair, which shares one column
 * because the two together answer a single question.
 */
export default function VolunteerScheduleFields({
  setFieldTouched,
  autoSetTimeOnClick,
}: VolunteerScheduleFieldsProps) {
  const { t } = useTranslation();

  return (
    <>
    {/* ---- Row 2: when it runs ---- */}
    {/* PDF page 16: an optional relative deadline; ordinary registration ends at first start. */}
    <RegistrationDeadlineFields />
    <OpportunityScheduleDates minDate={kuwaitDatePickerMinimum()} />
    <div className="flex gap-2">
      <Field name="startTime">
        {({
          field,
        }: FieldProps<string, VolunteerFormValues>) => (
          <TimepickerInput
            label={t("COMMON.START_TIME")}
            className="w-full"
            autoSetTime={autoSetTimeOnClick}
            {...field}
          />
        )}
      </Field>
      <Field name="endTime">
        {({
          field,
        }: FieldProps<string, VolunteerFormValues>) => (
          <TimepickerInput
            label={t("COMMON.END_TIME")}
            className="w-full"
            autoSetTime={autoSetTimeOnClick}
            {...field}
          />
        )}
      </Field>
    </div>
    <Input
      name="participantsNeeded"
      label={t("COMMON.PARTICIPANTS_NEEDED")}
      type="text"
      onFocus={() => setFieldTouched("participantsNeeded", true)}
    />

    </>
  );
}
