import Logger from 'src/application/logging/logger';
import { ResponseProductAttributes } from '@spiffy-ai/commerce-api-client';
import { hasPropertyOfType } from '../utils';

const logger = new Logger('isApiProductResponseAttributes');

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

  if (!hasPropertyOfType(attributes, 'description', 'string', true)) {
    logger.logError('isApiProductResponseAttributes: description is not a string', undefined, {
      attributes,
    });
    return false;
  }

  if (!hasPropertyOfType(attributes, 'image_url', 'string', true)) {
    logger.logError('isApiProductResponseAttributes: image_url is not a string', undefined, {
      attributes,
    });
    return false;
  }

  if (!hasPropertyOfType(attributes, 'title', 'string')) {
    logger.logError('isApiProductResponseAttributes: title is not a string', undefined, {
      attributes,
    });
    return false;
  }

  if (!hasPropertyOfType(attributes, 'url', 'string')) {
    logger.logError('isApiProductResponseAttributes: url is not a string', undefined, {
      attributes,
    });
    return false;
  }

  if (!hasPropertyOfType(attributes, 'original_price', 'number', true)) {
    logger.logError('isApiProductResponseAttributes: original_price is not a number', undefined, {
      attributes,
    });
    return false;
  }

  if (!hasPropertyOfType(attributes, 'sale_price', 'number', true)) {
    logger.logError('isApiProductResponseAttributes: sale_price is not a number', undefined, {
      attributes,
    });
    return false;
  }

  if (!hasPropertyOfType(attributes, 'average_rating', 'number', true)) {
    logger.logError('isApiProductResponseAttributes: average_rating is not a number', undefined, {
      attributes,
    });
    return false;
  }

  if (!hasPropertyOfType(attributes, 'number_reviews', 'number', true)) {
    logger.logError('isApiProductResponseAttributes: number_reviews is not a number', undefined, {
      attributes,
    });
    return false;
  }

  if (!hasPropertyOfType(attributes, 'metadata', 'array', true)) {
    logger.logError('isApiProductResponseAttributes: metadata is not an array', undefined, {
      attributes,
    });
    return false;
  }

  return true;
};
