import {z} from 'zod';
import {HuDataMember} from '@modules/chats/interfaces';
import {sentryLogs} from '@shared/utils';

import {getValidationResult, requiredInt, requiredString} from './shared';

export const userDBRequiredFieldsSchema = z
  .object({
    id: requiredInt,
    first_name: requiredString,
    last_name: requiredString,
  })
  .passthrough();

const logUserDBValidation = ({
  user,
  source,
  issues,
}: {
  user: HuDataMember;
  source: string;
  issues: string[];
}) => {
  sentryLogs.error({
    message: '[Events Validation] User data has invalid DB-required fields',
    module: 'CHATS',
    component: source,
    data: {
      source,
      userId: user.id,
      firstName: user.first_name,
      lastName: user.last_name,
      issues,
    },
  });
};

export const logUserDBValidationIssues = (
  user: HuDataMember,
  source: string,
) => {
  const validation = getValidationResult(userDBRequiredFieldsSchema, user);
  if (!validation.success) {
    logUserDBValidation({user, source, issues: validation.issues});
  }
};
