// import { 
//   Controller, 
//   Post, 
//   Body, 
//   UploadedFiles, 
//   UseInterceptors, 
//   BadRequestException 
// } from '@nestjs/common';
// import { FilesInterceptor } from '@nestjs/platform-express';
// import { ApiTags, ApiConsumes, ApiBody } from '@nestjs/swagger';
// import { VoiceTextSarvamService } from './voice-text-sarvam.service';

// @ApiTags('voice-text-sarvam')
// @Controller('voice-text-sarvam')
// export class VoiceTextSarvamController {
//   constructor(private readonly voiceTextSarvamService: VoiceTextSarvamService) {}

//   @Post()
//   @ApiConsumes('multipart/form-data')
//   @ApiBody({
//     schema: {
//       type: 'object',
//       properties: {
//         files: {
//           type: 'array',
//           items: {
//             type: 'string',
//             format: 'binary',
//           },
//           description: 'Array of audio files (.wav or .mp3 format)',
//         },
//         opd_id: {
//           type: 'string',
//           description: 'OPD identifier',
//         },
//         hospital_id: {
//           type: 'string',
//           description: 'Hospital identifier',
//         },
//       },
//     },
//   })
//   @UseInterceptors(
//     FilesInterceptor('files', 2, { // Accept up to 10 files
//       fileFilter: (req, file, cb) => {
        
        
//         if (!['audio/wav', 'audio/x-wav', 'audio/mpeg','audio/wave'].includes(file.mimetype)) {
          
          
//           cb(new BadRequestException('Only .wav or .mp3 files are allowed!'), false);
//         } else {
          
          
//           cb(null, true);
//         }
//       },
//     }),
//   )
//   async create(
//     @UploadedFiles() files: Express.Multer.File[],
//     @Body('opd_id') opd_id: string,
//     @Body('hospital_id') hospital_id: string,
//   ) {
    
    
//     if (!files || files.length === 0) {
//       throw new BadRequestException('At least one valid .wav or .mp3 file is required');
//     }

//     return this.voiceTextSarvamService.create(files, opd_id, hospital_id);
//   }
// }


import { 
  Controller, 
  Post, 
  Body, 
  UploadedFiles, 
  UseInterceptors, 
  BadRequestException, 
  UploadedFile,
  Get,
  Query
} from '@nestjs/common';
import { FileInterceptor, FilesInterceptor } from '@nestjs/platform-express';
import { ApiTags, ApiConsumes, ApiBody } from '@nestjs/swagger';
import { VoiceTextSarvamService } from './voice-text-sarvam.service';

@ApiTags('voice-text-sarvam')
@Controller('voice-text-sarvam')
export class VoiceTextSarvamController {
  constructor(private readonly voiceTextSarvamService: VoiceTextSarvamService) {}

  @Post()
  // @ApiConsumes('multipart/form-data')
  // @ApiBody({
  //   schema: {
  //     type: 'object',
  //     properties: {
  //       files: {
  //         type: 'array',
  //         items: {
  //           type: 'string',
  //           format: 'binary',
  //         },
  //         description: 'Array of audio files (.wav or .mp3 format)',
  //       },
  //       opd_id: {
  //         type: 'string',
  //         description: 'OPD identifier',
  //       },
  //       hospital_id: {
  //         type: 'string',
  //         description: 'Hospital identifier',
  //       },
  //     },
  //   },
  // })
  // @UseInterceptors(
  //   FilesInterceptor('files', 2, { // Accept up to 10 files
  //     fileFilter: (req, file, cb) => {
        
        
  //       if (!['audio/wav', 'audio/x-wav', 'audio/mpeg','audio/wave'].includes(file.mimetype)) {
          
          
  //         cb(new BadRequestException('Only .wav or .mp3 files are allowed!'), false);
  //       } else {
          
          
  //         cb(null, true);
  //       }
  //     },
  //   }),
  // )
  async create(
    // @UploadedFiles() files: Express.Multer.File[],
    @Body('opd_id') opd_id: string,
    @Body('hospital_id') hospital_id: string,
  ) {
    
    
    // if (!files || files.length === 0) {
    //   throw new BadRequestException('At least one valid .wav or .mp3 file is required');
    // }

    return this.voiceTextSarvamService.create(opd_id, hospital_id);
  }

  @Post('init-job')
  @UseInterceptors(
    FilesInterceptor('files', 2, { // Accept up to 10 files
      fileFilter: (req, file, cb) => {
        
        
        if (!['audio/wav', 'audio/x-wav', 'audio/mpeg','audio/wave'].includes(file.mimetype)) {
          
          
          cb(new BadRequestException('Only .wav or .mp3 files are allowed!'), false);
        } else {
          
          
          cb(null, true);
        }
      },
    }),
  )
  async initJob(
    @UploadedFiles() files: Express.Multer.File
  ) {
    // return this.voiceTextSarvamService.initialize(files);
    return this.voiceTextSarvamService.uploadFile(files);
  }


@Post('get-voice-data')
@ApiConsumes('multipart/form-data')
@ApiBody({
  schema: {
    type: 'object',
    properties: {
      file: {
        type: 'string',
        format: 'binary',
        description: 'Single audio file (.wav format only)',
      },
      opd_id: {
        type: 'string',
        description: 'OPD identifier',
      },
      hospital_id: {
        type: 'string',
        description: 'Hospital identifier',
      },
    },
    required: ['file', 'opd_id', 'hospital_id'],
  },
})
@UseInterceptors(
  FileInterceptor('file', {
    fileFilter: (req, file, cb) => {
      if (!['audio/wav', 'audio/x-wav','audio/wave'].includes(file.mimetype)) {
        cb(new BadRequestException('Only .wav files are allowed!'), false);
      } else {
        cb(null, true);
      }
    },
  }),
)
async getVoiceData(
  @UploadedFile() file: Express.Multer.File
) {
  if (!file) {
    throw new BadRequestException('A valid .wav file is required');
  }

  return this.voiceTextSarvamService.getFormData(file);
}




@Post('get-thp-data')
@ApiConsumes('multipart/form-data')
@ApiBody({
  schema: {
    type: 'object',
    properties: {
      file: {
        type: 'string',
        format: 'binary',
        description: 'Single audio file (.wav format only)',
      },
      opd_id: {
        type: 'string',
        description: 'OPD identifier',
      },
      hospital_id: {
        type: 'string',
        description: 'Hospital identifier',
      },
    },
    required: ['file', 'opd_id', 'hospital_id'],
  },
})
@UseInterceptors(
  FileInterceptor('file', {
    fileFilter: (req, file, cb) => {
      if (!['audio/wav', 'audio/x-wav','audio/wave'].includes(file.mimetype)) {
        cb(new BadRequestException('Only .wav files are allowed!'), false);
      } else {
        cb(null, true);
      }
    },
  }),
)
async getVoiceDataNew(
  @UploadedFile() file: Express.Multer.File
) {
  if (!file) {
    throw new BadRequestException('A valid .wav file is required');
  }

  return this.voiceTextSarvamService.getNewFormData(file);
}




@Post('aswin-res')
async ashwincreate(@Query('transcript_id') transcriptionText, @Body('opd_id') opd_id: string, @Body('hospital_id') hospital_id: string){
  
  return this.voiceTextSarvamService.categorizeWithAshwinAI(transcriptionText, opd_id,hospital_id)
}
}