import { MODULE_PATHS, APP_PATHS } from "./paths";

export interface CategoryConfig {
  name: string;
  sourcePattern: string;
  docPattern: string;
  exclusions: RegExp[];
}

export interface TestCaseCategoryConfig {
  name: string;
  docPattern: string;
  testDir: string;
}

export interface CheckTarget {
  glob: string;
  schemaKey: string;
}

export function moduleCategories(root: string): CategoryConfig[] {
  return [
    {
      name: "command",
      sourcePattern: `${root}/*/${MODULE_PATHS.commands}/*.ts`,
      docPattern: `${root}/*/${MODULE_PATHS.docs.commands}/*.md`,
      exclusions: [/\.test\.ts$/, /\.generated\.ts$/],
    },
    {
      name: "model",
      sourcePattern: `${root}/*/${MODULE_PATHS.db}/*.ts`,
      docPattern: `${root}/*/${MODULE_PATHS.docs.models}/*.md`,
      exclusions: [/\.test\.ts$/, /^index\.ts$/],
    },
    {
      name: "query",
      sourcePattern: `${root}/*/${MODULE_PATHS.queries}/*.ts`,
      docPattern: `${root}/*/${MODULE_PATHS.docs.queries}/*.md`,
      exclusions: [/\.test\.ts$/],
    },
  ];
}

export function appComposeCategories(root: string): CategoryConfig[] {
  return [
    {
      name: "resolver",
      sourcePattern: `${root}/backend/src/resolvers/*.ts`,
      docPattern: `${root}/${APP_PATHS.docs.resolver}/*.md`,
      exclusions: [/\.test\.ts$/, /^index\.ts$/],
    },
  ];
}

export function appTestCaseCategories(root: string): TestCaseCategoryConfig {
  return {
    name: "story-test-case",
    docPattern: `${root}/*/${APP_PATHS.docs.businessFlow}/*/${APP_PATHS.docs.story}/*.md`,
    testDir: APP_PATHS.tests.stories,
  };
}

export function moduleTestCaseCategories(root: string): TestCaseCategoryConfig[] {
  return [
    {
      name: "command-test-case",
      docPattern: `${root}/*/${MODULE_PATHS.docs.commands}/*.md`,
      testDir: MODULE_PATHS.commands,
    },
    {
      name: "query-test-case",
      docPattern: `${root}/*/${MODULE_PATHS.docs.queries}/*.md`,
      testDir: MODULE_PATHS.queries,
    },
  ];
}

export function buildCheckTargets(config: {
  modulesRoot?: string;
  appRoot?: string;
}): CheckTarget[] {
  const targets: CheckTarget[] = [];

  if (config.modulesRoot) {
    const m = config.modulesRoot;
    targets.push(
      { glob: `${m}/[a-zA-Z]*/${MODULE_PATHS.docs.features}/*.md`, schemaKey: "feature" },
      { glob: `${m}/[a-zA-Z]*/${MODULE_PATHS.docs.commands}/*.md`, schemaKey: "command" },
      { glob: `${m}/[a-zA-Z]*/${MODULE_PATHS.docs.models}/*.md`, schemaKey: "model" },
      { glob: `${m}/[a-zA-Z]*/${MODULE_PATHS.docs.queries}/*.md`, schemaKey: "query" },
      { glob: `${m}/[a-zA-Z]*/README.md`, schemaKey: "module" },
    );
  }

  if (config.appRoot) {
    const a = config.appRoot;
    targets.push(
      { glob: `${a}/README.md`, schemaKey: "app" },
      { glob: `${a}/${APP_PATHS.docs.actors}/*.md`, schemaKey: "actors" },
      {
        glob: `${a}/${APP_PATHS.docs.businessFlow}/*/README.md`,
        schemaKey: "business-flow",
      },
      {
        glob: `${a}/${APP_PATHS.docs.businessFlow}/*/${APP_PATHS.docs.story}/*.md`,
        schemaKey: "story",
      },
      { glob: `${a}/${APP_PATHS.docs.screen}/*.md`, schemaKey: "screen" },
      { glob: `${a}/${APP_PATHS.docs.resolver}/*.md`, schemaKey: "resolver" },
    );
  }

  return targets;
}
