import { Body, Controller, Delete, Get, HttpException, HttpStatus, Param, Post, Put, Query, Res, UploadedFiles, UseGuards, Headers, UseInterceptors } from '@nestjs/common';
import { ApiBearerAuth, ApiBody, ApiConsumes, ApiParam, ApiTags } from '@nestjs/swagger';
import { Response } from 'express';
import {  CreateDepartmentDto, CreateMaterialConsumedDto, CreateMaterialDto, CreateMaterialIssuedDto, CreateMaterialRecievedDto, UpdateDepartmentDto, UpdateMaterialConsumedDto, UpdateMaterialDto, UpdateMaterialIssuedDto, UpdateMaterialRecievedDto } from './dto/material.dto';
import { MaterialService } from './material.service';
import { FilesInterceptor } from '@nestjs/platform-express';
import { diskStorage } from 'multer';
const axios = require("axios").default;

@Controller('material')
@ApiTags('Material')

export class MaterialController {

    constructor(private readonly materialService: MaterialService) { }

    @Get('/')
    // @UseGuards(JwtAuthGuard)
    getMaterials() {
        try {
            return this.materialService.getMaterials();
        } catch (ex) {
            ex;
        }
       
    }

    @Get('/:id')
    // @UseGuards(JwtAuthGuard)
    async getMaterialById(@Param('id')id: number,@Res() res?: Response) {
        try {
        var response = await this.materialService.getMaterialById(id);
        if(response){
          return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Department detail with respect to id",data:response});
          }else{
          return res.status(HttpStatus.BAD_REQUEST).json({statusCode:res.statusCode,statusMessage:"Something went wrong",data:""});
          }
        } catch (ex) {
          ex;  
        }
        
    }
      
    @Post('/add')

    async add(@Body() data: CreateMaterialDto,@Headers() headers: Headers,@Res() res?: Response) {
        try {
            var response = await this.materialService.add(data);
            if(response == "alreadyexist"){
              return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Material Already Exist",data:"Material Already Exist"});
          }else{
              return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Material has been added successfully",data:"Material has been added successfully"});
          }
        } catch (ex) {
            throw ex;
        }
    }

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

      @ApiBearerAuth('jwt')
      // @UseGuards(JwtAuthGuard)
      @Delete('/delete/:id')
      async delete(@Param('id') id: number,@Res() res: Response) {
          try {
              if (!id) throw new HttpException('Please enter a valid id', HttpStatus.BAD_REQUEST)
              var execute_delete_query = await this.materialService.delete(id);
            if(execute_delete_query == "deleted"){
              return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Material has been deleted",data:"Material has been deleted"});
              }else{
              return res.status(HttpStatus.BAD_REQUEST).json({statusCode:res.statusCode,statusMessage:"Something went wrong",data:""});
              }
          } catch (ex) {
              throw ex;
          }
      }

      //Material Issued

      @Get('/materialIssue/all')
      // @UseGuards(JwtAuthGuard)
      getAllMaterialIssue() {
          try {
              return this.materialService.getAllMaterialIssue();
          } catch (ex) {
              ex;
          }
         
      }
  
      @Get('/materialIssue/:id')
      // @UseGuards(JwtAuthGuard)
      async getMaterialIssueById(@Param('id')id: number,@Res() res?: Response) {
          try {
          var response = await this.materialService.getMaterialIssueById(id);
          if(response){
            return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Material Issued detail with respect to id",data:response});
            }else{
            return res.status(HttpStatus.BAD_REQUEST).json({statusCode:res.statusCode,statusMessage:"Something went wrong",data:""});
            }
          } catch (ex) {
            ex;  
          }
          
      }
  
      @Post('/materialIssue/add')
  
      async materialIssue(@Body() data: CreateMaterialIssuedDto,@Headers() headers: Headers,@Res() res?: Response) {
          try {
              var response = await this.materialService.materialIssue(data);
              if(response == "alreadyexist"){
                return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Material Issued Already Exist",data:"Material Issued Already Exist"});
            }else{
                return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Material Issued has been added successfully",data:"Material Issued has been added successfully"});
            }
          } catch (ex) {
              throw ex;
          }
      }
  
