import type { RuntimeVisibilityCondition } from '~/lib/runtime/types';

export function evaluateVisibility(
  conditions: RuntimeVisibilityCondition[] | undefined,
  values: Record<string, unknown>,
) {
  if (!conditions || conditions.length === 0) return true;
  return conditions.every((condition) => {
    const value = values[condition.field];
    if (typeof condition.equals === 'string') return String(value || '') === condition.equals;
    if (Array.isArray(condition.in)) return condition.in.includes(String(value || ''));
    return true;
  });
}

export function normalizeRoleKey(value: string | null | undefined) {
  return String(value || '')
    .trim()
    .toUpperCase()
    .replace(/[-\s]+/g, '_');
}

export function schemaIdFromInput(roleKey: string, profession: string) {
  const normalizedRole = normalizeRoleKey(roleKey);
  const normalizedProfession = String(profession || '')
    .trim()
    .toLowerCase()
    .replace(/[-\s]+/g, '_')
    .replace(/[^a-z_]/g, '');

  if (normalizedRole === 'CUSTOMER') return 'customer_onboarding_v1';
  if (normalizedRole === 'COMPANY') return 'company_onboarding_v1';
  if (normalizedRole === 'JOB_SEEKER' || normalizedRole === 'JOBSEEKER') return 'jobseeker_onboarding_v1';
  if (normalizedRole === 'PHOTOGRAPHER') return 'photographer_onboarding_v1';
  if (normalizedRole === 'MAKEUP_ARTIST') return 'makeup_artist_onboarding_v1';
  if (normalizedRole === 'TUTOR') return 'tutor_onboarding_v1';
  if (normalizedRole === 'DEVELOPER') return 'developer_onboarding_v1';
  if (normalizedRole === 'VIDEO_EDITOR') return 'video_editor_onboarding_v1';
  if (normalizedRole === 'GRAPHIC_DESIGNER') return 'graphic_designer_onboarding_v1';
  if (normalizedRole === 'SOCIAL_MEDIA_MANAGER') return 'social_media_manager_onboarding_v1';
  if (normalizedRole === 'FITNESS_TRAINER') return 'fitness_trainer_onboarding_v1';
  if (normalizedRole === 'CATERING_SERVICES') return 'catering_services_onboarding_v1';
  if (normalizedRole === 'PROFESSIONAL' && normalizedProfession) return `${normalizedProfession}_onboarding_v1`;
  if (normalizedRole === 'PROFESSIONAL') return 'professional_onboarding_v1';
  return '';
}
