"use client"

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 { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"
import { Textarea } from "@/components/ui/textarea"

const schema = z.object({
  templateCode: z.string().min(1, "Template code is required."),
  content: z.string().min(1, "Content is required."),
})

export default function Page() {
  const t = useTranslations("Cms.Templates.Create")
  const tc = useTranslations("Common")

  const form = useForm<z.infer<typeof schema>>({
    resolver: zodResolver(schema),
    defaultValues: {
      templateCode: "homepage",
      content: "",
    },
  })

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

      <Form {...form}>
        <form className="grid gap-6" onSubmit={form.handleSubmit(() => {})}>
          <FormSection title={t("sections.template")}>
            <div className="grid gap-4">
              <FormField
                control={form.control}
                name="templateCode"
                render={({ field }) => (
                  <FormItem>
                    <FormLabel>{t("labels.templateCode")}</FormLabel>
                    <Select value={field.value} onValueChange={field.onChange}>
                      <FormControl>
                        <SelectTrigger className="w-full sm:max-w-xs">
                          <SelectValue />
                        </SelectTrigger>
                      </FormControl>
                      <SelectContent>
                        <SelectItem value="homepage">{t("codes.homepage")}</SelectItem>
                        <SelectItem value="footer-links">{t("codes.footerLinks")}</SelectItem>
                      </SelectContent>
                    </Select>
                    <FormMessage />
                  </FormItem>
                )}
              />

              <FormField
                control={form.control}
                name="content"
                render={({ field }) => (
                  <FormItem>
                    <FormLabel>{t("labels.content")}</FormLabel>
                    <FormControl>
                      <Textarea
                        rows={16}
                        className="min-h-64 font-mono text-sm"
                        spellCheck={false}
                        placeholder={t("placeholders.content")}
                        {...field}
                      />
                    </FormControl>
                    <FormMessage />
                  </FormItem>
                )}
              />
            </div>
          </FormSection>

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

