import { Body, Controller, Delete, Get, Param, Post, Put, Query, Request } from '@nestjs/common';
import { CollateralsService } from './collaterals.service';
import { RequirePermissions } from '../../common/decorators/permissions.decorator';

@Controller('collaterals/:type')
export class CollateralsController {
  constructor(private svc: CollateralsService) {}

  @Get()
  @RequirePermissions('applications.view')
  findAll(@Request() req: any, @Param('type') type: any, @Query('applicationId') applicationId?: string) {
    return this.svc.findAll(req.tenant, type, applicationId);
  }

  @Get(':id')
  @RequirePermissions('applications.view')
  findOne(@Request() req: any, @Param('type') type: any, @Param('id') id: string) {
    return this.svc.findOne(req.tenant, type, id);
  }

  @Post()
  @RequirePermissions('applications.create')
  create(@Request() req: any, @Param('type') type: any, @Body() body: any) {
    return this.svc.create(req.tenant, type, body, req.user?.sub);
  }

  @Post('bulk-upsert')
  @RequirePermissions('applications.create')
  bulkUpsert(@Request() req: any, @Param('type') type: any, @Body() body: { applicationId: string; items: any[] }) {
    return this.svc.bulkUpsert(req.tenant, type, body.applicationId, body.items, req.user?.sub);
  }

  @Put(':id')
  @RequirePermissions('applications.update')
  update(@Request() req: any, @Param('type') type: any, @Param('id') id: string, @Body() body: any) {
    return this.svc.update(req.tenant, type, id, body, req.user?.sub);
  }

  @Delete(':id')
  @RequirePermissions('applications.delete')
  remove(@Request() req: any, @Param('type') type: any, @Param('id') id: string) {
    return this.svc.remove(req.tenant, type, id);
  }
}
