import Logger from 'src/application/logging/logger';
import { OrganizationConfig } from '@spiffy-ai/commerce-api-client';

// the "config" property is a generic object in the commerce api spec, these interfaces
// are used to validate the structure of that object and to transform the api response
// into a typed object for the frontend. Do not use these interfaces for anything else.
interface ApiOrgAnalyticsConfigAmplitude {
  session_replay_enabled: boolean;
  session_replay_sample_rate: number;
  tracking_enabled: boolean;
}

interface ApiOrgAnalyticsConfigCustomerService {
  provider: string;
}

interface ApiOrgAnalyticsConfigGoogleAnalytics {
  measurement_id: string;
}

interface ApiOrgAnalyticsConfig {
  amplitude?: ApiOrgAnalyticsConfigAmplitude;
  customer_service?: ApiOrgAnalyticsConfigCustomerService;
  google_analytics?: ApiOrgAnalyticsConfigGoogleAnalytics;
}

export interface ApiOrganizationConfig extends OrganizationConfig {
  config: ApiOrgAnalyticsConfig;
}

const logger = new Logger('isApiOrganizationConfig');

const isApiOrgAnalyticsConfigAmplitude = (
  data: unknown,
): data is ApiOrgAnalyticsConfigAmplitude => {
  if (data == null || typeof data !== 'object') {
    logger.logDebug('isApiOrgAnalyticsConfigAmplitude: data is not an object', data);
    return false;
  }

  if (!('session_replay_enabled' in data) || typeof data.session_replay_enabled !== 'boolean') {
    logger.logDebug(
      'isApiOrgAnalyticsConfigAmplitude: session_replay_enabled is not a boolean',
      data,
    );
    return false;
  }

  if (
    !('session_replay_sample_rate' in data) ||
    typeof data.session_replay_sample_rate !== 'number'
  ) {
    logger.logDebug(
      'isApiOrgAnalyticsConfigAmplitude: session_replay_sample_rate is not a number',
      data,
    );
    return false;
  }

  if (!('tracking_enabled' in data) || typeof data.tracking_enabled !== 'boolean') {
    logger.logDebug('isApiOrgAnalyticsConfigAmplitude: tracking_enabled is not a boolean', data);
    return false;
  }

  return true;
};

const isApiOrgAnalyticsConfigCustomerService = (
  data: unknown,
): data is ApiOrgAnalyticsConfigCustomerService => {
  if (data == null || typeof data !== 'object') {
    logger.logDebug('isApiOrgAnalyticsConfigCustomerService: data is not an object', data);
    return false;
  }

  if (!('provider' in data) || typeof data.provider !== 'string') {
    logger.logDebug('isApiOrgAnalyticsConfigCustomerService: provider is not a string', data);
    return false;
  }

  return true;
};

const isApiOrgAnalyticsConfigGoogleAnalytics = (
  data: unknown,
): data is ApiOrgAnalyticsConfigGoogleAnalytics => {
  if (data == null || typeof data !== 'object') {
    logger.logDebug('isApiOrgAnalyticsConfigGoogleAnalytics: data is not an object', data);
    return false;
  }

  if (!('measurement_id' in data) || typeof data.measurement_id !== 'string') {
    logger.logDebug('isApiOrgAnalyticsConfigGoogleAnalytics: measurement_id is not a string', data);
    return false;
  }

  return true;
};

/**
 *
 * @param data
 * @returns
 */
const isApiOrgAnalyticsConfig = (data: unknown): data is ApiOrgAnalyticsConfig => {
  if (data == null || typeof data !== 'object') {
    logger.logDebug('isOrgAnalyticsConfig: data is not an object', data);
    return false;
  }

  if ('amplitude' in data && !isApiOrgAnalyticsConfigAmplitude(data.amplitude)) {
    logger.logDebug('isOrgAnalyticsConfig: amplitude is not an object', data);
    return false;
  }

  if (
    'customer_service' in data &&
    !isApiOrgAnalyticsConfigCustomerService(data.customer_service)
  ) {
    logger.logDebug('isOrgAnalyticsConfig: customer_service is not an object', data);
    return false;
  }

  if (
    'google_analytics' in data &&
    !isApiOrgAnalyticsConfigGoogleAnalytics(data.google_analytics)
  ) {
    logger.logDebug('isOrgAnalyticsConfig: google_analytics is not an object', data);
    return false;
  }

  return true;
};

/**
 *
 * @param data
 * @returns
 */
export const isApiOrganizationConfig = (data: unknown): data is ApiOrganizationConfig => {
  if (data == null || typeof data !== 'object') {
    logger.logDebug('isOrgConfig: data is not an object', data);
    return false;
  }

  if (!('created_at' in data)) {
    logger.logDebug('isOrgConfig: created_at is not defined', data);
    return false;
  }

  if (!('namespace' in data) || typeof data.namespace !== 'string') {
    logger.logDebug('isOrgConfig: namespace is not a string', data);
    return false;
  }

  if (!('is_latest' in data) || typeof data.is_latest !== 'boolean') {
    logger.logDebug('isOrgConfig: is_latest is not a boolean', data);
    return false;
  }

  if (!('version' in data) || typeof data.version !== 'string') {
    logger.logDebug('isOrgConfig: version is not a string', data);
    return false;
  }

  if (!('updated_at' in data)) {
    logger.logDebug('isOrgConfig: updated_at is not defined', data);
    return false;
  }

  if (
    !('config' in data) ||
    typeof data.config !== 'object' ||
    !isApiOrgAnalyticsConfig(data.config)
  ) {
    logger.logDebug('isOrgConfig: config is not an object', data);
    return false;
  }

  if ('organization_id' in data && typeof data.organization_id !== 'string') {
    logger.logDebug('isOrgConfig: organization_id is not a string', data);
    return false;
  }

  if ('id' in data && typeof data.id !== 'string') {
    logger.logDebug('isOrgConfig: id is not a string', data);
    return false;
  }

  return true;
};
