import * as Yup from 'yup';
import { buildFormData } from '../../api/helper/buildFormData';
import { collectionObjectToArray, objectToKeyValueArray } from '../../utils/ConvertObjToArr';

export interface Param {
  key: string;
  value: string;
}
interface Category {
  id?: number | null;
  name: string | null;
}

export interface Collection {
  name: string;
  collectionParams: {
    key: string;
    value: string;
  };
}

interface Attachment {
  id: number | null;
  attachment: string;
}


interface FormUtilCommon {
  name: string;
  description: string;
  model: string;
  file?: string;
  category?: Category;
  params?: Param[];
  collections?: Collection[];
  attachment?: Attachment[]; // Add attachments here
}
interface ObjectToEdit extends FormUtilCommon {
  id?: number | null;

}

export interface InitialValues extends ObjectToEdit {

}

interface ValidateSchema extends FormUtilCommon { }

export const getInitialValues = (objectToEdit: ObjectToEdit | null = null): any => {

  const params = objectToEdit?.params ? objectToKeyValueArray(objectToEdit.params) : [{ key: '', value: '' }];
  const collection = objectToEdit?.collections ? objectToKeyValueArray(objectToEdit.collections) : [{ name: '', collectionParams: [{ key: '', value: '' }]} ];
  // { name: '', collectionParams: [{ key: '', value: '' }] };

  const attachments = objectToEdit?.attachment ? objectToEdit.attachment.map(attachment => {
    return {
      url: attachment.attachment ?? ""
    }
  }) : []

  return {
    id: objectToEdit?.id ?? null,
    category_id: objectToEdit?.category?.id ?? null,
    name: objectToEdit?.name ?? '',
    description: objectToEdit?.description ?? '',
    model: objectToEdit?.model ?? '',
    file: objectToEdit?.file ?? '',
    params: params,
    collections: collection,
    attachments: attachments, // Set the attachments here
  };
}

export const getValidationSchema = (editMode: boolean = false): any => {
  return Yup.object().shape({
    name: Yup.string().required('required'),
    category_id: Yup.number().nullable().required('required'),
    description: Yup.string().required('required'),
    model: Yup.string().required('required'),
    file: Yup.string().required('required'),
    params: Yup.array().of(
      Yup.object().shape({
        key: Yup.string().required('Key is required'),
        value: Yup.string().required('Value is required'),
      })
    ).default([]),
    collections: Yup.array().of(
      Yup.object().shape({
        name: Yup.string().required('Collection Name is required'),
        collectionParams: Yup.array().of(
          Yup.object().shape({
            key: Yup.string().required('Key is required'),
            value: Yup.string().required('Value is required'),
          })
        ).required('At least one parameter is required').min(1, 'At least one parameter is required'), // Ensure at least one parameter
      })
    ).optional(), // Allow collections to be optional
  });
};

export const getDataToSend = (values: any): any => {
  const data = { ...values };
  console.log(values);
  
  if (data?.id) {
    data["_method"] = "PUT"
  }

  // Ensure params is an array
  if (!Array.isArray(data.params)) {
    data.params = [];
  }

  if (typeof data['file'] == 'string') delete data['file']
  // Return the data object directly

  if (!Array.isArray(data.collections)) {
    data.collections = [];
  }

  return data;
};