import { authBridgesProvidersBarrel, formatWorkspaceFiles, runGraphqlTypes, setEnvValue, wireBridgesModule, } from '../package-helpers.js';
/**
 * Identity (IAM + OAuth) package installer.
 *
 * Runs after both the `iam` and `o-auth` template trees have been copied by
 * the meta-package dispatch in `addBackPackage`. Responsible for:
 *
 * 1. Installing the union of runtime dependencies.
 * 2. Wiring `@bridges/bridges.module.ts` to the golden-reference shape.
 * 3. Authoring `@bridges/providers/index.ts` barrel.
 * 4. Formatting both touched files with the consumer's own Prettier (so
 *    eslint-plugin-prettier/recommended passes without further changes).
 * 5. Setting `OAUTH_STRATEGY=local-provider` in `.env`.
 * 6. Running GraphQL typings regeneration last (D6).
 *
 * MUST NOT:
 * - Edit `@aurora/decorators/auth.decorator.ts`.
 * - Auto-run `catalyst keys` or any seeder.
 *
 * Returns the "Next steps" banner as a string so the `add` command can print
 * it AFTER `spinner.succeed()`, keeping spinner output clean.
 */
export default async function install(ctx) {
    // 1. Install union of runtime deps (iam + o-auth combined).
    await ctx.install([
        'nestjs-i18n',
        '@nestjs/jwt',
        '@nestjs/passport',
        'passport',
        'passport-jwt',
    ]);
    // 2. Wire @bridges/bridges.module.ts to the golden-reference shape
    //    (idempotent: skip-if-present for each import / array element).
    const bridgesModule = ctx.sourceFile('@bridges', 'bridges.module.ts');
    wireBridgesModule(bridgesModule);
    // 3. Author @bridges/providers/index.ts barrel (five re-exports).
    authBridgesProvidersBarrel(ctx.cwd);
    // 4. Format both touched files with the consumer's Prettier so that
    //    eslint-plugin-prettier/recommended + prettier-plugin-organize-imports
    //    pass without further changes (fixes #1 from the adversarial review).
    await formatWorkspaceFiles(ctx, [
        'src/@bridges/bridges.module.ts',
        'src/@bridges/providers/index.ts',
    ]);
    // 5. Set OAUTH_STRATEGY=local-provider in .env (idempotent line rewrite).
    setEnvValue(ctx.cwd, 'OAUTH_STRATEGY', 'local-provider');
    // 6. GraphQL typings regen runs LAST (D6): both @api/iam and @api/o-auth
    //    schemas must be on disk before the generator resolves cross-BC adapter
    //    imports from @api/graphql.
    await runGraphqlTypes(ctx);
    // Return the post-install banner so the caller can print it after
    // spinner.succeed() — avoids mixing banner lines with the spinner chrome.
    return `
╔══════════════════════════════════════════════════════════════════════════╗
║  Identity (IAM + OAuth) installed — next steps required:                 ║
║                                                                          ║
║  1. Generate RSA keypair (run from the backend workspace):               ║
║       catalyst keys                                                      ║
║                                                                          ║
║  2. Set key paths in .env:                                               ║
║       OAUTH_PRIVATE_KEY_PATH = <path>/oauth-private.key                  ║
║       OAUTH_PUBLIC_KEY_PATH  = <path>/oauth-public.key                   ║
║                                                                          ║
║  3. Restart the backend (or run pnpm dev).                               ║
╚══════════════════════════════════════════════════════════════════════════╝
`;
}
