'use client';

import { zodResolver } from '@hookform/resolvers/zod';
import { useMemo } from 'react';
import { useForm } from 'react-hook-form';
import * as z from 'zod';

import { FormDialog } from '@/app/components/ui/dialog/form-dialog';
import { Input } from '@/app/components/ui/forms/input';
import { Select } from '@/app/components/ui/forms/select';
import { toast } from '@/app/hooks/use-toast';
import { useT } from '@/lib/i18n/client';

import { useCreateWebsite } from '../hooks/mutations';

type FormData = {
  domain: string;
  scanInterval: string;
};

interface AddWebsiteDialogProps {
  isOpen: boolean;
  onClose: () => void;
  organizationId: string;
}

export function AddWebsiteDialog({
  isOpen,
  onClose,
  organizationId,
}: AddWebsiteDialogProps) {
  const { t: tWebsites } = useT('websites');
  const { mutate: createWebsite, isPending: isLoading } = useCreateWebsite();

  const formSchema = useMemo(
    () =>
      z.object({
        domain: z
          .string()
          .min(1, tWebsites('validation.domainRequired'))
          .refine(
            (val) => {
              try {
                const url =
                  val.startsWith('http://') || val.startsWith('https://')
                    ? val
                    : `https://${val}`;
                const parsed = new URL(url);
                return !!parsed.hostname && parsed.hostname.includes('.');
              } catch {
                return false;
              }
            },
            { message: tWebsites('validation.validDomain') },
          ),
        scanInterval: z
          .string()
          .min(1, tWebsites('validation.scanIntervalRequired')),
      }),
    [tWebsites],
  );

  const SCAN_INTERVALS = [
    { value: '60m', label: tWebsites('scanIntervals.1hour') },
    { value: '6h', label: tWebsites('scanIntervals.6hours') },
    { value: '12h', label: tWebsites('scanIntervals.12hours') },
    { value: '1d', label: tWebsites('scanIntervals.1day') },
    { value: '5d', label: tWebsites('scanIntervals.5days') },
    { value: '7d', label: tWebsites('scanIntervals.7days') },
    { value: '30d', label: tWebsites('scanIntervals.30days') },
  ];

  const {
    register,
    handleSubmit,
    formState: { errors },
    reset,
    setValue,
    watch,
  } = useForm<FormData>({
    resolver: zodResolver(formSchema),
    defaultValues: {
      domain: '',
      scanInterval: '6h',
    },
  });

  const scanInterval = watch('scanInterval');

  const onSubmit = (data: FormData) => {
    createWebsite(
      {
        organizationId,
        domain: data.domain,
        scanInterval: data.scanInterval,
      },
      {
        onSuccess: () => {
          toast({
            title: tWebsites('toast.addSuccess'),
            variant: 'success',
          });
          reset();
          onClose();
        },
        onError: (error) => {
          console.error('Failed to add website:', error);
          const isDuplicate =
            error instanceof Error && error.message.includes('already exists');
          toast({
            title: isDuplicate
              ? tWebsites('toast.addErrorDuplicate')
              : tWebsites('toast.addError'),
            variant: 'destructive',
          });
        },
      },
    );
  };

  const handleClose = () => {
    reset();
    onClose();
  };

  return (
    <FormDialog
      open={isOpen}
      onOpenChange={(open) => !open && handleClose()}
      title={tWebsites('addWebsite')}
      submittingText={tWebsites('adding')}
      isSubmitting={isLoading}
      onSubmit={handleSubmit(onSubmit)}
    >
      <Input
        id="domain"
        type="text"
        label={tWebsites('domain')}
        placeholder={tWebsites('urlPlaceholder')}
        {...register('domain')}
        disabled={isLoading}
        errorMessage={errors.domain?.message}
      />

      <Select
        value={scanInterval}
        onValueChange={(value) =>
          setValue('scanInterval', value, { shouldDirty: true })
        }
        disabled={isLoading}
        id="scanInterval"
        label={tWebsites('scanInterval')}
        error={!!errors.scanInterval}
        placeholder={tWebsites('scanIntervalPlaceholder')}
        options={SCAN_INTERVALS}
      />
    </FormDialog>
  );
}
