import { Command } from 'commander'
import { Portal } from 'plazbot'
import { getStoredCredentials } from '../../utils/credentials'
import { logger } from '../../utils/logger'
import { BaseCommandOptions } from '../../types/common'

interface AddLinkOptions extends BaseCommandOptions {
  title: string
  url: string
}

export const addLinkCommand = new Command('add-link')
  .description('Agregar un enlace al portal')
  .argument('<portalId>', 'ID del portal')
  .requiredOption('-t, --title <title>', 'Título del enlace')
  .requiredOption('-u, --url <url>', 'URL del enlace')
  .option('--dev', 'Usar entorno de desarrollo')
  .action(async (portalId: string, options: AddLinkOptions) => {
    try {
      const credentials = await getStoredCredentials()
      const portal = new Portal({
        workspaceId: credentials.workspace,
        apiKey: credentials.apiKey,
        zone: credentials.zone,
        ...(options.dev && { customUrl: "http://localhost:5090" })
      })

      await portal.addLinkToPortal({
        portalId,
        value: options.title,
        url: options.url
      })

      console.log('✅ Enlace agregado exitosamente')
    } catch (error: unknown) {
      console.error('❌ Error al agregar el enlace:', (error as Error).message)
      process.exit(1)
    }
  })
