/**
 * Data Controller
 *
 * REST endpoints for raw SQL operations
 * Replaces: ExecuteCommand, GetRDataSQL
 */
import { Body, Controller, Post, UseGuards } from '@nestjs/common';
import { DataService } from './data.service';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';

@Controller('data')
@UseGuards(JwtAuthGuard)
export class DataController {
  constructor(private dataService: DataService) {}

  /**
   * POST /api/data/execute
   * Execute raw SQL (INSERT, UPDATE, DELETE)
   * Maps to: ExecuteCommand(sql)
   */
  @Post('execute')
  async execute(@Body() body: { sql: string }) {
    if (!body.sql || typeof body.sql !== 'string') {
      return { success: false, error: 'sql is required' };
    }
    return this.dataService.executeCommand(body.sql);
  }

  /**
   * POST /api/data/query
   * Execute SELECT and return results
   * Maps to: GetRDataSQL(sql, fname, flag)
   */
  @Post('query')
  async query(@Body() body: { sql: string }) {
    if (!body.sql || typeof body.sql !== 'string') {
      return { success: false, error: 'sql is required' };
    }
    const data = await this.dataService.queryData(body.sql);
    return { data };
  }
}
