"use client"

import { updateImage } from "@/actions/portfolio/images/updateImage";
import { Button } from "@/components/ui/button";
import { Calendar } from "@/components/ui/calendar";
import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import MultipleSelector from "@/components/ui/multiselect";
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
import { Switch } from "@/components/ui/switch";
import { Textarea } from "@/components/ui/textarea";
import { Color, ImageColor, ImageMetadata, ImageVariant, PortfolioCategory, PortfolioImage, PortfolioTag, PortfolioType } from "@/generated/prisma";
import { cn } from "@/lib/utils";
import { imageSchema } from "@/schemas/portfolio/imageSchema";
import { zodResolver } from "@hookform/resolvers/zod";
import { format } from "date-fns";
import { useRouter } from "next/navigation";
import { useForm } from "react-hook-form";
import { toast } from "sonner";
import { z } from "zod/v4";

type ImageWithItems = PortfolioImage & {
  metadata: ImageMetadata | null,
  colors: (
    ImageColor & {
      color: Color
    }
  )[],
  variants: ImageVariant[],
  categories: PortfolioCategory[],
  tags: PortfolioTag[],
  type: PortfolioType | null,
};


export default function EditImageForm({ image, categories, tags, types }:
  {
    image: ImageWithItems,
    categories: PortfolioCategory[]
    tags: PortfolioTag[],
    types: PortfolioType[]
  }) {
  const router = useRouter();
  const form = useForm<z.infer<typeof imageSchema>>({
    resolver: zodResolver(imageSchema),
    defaultValues: {
      fileKey: image.fileKey,
      originalFile: image.originalFile,
      nsfw: image.nsfw ?? false,
      published: image.nsfw ?? false,
      setAsHeader: image.setAsHeader ?? false,
      name: image.name,

      altText: image.altText || "",
      description: image.description || "",
      fileType: image.fileType || "",
      layoutGroup: image.layoutGroup || "",
      fileSize: image.fileSize || undefined,
      layoutOrder: image.layoutOrder || undefined,
      month: image.month || undefined,
      year: image.year || undefined,
      creationDate: image.creationDate ? new Date(image.creationDate) : undefined,

      typeId: image.typeId ?? undefined,
      tagIds: image.tags?.map(tag => tag.id) ?? [],
      categoryIds: image.categories?.map(cat => cat.id) ?? [],
    }
  })

  async function onSubmit(values: z.infer<typeof imageSchema>) {
    const updatedImage = await updateImage(values, image.id)
    if (updatedImage) {
      toast.success("Image updated")
      router.push(`/portfolio`)
    }
  }

  return (
    <div className="flex flex-col gap-8">
      <Form {...form}>
        <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8">
          {/* String */}
          <FormField
            control={form.control}
            name="name"
            render={({ field }) => (
              <FormItem>
                <FormLabel>Image name</FormLabel>
                <FormControl>
                  <Input {...field} placeholder="The public display name" />
                </FormControl>
                <FormMessage />
              </FormItem>
            )}
          />
          <FormField
            control={form.control}
            name="altText"
            render={({ field }) => (
              <FormItem>
                <FormLabel>Alt Text</FormLabel>
                <FormControl>
                  <Input {...field} placeholder="Alt for this image" />
                </FormControl>
                <FormMessage />
              </FormItem>
            )}
          />
          <FormField
            control={form.control}
            name="description"
            render={({ field }) => (
              <FormItem>
                <FormLabel>Description</FormLabel>
                <FormControl>
                  <Textarea {...field} placeholder="A descriptive text to the image" />
                </FormControl>
                <FormMessage />
              </FormItem>
            )}
          />
          {/* Number */}
          <FormField
            control={form.control}
            name="month"
            render={({ field }) => (
              <FormItem>
                <FormLabel>Creation Month</FormLabel>
                <FormControl>
                  <Input {...field} type="number" />
                </FormControl>
                <FormMessage />
              </FormItem>
            )}
          />
          <FormField
            control={form.control}
            name="year"
            render={({ field }) => (
              <FormItem>
                <FormLabel>Creation Year</FormLabel>
                <FormControl>
                  <Input {...field} type="number" />
                </FormControl>
                <FormMessage />
              </FormItem>
            )}
          />
          {/* Date  */}
          <FormField
            control={form.control}
            name="creationDate"
            render={({ field }) => (
              <FormItem className="flex flex-col gap-1">
                <FormLabel>Creation Date</FormLabel>
                <div className="flex items-center gap-2">
                  <Popover>
                    <PopoverTrigger asChild>
                      <FormControl>
                        <Button
                          variant="outline"
                          className={cn(
                            "pl-3 text-left font-normal",
                            !field.value && "text-muted-foreground"
                          )}
                        >
                          {field.value ? format(field.value, "PPP") : "Pick a date"}
                        </Button>
                      </FormControl>
                    </PopoverTrigger>
                    <PopoverContent className="w-auto p-0" align="start">
                      <Calendar
                        mode="single"
                        selected={field.value}
                        onSelect={(date) => {
                          field.onChange(date)
                        }}
                        initialFocus
                        fromYear={1990}
                        toYear={2030}
                        captionLayout="dropdown"
                      />
                    </PopoverContent>
                  </Popover>
                </div>
                <FormMessage />
              </FormItem>
            )}
          />
          {/* Select */}
          <FormField
            control={form.control}
            name="typeId"
            render={({ field }) => (
              <FormItem>
                <FormLabel>Art Type</FormLabel>
                <Select
                  onValueChange={(value) => field.onChange(value === "" ? undefined : value)}
                  value={field.value ?? ""}
                >
                  <FormControl>
                    <SelectTrigger>
                      <SelectValue placeholder="Select an art type" />
                    </SelectTrigger>
                  </FormControl>
                  <SelectContent>
                    {types.map((type) => (
                      <SelectItem key={type.id} value={type.id}>
                        {type.name}
                      </SelectItem>
                    ))}
                  </SelectContent>
                </Select>
                <FormMessage />
              </FormItem>
            )}
          />
          <FormField
            control={form.control}
            name="tagIds"
            render={({ field }) => {
              const selectedOptions = tags
                .filter(tag => field.value?.includes(tag.id))
                .map(tag => ({ label: tag.name, value: tag.id }));
              return (
                <FormItem>
                  <FormLabel>Tags</FormLabel>
                  <FormControl>
                    <MultipleSelector
                      defaultOptions={tags.map(tag => ({
                        label: tag.name,
                        value: tag.id,
                      }))}
                      placeholder="Select tags"
                      hidePlaceholderWhenSelected
                      selectFirstItem
                      value={selectedOptions}
                      onChange={(options) => {
                        const ids = options.map(option => option.value);
                        field.onChange(ids);
                      }}
                    />
                  </FormControl>
                  <FormMessage />
                </FormItem>
              )
            }}
          />
          <FormField
            control={form.control}
            name="categoryIds"
            render={({ field }) => {
              const selectedOptions = categories
                .filter(cat => field.value?.includes(cat.id))
                .map(cat => ({ label: cat.name, value: cat.id }));
              return (
                <FormItem>
                  <FormLabel>Categories</FormLabel>
                  <FormControl>
                    <MultipleSelector
                      defaultOptions={categories.map(cat => ({
                        label: cat.name,
                        value: cat.id,
                      }))}
                      placeholder="Select categories"
                      hidePlaceholderWhenSelected
                      selectFirstItem
                      value={selectedOptions}
                      onChange={(options) => {
                        const ids = options.map(option => option.value);
                        field.onChange(ids);
                      }}
                    />
                  </FormControl>
                  <FormMessage />
                </FormItem>
              )
            }}
          />
          {/* Boolean */}
          <FormField
            control={form.control}
            name="nsfw"
            render={({ field }) => (
              <FormItem className="flex items-center justify-between rounded-lg border p-4">
                <div className="space-y-0.5">
                  <FormLabel>NSFW</FormLabel>
                  <FormDescription>This image contains sensitive or adult content.</FormDescription>
                </div>
                <FormControl>
                  <Switch checked={field.value} onCheckedChange={field.onChange} />
                </FormControl>
              </FormItem>
            )}
          />
          <FormField
            control={form.control}
            name="published"
            render={({ field }) => (
              <FormItem className="flex items-center justify-between rounded-lg border p-4">
                <div className="space-y-0.5">
                  <FormLabel>Publish</FormLabel>
                  <FormDescription>Will this image be published.</FormDescription>
                </div>
                <FormControl>
                  <Switch checked={field.value} onCheckedChange={field.onChange} />
                </FormControl>
              </FormItem>
            )}
          />
          <FormField
            control={form.control}
            name="setAsHeader"
            render={({ field }) => (
              <FormItem className="flex items-center justify-between rounded-lg border p-4">
                <div className="space-y-0.5">
                  <FormLabel>Set as header image</FormLabel>
                  <FormDescription>Will be the main banner image. Choose a fitting one.</FormDescription>
                </div>
                <FormControl>
                  <Switch checked={field.value} onCheckedChange={field.onChange} />
                </FormControl>
              </FormItem>
            )}
          />
          {/* Read only */}
          <FormField
            control={form.control}
            name="fileKey"
            render={({ field }) => (
              <FormItem>
                <FormLabel>Image Key</FormLabel>
                <FormControl><Input {...field} disabled /></FormControl>
                <FormMessage />
              </FormItem>
            )}
          />
          <FormField
            control={form.control}
            name="originalFile"
            render={({ field }) => (
              <FormItem>
                <FormLabel>Original file</FormLabel>
                <FormControl><Input {...field} disabled /></FormControl>
                <FormMessage />
              </FormItem>
            )}
          />
          <FormField
            control={form.control}
            name="fileType"
            render={({ field }) => (
              <FormItem>
                <FormLabel>Filetype</FormLabel>
                <FormControl><Input {...field} disabled /></FormControl>
                <FormMessage />
              </FormItem>
            )}
          />
          <FormField
            control={form.control}
            name="fileSize"
            render={({ field }) => (
              <FormItem>
                <FormLabel>FileSize</FormLabel>
                <FormControl><Input type="number" {...field} disabled /></FormControl>
                <FormMessage />
              </FormItem>
            )}
          />
          <div className="flex flex-col gap-4">
            <Button type="submit">Submit</Button>
            <Button type="reset" variant="secondary" onClick={() => router.back()}>Cancel</Button>
          </div>
        </form>
      </Form>
    </div >
  );
}