'use client'
import type {FunctionComponent} from 'react'
import {useActionState} from 'react'
import type {Contact} from '@/models/contact'
import {Avatar, AvatarImage} from '@/components/ui/avatar'
import {Button} from '@/components/ui/button'
import {Loader2, Plus, Trash} from 'lucide-react'
import {Textarea} from '@/components/ui/textarea'
import {Input} from '@/components/ui/input'
import LinedCircleIconButton from '@/components/custom/linedCircleIconButton'
import SubmitButtonWithLoading from '@/components/custom/submitButtonWithLoading'
import {useArrayState} from '@/lib/useArrayState'
import {deleteContactServerFunction, updateContactAction} from '@/serverFunctions/contacts'

const ContactDetailForm: FunctionComponent<Contact> = contact => {
  const [contactInfo, setContactInfo, updateContactInfoItem, deleteContactInfoItem] = useArrayState<{
    type: string
    value: string
  }>(contact.contactInfo)
  const [, updateContact] = useActionState(updateContactAction, undefined)

  return (
    <form action={updateContact}>
      <input type="hidden" name="id" value={contact.id} />
      <div className="flex items-center">
        <Avatar className="h-18 w-18">
          <AvatarImage src={contact.avatar ?? `https://ui-avatars.com/api/?name=${contact.firstName}`} />
        </Avatar>
        <div className="text-4xl ms-4 flex-grow">
          <div className="flex gap-4 me-4">
            <div className="flex-grow">
              <Input id="name" type="text" name="firstName" defaultValue={contact.firstName ?? ''} placeholder="John" />
            </div>
            <div className="flex-grow">
              <Input id="name" type="text" name="lastName" defaultValue={contact.lastName ?? ''} placeholder="Doe" />
            </div>
          </div>
        </div>
      </div>

      <div className="text-muted-foreground my-4">
        <Textarea name="description" defaultValue={contact.description ?? ''} placeholder="Description" />
      </div>

      <div className="text-xl">Contactinfo</div>

      <div className="flex flex-col gap-4 my-4">
        {contactInfo.map(({type, value}, i) => (
          <div className="flex gap-4" key={i}>
            <Input
              id={`contactInfo.${i}.type`}
              type="text"
              className="flex-grow"
              name={`contactInfo.${i}.type`}
              value={type}
              onChange={evt => updateContactInfoItem(i, {type: evt.target.value, value})}
              placeholder="Contact type (e.g. email, phone)"
            />
            <Input
              id={`contactInfo.${i}.value`}
              name={`contactInfo.${i}.value`}
              type="text"
              className="flex-grow"
              value={value}
              onChange={evt => updateContactInfoItem(i, {type, value: evt.target.value})}
              placeholder="Contact info"
            />
            <Button
              variant="ghost"
              onClick={evt => {
                evt.preventDefault()
                deleteContactInfoItem(i)
              }}>
              <Trash />
            </Button>
          </div>
        ))}
      </div>

      <LinedCircleIconButton
        Icon={Plus}
        disabled={contactInfo?.at(-1)?.value === '' || contactInfo?.at(-1)?.type === ''}
        onClick={() => setContactInfo(old => [...old, {type: '', value: ''}])}
      />

      <div className="flex flex-col gap-4 mt-4">
        <SubmitButtonWithLoading loadingText="Updating contact..." text="Update contact" />
        <Button
          variant="destructive"
          className="w-full"
          disabled={false}
          onClick={evt => {
            // Prevent the form from submitting.
            evt.preventDefault()
            void deleteContactServerFunction({id: contact.id})
          }}>
          {false && <Loader2 className="animate-spin" />} Delete contact
        </Button>
      </div>
    </form>
  )
}

export default ContactDetailForm
