import type { InteractiveContentBlockType, InteractiveContentContext } from './types';

export type InteractiveContentCapabilityProfile = {
  id: 'learn' | 'cms' | 'interactive-doc';
  contexts: readonly InteractiveContentContext[];
  allowedBlocks: ReadonlySet<InteractiveContentBlockType>;
  maxTopLevelBlocks: number;
};

const ALL_BLOCKS: readonly InteractiveContentBlockType[] = [
  'paragraph','header','list','checklist','quote','delimiter','callout','table','tabs','accordion',
  'image','gallery','video','audio','file','code','terminal','terminalTask','codeExercise','checkpoint','solution','legacyImport',
];

const INTERACTIVE_DOC_BLOCKS: readonly InteractiveContentBlockType[] = [
  'paragraph','header','list','checklist','quote','delimiter','callout','table','tabs','accordion',
  'image','gallery','video','audio','file','code',
];

export const LEARN_BLOCK_PROFILE: InteractiveContentCapabilityProfile = Object.freeze({
  id:'learn', contexts:['learn'], allowedBlocks:new Set(ALL_BLOCKS), maxTopLevelBlocks:250,
});

export const CMS_BLOCK_PROFILE: InteractiveContentCapabilityProfile = Object.freeze({
  id:'cms', contexts:['blog','docs'], allowedBlocks:new Set(ALL_BLOCKS), maxTopLevelBlocks:500,
});

export const INTERACTIVE_DOC_BLOCK_PROFILE: InteractiveContentCapabilityProfile = Object.freeze({
  id:'interactive-doc', contexts:['interactive-doc'], allowedBlocks:new Set(INTERACTIVE_DOC_BLOCKS), maxTopLevelBlocks:1500,
});

export function interactiveContentProfileForContext(context: InteractiveContentContext): InteractiveContentCapabilityProfile {
  if (context === 'learn') return LEARN_BLOCK_PROFILE;
  if (context === 'interactive-doc') return INTERACTIVE_DOC_BLOCK_PROFILE;
  return CMS_BLOCK_PROFILE;
}

export function validateInteractiveContentBlockTypes(
  blockTypes: readonly string[],
  profile: InteractiveContentCapabilityProfile,
): { ok: true } | { ok: false; unsupported: string[] } {
  const unsupported = [...new Set(blockTypes.filter((type) => type !== 'paragraph' && !profile.allowedBlocks.has(type as InteractiveContentBlockType)))];
  return unsupported.length ? { ok:false, unsupported } : { ok:true };
}
