import { Request, Response } from 'express';
import { ResponseHandler } from '../utils/responseHandler';
import logger from '../utils/logger';
import { runForm16SapResponseIngestionOnce } from '../jobs/form16SapResponseJob';

export class Form16SapController {
  /**
   * POST /api/v1/form16/sap/pull
   * Trigger an immediate scan of the SAP OUTGOING directories for new Form 16 response CSVs.
   * Safe to call multiple times; ingestion is idempotent by file name.
   */
  async pull(req: Request, res: Response): Promise<void> {
    try {
      const result = await runForm16SapResponseIngestionOnce();
      return ResponseHandler.success(res, result, 'Pulled SAP responses');
    } catch (e: any) {
      logger.error('[Form16SapController] pull error:', e);
      return ResponseHandler.error(res, 'Failed to pull SAP responses', 500, e?.message || 'Unknown error');
    }
  }
}

export const form16SapController = new Form16SapController();

