type OntologyLeaf = {
  id: string;
  keywords: string[];
};

type OntologyAxis = {
  id: string;
  leaves: OntologyLeaf[];
};

export const placeOntology: OntologyAxis[] = [
  {
    id: 'energy',
    leaves: [
      { id: 'calm', keywords: ['quiet', 'calm', 'slow', 'soft', 'peaceful'] },
      { id: 'dynamic', keywords: ['busy', 'loud', 'alive', 'fast', 'crowd'] },
    ],
  },
  {
    id: 'privacy',
    leaves: [
      { id: 'intimate', keywords: ['private', 'cozy', 'small', 'hidden'] },
      { id: 'open', keywords: ['open', 'public', 'visible', 'spacious'] },
    ],
  },
  {
    id: 'sociality',
    leaves: [
      { id: 'solo', keywords: ['alone', 'focus', 'work', 'read'] },
      { id: 'group', keywords: ['friends', 'date', 'talk', 'party'] },
    ],
  },
  {
    id: 'function',
    leaves: [
      { id: 'reset', keywords: ['rest', 'breathe', 'reset', 'recover'] },
      { id: 'impress', keywords: ['impress', 'beautiful', 'special', 'wow'] },
      { id: 'transit', keywords: ['quick', 'nearby', 'stop', 'between'] },
    ],
  },
  {
    id: 'aesthetic',
    leaves: [
      { id: 'clean', keywords: ['clean', 'minimal', 'simple', 'bright'] },
      { id: 'expressive', keywords: ['design', 'art', 'unusual', 'texture'] },
    ],
  },
];

export type PlaceAnalysis = {
  placeName: string;
  tags: string[];
  signals: Array<{
    axis: string;
    leaf: string;
    matches: string[];
  }>;
};

export function buildPlaceAnalysis(input: {
  placeName: string;
  transcript: string;
}): PlaceAnalysis {
  const normalized = input.transcript.toLowerCase();
  const signals = placeOntology.flatMap((axis) =>
    axis.leaves
      .map((leaf) => ({
        axis: axis.id,
        leaf: leaf.id,
        matches: leaf.keywords.filter((keyword) => normalized.includes(keyword)),
      }))
      .filter((signal) => signal.matches.length > 0),
  );

  return {
    placeName: input.placeName,
    tags: signals.map((signal) => `${signal.axis}:${signal.leaf}`),
    signals,
  };
}
