import { Controller, Get, Post, Body, Patch, Param, Delete, Query, HttpException, HttpStatus, Put, UseGuards, Res, UseInterceptors, UploadedFile, Headers, UploadedFiles } from '@nestjs/common';
import { AttachmentService } from './attachment.service';
import { CreateAttachmentDto,UpdateAttachmentDto,editFileName} from './dto/attachment.dto';
import { Response } from 'express';
import { JwtAuthGuard } from 'src/guards/auth/jwt-auth.guard';
import { ApiBody, ApiConsumes, ApiTags } from '@nestjs/swagger';
import { FileInterceptor, FilesInterceptor } from '@nestjs/platform-express';
import { diskStorage } from 'multer';

@Controller('attachment')
@ApiTags('Attachment')
export class AttachmentController {
  constructor(private readonly attachmentService: AttachmentService) {}
  // async create(@Body() data: CreateAttachmentDto,@Body() reqested,@Res() res: Response,@UploadedFile() file: Express.Multer.File,@Headers() headers: Headers) {
  //   try {
  //    // console.log("hererer",headers.get('host'));
  //     var responsedData = await this.attachmentService.create(data,reqested,file.filename);           
  //     if(responsedData){
        
  //       // return res.status(HttpStatus.CREATED).send();
    
  //       return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Attachment has been added successfully",data:responsedData});
  //     }
  // } catch (ex) {
  //   return res.status(HttpStatus.BAD_REQUEST).json({statusCode:res.statusCode,statusMessage:"Something went wrong",data:ex});
  
  // }
  
  // }

  @Post('/add')
  // @UseGuards(JwtAuthGuard)
  @UseInterceptors(
    FilesInterceptor('name', 20, {
      storage: diskStorage({
        destination: './files',
        filename: editFileName,
      }),
    }),
  )
  @ApiConsumes('multipart/form-data')
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        attachment_type :{
          type: 'string'
        },
        name: { // 👈 this property
          type: 'array',
          items:{
            type: 'string',
            format: 'binary',

          }
        },
      },
    },
  })
  async create(@Body() data: CreateAttachmentDto,@Body() reqested,@Res() res: Response,@UploadedFiles() files,@Headers() headers: Headers) {
    try {
      var responsedData = await this.attachmentService.create(data,reqested,files);           
      if(responsedData){
        
        // return res.status(HttpStatus.CREATED).send();
    
        return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Attachment has been added successfully",data:responsedData});
      }
  } catch (ex) {
    return res.status(HttpStatus.BAD_REQUEST).json({statusCode:res.statusCode,statusMessage:"Something went wrong",data:ex});
  
  }
  
  }





  @Get('/allAttachments')
  // @UseGuards(JwtAuthGuard)
   async getAttachments(
        @Query('page') page?: number,
        @Query('limit') limit?: number,@Res() res?: Response
    ) {
        try {
            page= page || 1;
            limit= limit || 10;
            var data = await this.attachmentService.getAttachments({page,limit});
            return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Attachment list",data:data});
        } catch (ex) {
          return res.status(HttpStatus.BAD_REQUEST).json({statusCode:res.statusCode,statusMessage:"Something went wrong",data:ex});
        }
       
    }
  //   @Get('/gethealthandsafety')
  // // @UseGuards(JwtAuthGuard)
  //  async gethealthandsafety(@Res() res?: Response) {
  //       try {
  //           var data = await this.attachmentService.gethealthandsafety();
  //           return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Health Attachment list",data:data});
  //       } catch (ex) {
  //         return res.status(HttpStatus.BAD_REQUEST).json({statusCode:res.statusCode,statusMessage:"Something went wrong",data:ex});
  //       }
       
  //   }

    

  // @Get(':id')
  // findOne(@Param('id') id: string) {
  //   return this.serviceService.findOne(+id);
  // }

  @Get('/:id')
  // @UseGuards(JwtAuthGuard)
   async getAttachment(@Param('id') id: number,@Res() res?: Response) {
        try {
            if (!id) throw new HttpException('Please enter a valid id', HttpStatus.BAD_REQUEST)
        var data = await this.attachmentService.getAttachment(id);
        return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"single Attachment detail",data:data});
        } catch (ex) {
        return res.status(HttpStatus.BAD_REQUEST).json({statusCode:res.statusCode,statusMessage:"Something went wrong",data:ex});
        }
        
    }
    


  // @Get('/:activityId/:tokenId')
  // // @UseGuards(JwtAuthGuard)
  //  async getAttachmentActivityId(@Param('activityId') activityId: number,@Param('tokenId') tokenId: string,@Res() res?: Response) {
  //       try {
  //           if (!activityId) throw new HttpException('Please enter a valid id', HttpStatus.BAD_REQUEST)
  //       var data = await this.attachmentService.getAttachmentActivityId(activityId,tokenId);
  //       return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Activity Attachments detail",data:data});
  //       } catch (ex) {
  //       return res.status(HttpStatus.BAD_REQUEST).json({statusCode:res.statusCode,statusMessage:"Something went wrong",data:ex});
  //       }
        
  //   }

    
  // @Patch(':id')
  // update(@Param('id') id: string, @Body() updateServiceDto: UpdateServiceDto) {
  //   return this.serviceService.update(+id, updateServiceDto);
  // }

  
  @Put('/update/:id')
  // @UseGuards(JwtAuthGuard)
    async updateUser(@Param('id') id: number,@Body() data :UpdateAttachmentDto,@Body() requested,@Res() res?: Response) {
        try {
            if (!id) throw new HttpException('Please enter a valid id', HttpStatus.BAD_REQUEST)
        var mydata = await this.attachmentService.update(id,data,requested);
        return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Attachment has been updated",data:mydata});
        } catch (ex) {
        return res.status(HttpStatus.BAD_REQUEST).json({statusCode:res.statusCode,statusMessage:"Something went wrong",data:ex});
        }
        
    }

  @Delete(':id')
  // @UseGuards(JwtAuthGuard)
  remove(@Param('id') id: string) {
    return this.attachmentService.remove(+id);
  }

  // @Post('/healthandsafety/add')
  // // @UseGuards(JwtAuthGuard)
  // @UseInterceptors(
  //   FilesInterceptor('name', 20, {
  //     storage: diskStorage({
  //       destination: './media',
  //       filename: editFileName,
  //     }),
  //   }),
  // )
  // @ApiConsumes('multipart/form-data')
  // @ApiBody({
  //   schema: {
  //     type: 'object',
  //     properties: {
  //       title :{
  //         type: 'string'
  //       },
  //       description :{
  //         type: 'string'
  //       },
  //       name: { // 👈 this property
  //         type: 'string',
  //         format: 'binary',
  //       },
  //     },
  //   },
  // })

  // async healthandsafety(@Body() requested,@Res() res: Response,@UploadedFiles() files,@Headers() headers: Headers) {
  //   try {
  //     var responsedData = await this.attachmentService.healthandsafety(requested,files);           
  //     if(responsedData == "done"){
        
  //       // return res.status(HttpStatus.CREATED).send();
    
  //       return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Attachment has been added successfully",data:responsedData});
  //     }
  // } catch (ex) {
  //   return res.status(HttpStatus.BAD_REQUEST).json({statusCode:res.statusCode,statusMessage:"Something went wrong",data:ex});
  
  // }
  
  // }


}
