#!/usr/bin/env node
import { CloverServer } from './server.js';
import { CloverConfig } from './types/index.js';

function getConfig(): CloverConfig {
  const merchantId = process.env.CLOVER_MERCHANT_ID;
  const apiKey = process.env.CLOVER_API_KEY;
  const accessToken = process.env.CLOVER_ACCESS_TOKEN;
  const environment = (process.env.CLOVER_ENVIRONMENT || 'sandbox') as 'sandbox' | 'production';

  if (!merchantId) {
    console.error('Error: CLOVER_MERCHANT_ID environment variable is required');
    process.exit(1);
  }

  if (!apiKey && !accessToken) {
    console.error('Error: Either CLOVER_API_KEY or CLOVER_ACCESS_TOKEN must be set');
    process.exit(1);
  }

  return {
    merchantId,
    apiKey,
    accessToken,
    environment,
  };
}

async function main() {
  try {
    const config = getConfig();
    const server = new CloverServer(config);
    
    console.error('Clover MCP Server starting...');
    console.error(`Merchant ID: ${config.merchantId}`);
    console.error(`Environment: ${config.environment}`);
    console.error('Server running. Ready for MCP connections.');

    await server.run();
  } catch (error: any) {
    console.error('Fatal error:', error.message);
    process.exit(1);
  }
}

main();
