import path from 'node:path';
import { z } from 'zod';

export const rootPath = path.resolve(__dirname, '../../..');

export const KNOWN_PACKAGES = {
  rootPkg: 'learnitive',
  constantsPkg: '@learnitive/constants',
  sharedTypedPkg: '@learnitive/types',
  colorSchemeManagerPkg: '@learnitive/color-scheme-manager',
  testUtils: '@learnitive/test-utils',
};
export const VITEST_PKG_NAME = 'vitest';

export const PACKAGE_JSON_DEFAULTS = {
  license: 'AGPL-3.0-or-later',
  repository: {
    type: 'git',
    url: 'https://www.learnitive.com',
  },
  authors: [
    {
      name: 'Learnitive',
      web: 'https://www.learnitive.com',
    },
  ],
  homepage: 'https://www.learnitive.com',
  bugs: {
    url: 'https://www.learnitive.com/contact',
  },
  learnitivePackageConfig: {
    env: 'universal',
    kind: 'library',
  } satisfies LearnitivePackageConfig,
};

export const learnitiveWorkspaceConfigSchema = z.object({
  // The packages in workspaces (the folders in packages dir) that are allowed to be dependent on
  allowedWorkspaces: z.array(z.string()),
});

export const learnitivePackageConfigSchema = z.object({
  env: z.enum(['nodejs', 'browser', 'universal']),
  kind: z.enum([
    'library',
    'js-util',
    // all things that are static and have limited dep: configs, constants
    'static',
    'types',
    'build',
    'app',
    'service-platform',
    'service-core',
    'service-ui',
  ]),
  skipValidation: z.boolean().optional(),
  allowedRuntimeImports: z.array(z.string()).optional(),
});

// defines the order in which the services should be depended on
// for example. first service-platform cannot anything after it.
// service-core can depend on itself and service-platform
export const serviceKindOrders = [
  'service-platform',
  'service-core',
  'service-ui',
];

export type LearnitiveWorkspaceConfig = z.infer<typeof learnitiveWorkspaceConfigSchema>;
export type LearnitivePackageConfig = z.infer<typeof learnitivePackageConfigSchema>;