      @Put('/materialIssue/update/:id')
      //@UseGuards(JwtAuthGuard)
      async updateMaterialIssue(@Param('id') id: number,@Body() data:UpdateMaterialIssuedDto,@Res() res: Response) {
            try {
                if (!id) throw new HttpException('Please enter a valid id', HttpStatus.BAD_REQUEST)
            var mydata = await this.materialService.updateMaterialIssue(id,data);
            return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Material Issued has been updated",data:mydata});
            } catch (ex) {
            return res.status(HttpStatus.BAD_REQUEST).json({statusCode:res.statusCode,statusMessage:"Something went wrong",data:ex});
            }
            
        }
               
      @ApiBearerAuth('jwt')
      // @UseGuards(JwtAuthGuard)
      @Delete('/materialIssue/delete/:id')
      async deleteMaterialIssue(@Param('id') id: number,@Res() res: Response) {
          try {
              if (!id) throw new HttpException('Please enter a valid id', HttpStatus.BAD_REQUEST)
              var execute_delete_query = await this.materialService.deleteMaterialIssue(id);
            if(execute_delete_query == "deleted"){
              return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Material Issued has been deleted",data:"Material Issued has been deleted"});
              }else{
              return res.status(HttpStatus.BAD_REQUEST).json({statusCode:res.statusCode,statusMessage:"Something went wrong",data:""});
              }
          } catch (ex) {
              throw ex;
          }
      }

        //Material Consumed

        @Get('/materialConsume/all')
        // @UseGuards(JwtAuthGuard)
        getAllMaterialConsume() {
            try {
                return this.materialService.getAllMaterialConsume();
            } catch (ex) {
                ex;
            }
           
        }
    
        @Get('/materialConsume/:id')
        // @UseGuards(JwtAuthGuard)
        async getMaterialConsumeById(@Param('id')id: number,@Res() res?: Response) {
            try {
            var response = await this.materialService.getMaterialConsumeById(id);
            if(response){
              return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Material Consumed detail with respect to id",data:response});
              }else{
              return res.status(HttpStatus.BAD_REQUEST).json({statusCode:res.statusCode,statusMessage:"Something went wrong",data:""});
              }
            } catch (ex) {
              ex;  
            }
            
        }
    
        @Post('/materialConsume/add')
    
        async materialConsume(@Body() data: CreateMaterialConsumedDto,@Headers() headers: Headers,@Res() res?: Response) {
            try {
                var response = await this.materialService.materialConsume(data);
                if(response == "alreadyexist"){
                  return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Material Consumed Already Exist",data:"Material Consumed Already Exist"});
              }else{
                  return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Material Consumed has been added successfully",data:"Material Consumed has been added successfully"});
              }
            } catch (ex) {
                throw ex;
            }
        }
    
        @Put('/materialConsume/update/:id')
        //@UseGuards(JwtAuthGuard)
        async updateMaterialConsume(@Param('id') id: number,@Body() data:UpdateMaterialConsumedDto,@Res() res: Response) {
              try {
                  if (!id) throw new HttpException('Please enter a valid id', HttpStatus.BAD_REQUEST)
              var mydata = await this.materialService.updateMaterialConsume(id,data);
              return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Material Consumed has been updated",data:mydata});
              } catch (ex) {
              return res.status(HttpStatus.BAD_REQUEST).json({statusCode:res.statusCode,statusMessage:"Something went wrong",data:ex});
              }
              
          }
                 
        @ApiBearerAuth('jwt')
        // @UseGuards(JwtAuthGuard)
        @Delete('/materialConsume/delete/:id')
        async deleteMaterialConsume(@Param('id') id: number,@Res() res: Response) {
            try {
                if (!id) throw new HttpException('Please enter a valid id', HttpStatus.BAD_REQUEST)
                var execute_delete_query = await this.materialService.deleteMaterialConsume(id);
              if(execute_delete_query == "deleted"){
                return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Material Consumed has been deleted",data:"Material Consumed has been deleted"});
                }else{
                return res.status(HttpStatus.BAD_REQUEST).json({statusCode:res.statusCode,statusMessage:"Something went wrong",data:""});
                }
            } catch (ex) {
                throw ex;
            }
        }

        //Material Recieved


        @Get('/materialRecieve/all')
      // @UseGuards(JwtAuthGuard)
      getAllMaterialRecieve() {
          try {
              return this.materialService.getAllMaterialRecieve();
          } catch (ex) {
              ex;
          }
         
      }
  
