import { type FieldErrors } from 'react-hook-form';

import { type FileCardType } from '@design-system/FileCard/types';
import { getAttachmentFromFileAsset } from '@utils/attachments';
import i18next from 'i18next';
import { type FileAsset } from 'types/attachments';
import { type ZodTypeAny } from 'zod';

import {
  type DynamicFormComponent,
  type DynamicFormComponentValue,
  type DynamicFormValues,
  type ErrorTextsDefinition,
} from '../types';
import { getComponentSchema } from '../utils/getComponentSchema';
import { getParseAnswer } from '../utils/getParseAnswer';

export const getFieldName = (section: string, field: string) =>
  `${section}-${field}`;

export const hasRequiredFieldError = (errors: FieldErrors<DynamicFormValues>) =>
  Object.values(errors).some(
    error =>
      error?.message ===
      i18next.t('service_management.required_field', {
        ns: 'material_hu_only',
      }),
  );

export const scrollToFormTop = () => {
  const formSection = document.getElementById('form-section');
  if (!formSection) return;
  formSection.scrollIntoView({ behavior: 'smooth', block: 'start' });
};

export const getInitialFiles = (initialFiles: FileAsset[]) => {
  if (!initialFiles?.length) return [];

  const formattedInitialValues: FileCardType[] = initialFiles?.map(file => ({
    status: 'success',
    attachment: {
      ...getAttachmentFromFileAsset(file),
      fileAssetReferenceId: file.id,
    },
  }));

  return formattedInitialValues;
};

export const buildFormSchemaAndValues = ({
  sectionId,
  components,
  errorTexts,
}: {
  sectionId: string;
  components: DynamicFormComponent[];
  errorTexts: ErrorTextsDefinition;
}) => {
  // Initialize accumulators
  const formSchema: Record<string, ZodTypeAny> = {};
  const values: DynamicFormValues = {};

  // Process each component
  for (const component of components) {
    // Get the name of the component
    const name = getFieldName(sectionId, component.nameId);

    // Get the component schema
    const componentSchema = getComponentSchema(component, errorTexts);

    // Assign the validation schema for the component
    if (componentSchema) {
      formSchema[name] = componentSchema;
    }

    // Assign the value for the component
    values[name] = getParseAnswer(component);
  }

  return { schema: formSchema, values };
};

export const isFileAsset = (
  value: DynamicFormComponentValue,
): value is FileAsset => {
  if (!value || typeof value !== 'object') return false;

  const obj = value as Record<string, unknown>;

  return (
    typeof obj.id === 'number' &&
    typeof obj.url === 'string' &&
    typeof obj.key === 'string' &&
    typeof obj.name === 'string' &&
    typeof obj.contentType === 'string'
  );
};

export const transformIntegerInput = (value: string) => {
  if (!value) return value;

  let cleaned = value.replace(/[^0-9.,]/g, '');
  const firstSeparatorMatch = cleaned.match(/[.,]/);

  if (firstSeparatorMatch) {
    const separatorIndex = cleaned.indexOf(firstSeparatorMatch[0]);
    const beforeSeparator = cleaned.substring(0, separatorIndex);
    const afterSeparator = cleaned
      .substring(separatorIndex + 1)
      .replace(/[.,]/g, '');
    cleaned = beforeSeparator + firstSeparatorMatch[0] + afterSeparator;
  }
  return cleaned;
};
