import { CONSTITUTIONAL_CHANGE_TYPES } from '../config/constants.js';

const ALL_CHANGE_TYPES = Object.values(CONSTITUTIONAL_CHANGE_TYPES) as string[];

export function isRegisteredConstitutionalChangeType(value: string): boolean {
  return ALL_CHANGE_TYPES.includes(value);
}

/**
 * Map UI / legacy profile labels to a value that exists on `constitutional_changes.changeType` ENUM.
 */
export function normalizeToConstitutionalChangeType(raw: string | null | undefined): string | null {
  const s = String(raw || '').trim();
  if (!s) return null;
  if (isRegisteredConstitutionalChangeType(s)) return s;
  const compact = s
    .toLowerCase()
    .replace(/\./g, '')
    .replace(/\s+/g, ' ')
    .trim();
  if (
    (compact.includes('private') && (compact.includes('ltd') || compact.includes('limited'))) ||
    compact === 'pvt ltd' ||
    compact === 'pvtltd'
  ) {
    return CONSTITUTIONAL_CHANGE_TYPES.PRIVATE_LIMITED;
  }
  if (compact.includes('llp') && compact.includes('conversion')) {
    return CONSTITUTIONAL_CHANGE_TYPES.LLP_CONVERSION;
  }
  if (compact.includes('llp')) {
    return CONSTITUTIONAL_CHANGE_TYPES.LLP;
  }
  if (compact.includes('partnership') && compact.includes('change')) {
    return CONSTITUTIONAL_CHANGE_TYPES.PARTNERSHIP_CHANGE;
  }
  if (compact.includes('partnership')) {
    return CONSTITUTIONAL_CHANGE_TYPES.PARTNERSHIP;
  }
  if (compact.includes('proprietorship') || compact === 'sole proprietorship') {
    return CONSTITUTIONAL_CHANGE_TYPES.PROPRIETORSHIP;
  }
  if (compact.includes('director')) {
    return CONSTITUTIONAL_CHANGE_TYPES.DIRECTOR_CHANGE;
  }
  if (compact.includes('ownership') && compact.includes('transfer')) {
    return CONSTITUTIONAL_CHANGE_TYPES.OWNERSHIP_TRANSFER;
  }
  if (compact.includes('company') && compact.includes('formation')) {
    return CONSTITUTIONAL_CHANGE_TYPES.COMPANY_FORMATION;
  }
  const exact = ALL_CHANGE_TYPES.find((v) => v.toLowerCase() === s.toLowerCase());
  return exact || null;
}

/**
 * Maps `constitutional_changes.changeType` → `dealers.constitutionType` when the workflow completes.
 * Returns `null` when the change type does not represent a single target legal structure (skip master update).
 */
export function mapConstitutionalChangeTypeToDealerProfile(changeType: string): string | null {
  const t = String(changeType || '').trim();
  if (!t) return null;

  if (t === CONSTITUTIONAL_CHANGE_TYPES.LLP_CONVERSION) return CONSTITUTIONAL_CHANGE_TYPES.LLP;

  const structureTargets = [
    CONSTITUTIONAL_CHANGE_TYPES.PROPRIETORSHIP,
    CONSTITUTIONAL_CHANGE_TYPES.PARTNERSHIP,
    CONSTITUTIONAL_CHANGE_TYPES.LLP,
    CONSTITUTIONAL_CHANGE_TYPES.PRIVATE_LIMITED
  ];
  if (structureTargets.includes(t as (typeof structureTargets)[number])) return t;

  const skipAutoUpdate = [
    CONSTITUTIONAL_CHANGE_TYPES.PARTNERSHIP_CHANGE,
    CONSTITUTIONAL_CHANGE_TYPES.DIRECTOR_CHANGE,
    CONSTITUTIONAL_CHANGE_TYPES.OWNERSHIP_TRANSFER,
    CONSTITUTIONAL_CHANGE_TYPES.COMPANY_FORMATION
  ];
  if (skipAutoUpdate.includes(t as (typeof skipAutoUpdate)[number])) return null;

  return null;
}
