import type { Route } from './+types/route'

import { useForm } from 'react-hook-form'
import { PatternFormat } from 'react-number-format'
import { useOutletContext } from 'react-router'

import { Button } from '@repo/ui/components/ui/button'
import {
  Card,
  CardContent,
  CardDescription,
  CardHeader,
  CardTitle
} from '@repo/ui/components/ui/card'
import { FieldSet } from '@repo/ui/components/ui/field'
import {
  Form,
  FormControl,
  FormField,
  FormItem,
  FormLabel,
  FormMessage
} from '@repo/ui/components/ui/form'
import { Input } from '@repo/ui/components/ui/input'

import type { Org } from '../_app.orgs.$id/data'

export default function Route({}: Route.ComponentProps) {
  const { org } = useOutletContext() as { org: Org }
  const form = useForm({ defaultValues: org })
  const { handleSubmit, control } = form

  const onSubmit = async () => {}

  return (
    <Form {...form}>
      <form onSubmit={handleSubmit(onSubmit)} className="space-y-8">
        <Card>
          <CardHeader>
            <CardTitle className="font-semibold text-2xl">
              Editar empresa
            </CardTitle>
            <CardDescription>
              Configurar as informações gerais para esta empresa.
            </CardDescription>
          </CardHeader>

          <CardContent className="space-y-6">
            <FieldSet disabled={true}>
              <FormField
                control={control}
                name="name"
                render={({ field }) => (
                  <FormItem>
                    <FormLabel>Nome</FormLabel>
                    <FormControl>
                      <Input {...field} />
                    </FormControl>
                    <FormMessage />
                  </FormItem>
                )}
              />

              <FormField
                control={control}
                name="email"
                render={({ field }) => (
                  <FormItem>
                    <FormLabel>Email</FormLabel>
                    <FormControl>
                      <Input {...field} />
                    </FormControl>
                    <FormMessage />
                  </FormItem>
                )}
              />

              <FormField
                control={control}
                name="cnpj"
                render={({
                  field: { onChange, ref, ...field },
                  fieldState
                }) => (
                  <FormItem>
                    <FormLabel>CNPJ</FormLabel>
                    <FormControl>
                      <PatternFormat
                        id={field.name}
                        format="##.###.###/####-##"
                        mask="_"
                        placeholder="__.___.___/____-__"
                        customInput={Input}
                        getInputRef={ref}
                        aria-invalid={fieldState.invalid}
                        onValueChange={({ value }) => {
                          onChange(value)
                        }}
                        {...field}
                      />
                    </FormControl>
                    <FormMessage />
                  </FormItem>
                )}
              />
            </FieldSet>

            <Button type="submit" disabled={true}>
              Atualizar perfil
            </Button>
          </CardContent>
        </Card>
      </form>
    </Form>
  )
}
