import React, { FormEvent } from 'react'
import { Label, Select, TextInput } from 'flowbite-react'

import formTheme from '@/lib/themes/form'
import DatePicker, { FormattedDate } from '@/lib/components/DatePicker'
import SearchButton from '@/lib/components/buttons/SearchButton'
import { ApprovalStatus, SearchFormProps } from '../types'
import {
  FOLLOW_UP_STATUS_OPTIONS,
  FollowUpStatus,
} from '@/lib/constants/follow-up'
import FollowUpRatingSelector from '@/lib/components/forms/FollowUpRatingSelector'

interface Props {
  searchForm: SearchFormProps
  handleSearch: (e?: FormEvent) => void
  setSearchForm: React.Dispatch<React.SetStateAction<SearchFormProps>>
}

export default function SearchForm({
  searchForm,
  handleSearch,
  setSearchForm,
}: Props) {
  function handleDateChange(formattedDate: FormattedDate) {
    setSearchForm({
      ...searchForm,
      params: {
        ...searchForm.params,
        startDate: formattedDate?.startDate ?? null,
        endDate: formattedDate?.endDate ?? null,
      },
    })
  }

  return (
    <form className="mt-10" onSubmit={handleSearch}>
      <div className="grid grid-cols-1 sm:grid-cols-2 xl:grid-cols-3 w-full gap-4 max-w-6xl">
        {/* Invoice Number */}
        <div className="w-full">
          <div className="mb-2 block">
            <Label theme={formTheme.label} htmlFor="invoice_number">
              Invoice Number
            </Label>
          </div>
          <TextInput
            theme={formTheme.textInput}
            id="invoice_number"
            type="text"
            placeholder="Search by invoice number..."
            className="w-full max-w-96"
            autoComplete="off"
            value={searchForm.params.invoiceNumber}
            onChange={(e) =>
              setSearchForm({
                ...searchForm,
                params: {
                  ...searchForm.params,
                  invoiceNumber: e.target.value,
                },
              })
            }
          />
        </div>

        {/* Request Date */}
        <div className="flex w-full max-w-96 gap-4">
          <div className="flex-grow">
            <div className="mb-2 block">
              <Label theme={formTheme.label} htmlFor="request_date">
                Request Date
              </Label>
            </div>
            <DatePicker
              inputId="request_date"
              value={{
                startDate: searchForm.params.startDate
                  ? new Date(searchForm.params.startDate)
                  : null,
                endDate: searchForm.params.endDate
                  ? new Date(searchForm.params.endDate)
                  : null,
              }}
              onDateChange={handleDateChange}
            />
          </div>
        </div>

        {/* Approval Status */}
        <div className="flex w-full max-w-96 gap-4">
          <div className="flex-grow">
            <div className="mb-2 block">
              <Label theme={formTheme.label} htmlFor="status">
                Approval Status
              </Label>
            </div>
            <Select
              theme={formTheme.select}
              id="approval_status"
              defaultValue={searchForm.params.status || ''}
              onChange={(e) =>
                setSearchForm({
                  ...searchForm,
                  params: {
                    ...searchForm.params,
                    status: e.target.value as ApprovalStatus,
                  },
                })
              }
            >
              <option value="">All</option>
              <option value="pending">Pending</option>
              <option value="approved">Approved</option>
              <option value="rejected">Rejected</option>
            </Select>
          </div>
        </div>

        {/* Follow Up Status */}
        <div className="flex w-full max-w-96 gap-4">
          <div className="flex-grow">
            <div className="mb-2 block">
              <Label theme={formTheme.label} htmlFor="follow_up_status">
                Follow Up Status
              </Label>
            </div>
            <Select
              theme={formTheme.select}
              id="follow_up_status"
              defaultValue={searchForm.params.followUpStatus ?? ''}
              onChange={(e) =>
                setSearchForm({
                  ...searchForm,
                  params: {
                    ...searchForm.params,
                    followUpStatus: (e.target.value as FollowUpStatus) || null,
                  },
                })
              }
            >
              <option value="">All</option>
              {FOLLOW_UP_STATUS_OPTIONS.map((followUpStatus) => (
                <option key={followUpStatus.value} value={followUpStatus.value}>
                  {followUpStatus.name}
                </option>
              ))}
            </Select>
          </div>
        </div>

        {/* Follow Up Rating */}
        <FollowUpRatingSelector
          label="Follow Up Rating"
          placeholder="All"
          value={searchForm.params.followUpRatingId}
          onChange={(rating) => {
            setSearchForm({
              ...searchForm,
              params: {
                ...searchForm.params,
                followUpRatingId: rating?.id,
              },
            })
          }}
        />
      </div>

      {/* Action Buttons */}
      <SearchButton isProcessing={searchForm.searching} className="mt-5" />
    </form>
  )
}