      @Get('/materialRecieve/:id')
      // @UseGuards(JwtAuthGuard)
      async getMaterialRecieveById(@Param('id')id: number,@Res() res?: Response) {
          try {
          var response = await this.materialService.getMaterialRecieveById(id);
          if(response){
            return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Material Recieved detail with respect to id",data:response});
            }else{
            return res.status(HttpStatus.BAD_REQUEST).json({statusCode:res.statusCode,statusMessage:"Something went wrong",data:""});
            }
          } catch (ex) {
            ex;  
          }
          
      }
  
      @Post('/materialRecieve/add')
  
      async materialRecieve(@Body() data: CreateMaterialRecievedDto,@Headers() headers: Headers,@Res() res?: Response) {
          try {
              var response = await this.materialService.materialRecieve(data);
              if(response == "alreadyexist"){
                return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Material Recieved Already Exist",data:"Material Recieved Already Exist"});
            }else{
                return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Material Recieved has been added successfully",data:"Material Recieved has been added successfully"});
            }
          } catch (ex) {
              throw ex;
          }
      }
  
      @Put('/materialRecieve/update/:id')
      //@UseGuards(JwtAuthGuard)
      async updateMaterialRecieve(@Param('id') id: number,@Body() data:UpdateMaterialRecievedDto,@Res() res: Response) {
            try {
                if (!id) throw new HttpException('Please enter a valid id', HttpStatus.BAD_REQUEST)
            var mydata = await this.materialService.updateMaterialRecieve(id,data);
            return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Material Recieved has been updated",data:mydata});
            } catch (ex) {
            return res.status(HttpStatus.BAD_REQUEST).json({statusCode:res.statusCode,statusMessage:"Something went wrong",data:ex});
            }
            
        }
               
      @ApiBearerAuth('jwt')
      // @UseGuards(JwtAuthGuard)
      @Delete('/materialRecieve/delete/:id')
      async deleteMaterialRecieve(@Param('id') id: number,@Res() res: Response) {
          try {
              if (!id) throw new HttpException('Please enter a valid id', HttpStatus.BAD_REQUEST)
              var execute_delete_query = await this.materialService.deleteMaterialRecieve(id);
            if(execute_delete_query == "deleted"){
              return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Material Recieved has been deleted",data:"Material Recieved has been deleted"});
              }else{
              return res.status(HttpStatus.BAD_REQUEST).json({statusCode:res.statusCode,statusMessage:"Something went wrong",data:""});
              }
          } catch (ex) {
              throw ex;
          }
      }

   



















      //Department
   
    @Get('department/all')
    // @UseGuards(JwtAuthGuard)
    getDepartments() {
        try {
            return this.materialService.getDepartments();
        } catch (ex) {
            ex;
        }
       
    }

    @Get('/department/:id')
    // @UseGuards(JwtAuthGuard)
    async getDepartmentById(@Param('id')id: number,@Res() res?: Response) {
        try {
        var response = await this.materialService.getDepartmentById(id);
        if(response){
          return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Department detail with respect to id",data:response});
          }else{
          return res.status(HttpStatus.BAD_REQUEST).json({statusCode:res.statusCode,statusMessage:"Something went wrong",data:""});
          }
        } catch (ex) {
          ex;  
        }
        
    }

    @Post('/department/add')

    async addDepartment(@Body() data: CreateDepartmentDto,@Headers() headers: Headers,@Res() res?: Response) {
        try {
            var response = await this.materialService.addDepartment(data);
            if(response == "alreadyexist"){
              return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Department Already Exist",data:"Department Already Exist"});
          }else{
              return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Department has been added successfully",data:"Department has been added successfully"});
          }
        } catch (ex) {
            throw ex;
        }
    }

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

      @ApiBearerAuth('jwt')
      // @UseGuards(JwtAuthGuard)
      @Delete('/department/delete/:id')
      async delete_dept(@Param('id') id: number,@Res() res: Response) {
          try {
              if (!id) throw new HttpException('Please enter a valid id', HttpStatus.BAD_REQUEST)
              var execute_delete_query = await this.materialService.delete_dept(id);
            if(execute_delete_query == "deleted"){
              return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Department has been deleted",data:"Department has been deleted"});
              }else{
              return res.status(HttpStatus.BAD_REQUEST).json({statusCode:res.statusCode,statusMessage:"Something went wrong",data:""});
              }
          } catch (ex) {
              throw ex;
          }
      }

}
