<script setup lang="ts">
import { Button } from '@/components/ui/button'
import {
  Dialog,
  DialogClose,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle
} from '@/components/ui/dialog'
import { FormField, FormItem, FormLabel, FormMessage } from '@/components/ui/form'
import { Input } from '@/components/ui/input'
import { useToast } from '@/components/ui/toast/use-toast'

import { useMutation, useQueryClient } from '@tanstack/vue-query'
import { defineRule, useForm } from 'vee-validate'
import { onMounted, ref } from 'vue'

import { useAPI } from '@/api'
import type { Link, Ticket } from '@/client/models'
import { handleError } from '@/lib/utils'

const api = useAPI()

const queryClient = useQueryClient()
const { toast } = useToast()

const props = defineProps<{
  ticket: Ticket
}>()

const isOpen = defineModel<boolean>()

const addLinkMutation = useMutation({
  mutationFn: (values: any): Promise<Link> =>
    api.createLink({
      newLink: {
        ticket: props.ticket.id,
        name: values.name,
        url: values.url
      }
    }),
  onSuccess: () => {
    queryClient.invalidateQueries({ queryKey: ['links', props.ticket.id] })
    toast({
      title: 'Link added',
      description: 'The link has been added successfully'
    })
    isOpen.value = false
  },
  onError: handleError('Failed to add link')
})

defineRule('required', (value: string) => {
  if (!value || !value.length) {
    return 'This field is required'
  }

  return true
})

const { handleSubmit, validate } = useForm({
  validationSchema: {
    name: 'required',
    url: 'required'
  }
})

const onSubmit = handleSubmit((values) => addLinkMutation.mutate(values))

const submitDisabled = ref(true)
onMounted(() => change())

const change = () => validate({ mode: 'silent' }).then((res) => (submitDisabled.value = !res.valid))
</script>

<template>
  <Dialog v-model:open="isOpen">
    <DialogContent>
      <DialogHeader>
        <DialogTitle>New Link</DialogTitle>
        <DialogDescription> Add a new link to this ticket</DialogDescription>
      </DialogHeader>

      <form @submit="onSubmit" @change="change">
        <FormField name="name" v-slot="{ componentField }">
          <FormItem>
            <FormLabel for="name" class="text-left"> Name</FormLabel>
            <Input id="name" v-bind="componentField" />
            <FormMessage />
          </FormItem>
        </FormField>

        <FormField name="url" v-slot="{ componentField }" class="mt-2">
          <FormItem>
            <FormLabel for="url" class="text-left"> URL</FormLabel>
            <Input id="url" v-bind="componentField" />
            <FormMessage />
          </FormItem>
        </FormField>

        <DialogFooter class="mt-2 sm:justify-start">
          <Button
            :title="submitDisabled ? 'Please fill out all required fields' : undefined"
            :disabled="submitDisabled"
            type="submit"
          >
            Save
          </Button>
          <DialogClose as-child>
            <Button type="button" variant="secondary"> Cancel</Button>
          </DialogClose>
        </DialogFooter>
      </form>
    </DialogContent>
  </Dialog>
</template>
