"use client"

import { use } from "react"
import { Link } from "@/i18n/navigation"
import { z } from "zod"
import { zodResolver } from "@hookform/resolvers/zod"
import { useForm } from "react-hook-form"
import {useTranslations} from "next-intl"

import { FormSection } from "@/components/form-section"
import { PageHeader } from "@/components/page-header"
import { Button } from "@/components/ui/button"
import {
  Form,
  FormControl,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
} from "@/components/ui/form"
import { Input } from "@/components/ui/input"

const schema = z.object({
  code: z.string().min(3).max(3),
  name: z.string().min(2, "Name is required."),
  symbol: z.string().min(1, "Symbol is required."),
})

export default function Page({ params }: { params: Promise<{ id: string }> }) {
  const { id } = use(params)
  const t = useTranslations("Currencies.Edit")
  const tCreate = useTranslations("Currencies.Create")
  const tc = useTranslations("Common")

  const form = useForm<z.infer<typeof schema>>({
    resolver: zodResolver(schema),
    defaultValues: { code: id.toUpperCase(), name: "Currency", symbol: "$" },
  })

  return (
    <div className="flex flex-col gap-4 md:gap-6 py-4 md:py-6">
      <PageHeader
        title={t("title", { id })}
        actions={
          <Button variant="outline" asChild>
            <Link href="/currencies">{tc("actions.back")}</Link>
          </Button>
        }
      />

      <Form {...form}>
        <form className="gap-6 grid" onSubmit={form.handleSubmit(() => {})}>
          <FormSection title={t("sections.currency")}>
            <div className="gap-4 grid md:grid-cols-3">
              <FormField
                control={form.control}
                name="code"
                render={({ field }) => (
                  <FormItem>
                    <FormLabel>{tCreate("labels.code")}</FormLabel>
                    <FormControl>
                      <Input disabled {...field} />
                    </FormControl>
                    <FormMessage />
                  </FormItem>
                )}
              />
              <FormField
                control={form.control}
                name="name"
                render={({ field }) => (
                  <FormItem className="md:col-span-2">
                    <FormLabel>{tCreate("labels.name")}</FormLabel>
                    <FormControl>
                      <Input {...field} />
                    </FormControl>
                    <FormMessage />
                  </FormItem>
                )}
              />
              <FormField
                control={form.control}
                name="symbol"
                render={({ field }) => (
                  <FormItem>
                    <FormLabel>{tCreate("labels.symbol")}</FormLabel>
                    <FormControl>
                      <Input {...field} />
                    </FormControl>
                    <FormMessage />
                  </FormItem>
                )}
              />
            </div>
          </FormSection>

          <div className="px-4 lg:px-6">
            <div className="flex justify-end items-center gap-2">
              <Button variant="outline" type="button" asChild>
                <Link href="/currencies">{tc("actions.cancel")}</Link>
              </Button>
              <Button type="submit">{tc("actions.saveChanges")}</Button>
            </div>
          </div>
        </form>
      </Form>
    </div>
  )
}

