/**
 * `tbd template` - Find and output document templates.
 *
 * Templates are reusable document templates for specs, research briefs, etc.
 * Give a name or description and tbd will find the matching template.
 */

import { Command } from 'commander';

import { DocCommandHandler, type DocCommandOptions } from '../lib/doc-command-handler.js';
import { CLIError } from '../lib/errors.js';
import { DEFAULT_TEMPLATE_PATHS } from '../../lib/paths.js';

class TemplateHandler extends DocCommandHandler {
  constructor(command: Command) {
    super(command, {
      typeName: 'template',
      typeNamePlural: 'templates',
      paths: DEFAULT_TEMPLATE_PATHS,
      docType: 'template',
    });
  }

  async run(query: string | undefined, options: DocCommandOptions): Promise<void> {
    await this.execute(async () => {
      // Add mode
      if (options.add) {
        if (!options.name) {
          throw new CLIError('--name is required when using --add');
        }
        await this.handleAdd(options.add, options.name);
        return;
      }

      await this.initCache();

      // List mode
      if (options.list) {
        await this.handleList(options.all);
        return;
      }

      // No query: show help
      if (!query) {
        await this.handleNoQuery();
        return;
      }

      // Query provided: try exact match first, then fuzzy
      await this.handleQuery(query);
    }, 'Failed to find template');
  }
}

export const templateCommand = new Command('template')
  .description('Find and output document templates')
  .argument('[query]', 'Template name or description to search for')
  .option('--list', 'List all available templates')
  .option('--all', 'Include shadowed templates (use with --list)')
  .option('--add <url>', 'Add a template from a URL')
  .option('--name <name>', 'Name for the added template (required with --add)')
  .action(async (query: string | undefined, options: DocCommandOptions, command) => {
    const handler = new TemplateHandler(command);
    await handler.run(query, options);
  });
