import Logger from 'src/application/logging/logger';
import { FormSubmittedAttributes, FormType } from '@spiffy-ai/commerce-api-client';
import { hasPropertyOfType } from 'src/application/models/guards/utils';

const logger = new Logger('isApiFormSubmittedResponseAttributes');

export const isApiFormSubmittedResponseAttributes = (
  data: unknown,
): data is FormSubmittedAttributes => {
  if (data == null || typeof data !== 'object') {
    logger.logError('isApiFormSubmittedResponseAttributes', 'data is null or not an object', data);
    return false;
  }

  if (!hasPropertyOfType(data, 'filled_schema', 'object')) {
    logger.logError('isApiFormSubmittedResponseAttributes', 'filled_schema is not an object', data);
    return false;
  }

  if (!hasPropertyOfType(data, 'form_response_id', 'string')) {
    logger.logError(
      'isApiFormSubmittedResponseAttributes',
      'form_response_id is not a string',
      data,
    );
    return false;
  }

  if (!('form_type' in data) || !Object.values(FormType).includes(data.form_type as FormType)) {
    logger.logError(
      'isApiFormSubmittedResponseAttributes',
      'form_type is not a valid form type enum',
      data,
    );
    return false;
  }

  return true;
};
