import Logger from 'src/application/logging/logger';
import { GetMerchantColorsQueryData } from 'src/application/models/graphql';

const REQUIRED_COLOR_FIELDS = [
  'accent_primary',
  'accent_secondary',
  'background_dark',
  'background_light',
  'background_primary',
  'background_saturated',
  'background_secondary',
  'background_secondary_dark',
  'background_tertiary',
  'border_dark',
  'border_light',
  'border_medium',
  'border_outline',
  'text_accent',
  'text_light',
  'text_link',
  'text_primary',
  'text_secondary',
] as const;

const logger = new Logger('isGraphQLColorsConfig');

export const isGraphQLColorsConfig = (data: unknown): data is GetMerchantColorsQueryData => {
  if (typeof data !== 'object' || data === null) {
    logger.logError('Invalid graphql response for colors config', undefined, { data });
    return false;
  }

  // Check each required field
  const missingFields = REQUIRED_COLOR_FIELDS.filter(
    field => !(field in data) || typeof (data as Record<string, unknown>)[field] !== 'string',
  );

  if (missingFields.length === REQUIRED_COLOR_FIELDS.length) {
    logger.logError('All color fields are missing or null', undefined);
    return false;
  }

  if (missingFields.length > 0) {
    logger.logError('Missing or invalid color fields', undefined, {
      data,
      missingFields,
    });
    return false;
  }

  return true;
};
