import logger from '../../../libs/logger';
import { Customer, TEventExpanded } from '../../../store/models';

export async function handleCustomerEvent(event: TEventExpanded) {
  const localId = event.data.object.metadata?.id;
  if (!localId) {
    logger.warn('local customer id not found in strip event', { id: event.id, type: event.type });
    return;
  }

  const customer = await Customer.findByPk(localId);
  if (!customer) {
    logger.warn('local customer not found', { localId });
    return;
  }

  logger.info('received customer event', { id: event.id, type: event.type, localId });

  if (event.type === 'customer.updated') {
    if (event.data.object.balance !== customer.balance) {
      await customer.update({ balance: event.data.object.balance });
    }
  }
}
