/**
 * `tbd readme` - Display the README.
 *
 * Shows the bundled README (same as the GitHub landing page).
 */

import { Command } from 'commander';
import { readFile } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';

import { BaseCommand } from '../lib/base-command.js';
import { shouldUseInteractiveOutput } from '../lib/context.js';
import { CLIError } from '../lib/errors.js';
import { renderMarkdown, paginateOutput } from '../lib/output.js';

/**
 * Get the path to the bundled README file.
 * The README is copied to dist/docs/ during build.
 */
function getReadmePath(): string {
  const __filename = fileURLToPath(import.meta.url);
  const __dirname = dirname(__filename);
  // When bundled, runs from dist/bin.mjs or dist/cli.mjs
  // README is at dist/docs/README.md (same level as the bundle)
  return join(__dirname, 'docs', 'README.md');
}

class ReadmeHandler extends BaseCommand {
  async run(): Promise<void> {
    let content: string;
    try {
      content = await readFile(getReadmePath(), 'utf-8');
    } catch {
      // Fallback: try to read from source location during development
      try {
        const __filename = fileURLToPath(import.meta.url);
        const __dirname = dirname(__filename);
        // During development without bundle: src/cli/commands -> repo root
        const devPath = join(__dirname, '..', '..', '..', '..', '..', 'README.md');
        content = await readFile(devPath, 'utf-8');
      } catch {
        // Last fallback: try package-level README
        try {
          const __filename = fileURLToPath(import.meta.url);
          const __dirname = dirname(__filename);
          // From packages/tbd/src/cli/commands -> packages/tbd/README.md
          const pkgPath = join(__dirname, '..', '..', '..', 'README.md');
          content = await readFile(pkgPath, 'utf-8');
        } catch {
          throw new CLIError('README file not found. Please rebuild the CLI.');
        }
      }
    }

    // Output the README with Markdown colorization and pagination for interactive
    if (shouldUseInteractiveOutput(this.ctx)) {
      const rendered = renderMarkdown(content, this.ctx.color);
      await paginateOutput(rendered, true);
    } else {
      console.log(content);
    }
  }
}

export const readmeCommand = new Command('readme')
  .description('Display the README (same as GitHub landing page)')
  .action(async (_options: object, command: Command) => {
    const handler = new ReadmeHandler(command);
    await handler.run();
  });
