"use client";
import DatePicker from "@/components/FormElements/DatePicker/DatePicker";
import InputGroup from "@/components/FormElements/InputGroup";
import { TextAreaGroup } from "@/components/FormElements/InputGroup/text-area";
import MultiSelect from "@/components/FormElements/MultiSelect";
import { Select } from "@/components/FormElements/select";
import { ShowcaseSection } from "@/components/Layouts/showcase-section";
import FormFooter from "@/components/ui/FormFooter";
import { REGEX } from "@/constants/regex";
import { FormErrorMessages } from "@/constants/Texts";
import useCreateNotificationFormPresenter from "./useCreateNotificationFormPresenter";

const CreateNotificationForm = () => {
  const {
    register,
    errors,
    control,
    handleSubmit,
    recipient,
    onSubmit,
    onSelectTarget,
    priorities,
    targets,
    types,
    channels,
  } = useCreateNotificationFormPresenter();
  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <ShowcaseSection
        title="Create Notification"
        className="grid grid-cols-2 gap-5"
      >
        <div className="flex flex-col gap-2">
          <InputGroup
            required
            {...register("title", {
              required: { value: true, message: FormErrorMessages.required },
            })}
            name="title"
            label="Title"
            type="text"
            placeholder="Enter title"
          />
          {errors.title && (
            <p className="mt-1 text-sm text-red-500">{errors.title.message}</p>
          )}
        </div>
        <div className="flex flex-col gap-2">
          <DatePicker
            control={control}
            name="schedule_at"
            label="schedule at"
            required
            range={false}
            rules={{
              required: {
                value: true,
                message: FormErrorMessages.required,
              },
            }}
            placeholder="Select schedule date"
          />
        </div>
        <div className="flex flex-col gap-2">
          <Select
            required
            {...register("target", {
              required: {
                value: true,
                message: FormErrorMessages.required,
              },
              onChange: onSelectTarget,
            })}
            items={targets}
            label="target"
            placeholder="Select target"
            name="target"
          />
          {errors.target && (
            <p className="mt-1 text-sm text-red-500">{errors.target.message}</p>
          )}
        </div>

        <div className="flex flex-col gap-2">
          <MultiSelect
            disabled={!recipient}
            required={!!recipient}
            {...register("recipient")}
            label="recipient"
            items={recipient ?? []}
            placeholder="Select recipient"
            name="recipient"
          />
        </div>
        <div className="flex flex-col gap-2">
          <MultiSelect
            required
            {...register("channels")}
            label="channels"
            items={channels ?? []}
            placeholder="Select channels"
            name="channels"
          />
        </div>
        <div className="flex flex-col gap-2">
          <InputGroup
            required
            {...register("link", {
              required: { value: true, message: FormErrorMessages.required },
              pattern: {
                value: REGEX.URL,
                message: FormErrorMessages.invalidPattern,
              },
            })}
            name="link"
            label="Link"
            type="url"
            placeholder="Enter link"
          />
          {errors.link && (
            <p className="mt-1 text-sm text-red-500">{errors.link.message}</p>
          )}
        </div>
        <div className="flex flex-col gap-2">
          <Select
            required
            {...register("priority", {
              required: {
                value: true,
                message: FormErrorMessages.required,
              },
            })}
            items={priorities}
            label="Priority"
            placeholder="Select priority"
            name="priority"
          />
          {errors.priority && (
            <p className="mt-1 text-sm text-red-500">
              {errors.priority.message}
            </p>
          )}
        </div>
        <div className="flex flex-col gap-2">
          <Select
            required
            {...register("type", {
              required: {
                value: true,
                message: FormErrorMessages.required,
              },
            })}
            items={types}
            label="type"
            placeholder="Select type"
            name="type"
          />
          {errors.type && (
            <p className="mt-1 text-sm text-red-500">{errors.type.message}</p>
          )}
        </div>
        <div className="flex flex-col gap-2 col-span-2">
        <TextAreaGroup
          {...register("description")}
          label="Description"
          name="description"
          placeholder="Enter description"
        />
        </div>
        <FormFooter />
      </ShowcaseSection>
    </form>
  );
};

export default CreateNotificationForm;
