import { OrganizationConfig } from 'src/application/models/api/organizationConfig';
import { isApiOrganizationConfig } from 'src/application/models/guards/api/isApiOrganizationConfig';

export const validateOrganizationConfig = (data: unknown): OrganizationConfig | undefined => {
  if (!isApiOrganizationConfig(data)) {
    return undefined;
  }

  return {
    id: data.id,
    createdAt: data.created_at.toISOString(),
    namespace: data.namespace,
    organizationId: data.organization_id,
    isLatest: data.is_latest,
    version: data.version,
    updatedAt: data.updated_at.toISOString(),
    config: {
      amplitude: data.config.amplitude
        ? {
            sessionReplayEnabled: data.config.amplitude.session_replay_enabled,
            sessionReplaySampleRate: data.config.amplitude.session_replay_sample_rate,
            trackingEnabled: data.config.amplitude.tracking_enabled,
          }
        : undefined,
      customerService: data.config.customer_service
        ? {
            provider: data.config.customer_service.provider,
          }
        : undefined,
      googleAnalytics: data.config.google_analytics
        ? {
            measurementId: data.config.google_analytics.measurement_id,
          }
        : undefined,
    },
  };
};
