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 { CreateProjectDto,UpdateProjectDto,CreateSubProjectDto,UpdateSubProjectDto, CreateProjectHistoryDto, UpdateProjectHistoryDto, CreateSubSubProjectDto, UpdateSubSubProjectDto } from './dto/project.dto';
import { ProjectService } from './project.service';
import { FilesInterceptor } from '@nestjs/platform-express';
import { diskStorage } from 'multer';
const axios = require("axios").default;

@Controller('project')
@ApiTags('Project')
export class ProjectController {
    constructor(private readonly projectService: ProjectService) { }

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

    @Get('/:id')
    // @UseGuards(JwtAuthGuard)
    async getProjectbyId(@Param('id') id: number,@Res() res?: Response) {
        try {
           
        var response = await this.projectService.getProjectbyId(id);
        if(response){
          return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Project detail by Id",data:response});
          }else{
          return res.status(HttpStatus.BAD_REQUEST).json({statusCode:res.statusCode,statusMessage:"Something went wrong",data:""});
          }
        } catch (ex) {
          ex;  
        }
        
    }

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


    @Post('/addProject')

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


    @Put('/update/:id')
    //@UseGuards(JwtAuthGuard)
    async updateProject(@Param('id') id: number,@Body() data :UpdateProjectDto,@Res() res: Response) {
          try {
              if (!id) throw new HttpException('Please enter a valid id', HttpStatus.BAD_REQUEST)
          var mydata = await this.projectService.update(id,data);
          return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Project 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')
     remove(@Param('id') id: string) {
     return this.projectService.remove(+id);
      }


//Project History

@Get('/projectHistory/all')
// @UseGuards(JwtAuthGuard)
getHistory() {
    try {
        return this.projectService.getHistory();
    } catch (ex) {
        ex;
    }
   
}

@Get('/projectHistory/:id')
// @UseGuards(JwtAuthGuard)
async getProjectHistorybyId(@Param('id') id: number,@Res() res?: Response) {
    try {
       
    var response = await this.projectService.getProjectHistorybyId(id);
    if(response){
      return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Project History detail by Id",data:response});
      }else{
      return res.status(HttpStatus.BAD_REQUEST).json({statusCode:res.statusCode,statusMessage:"Something went wrong",data:""});
      }
    } catch (ex) {
      ex;  
    }
    
}


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


@Post('/projectHistory/add')

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


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


//Subproject

      @Post('/addSubProject')
   
      async addsubproject(@Body() data: CreateSubProjectDto,@Headers() headers: Headers,@Res() res?: Response) {
          try {
              var response = await this.projectService.addsub(data);
              if(response == "alreadyexist"){
                return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Sub-Project Already Exist",data:"Sub-Project Already Exist"});
            }else{
                return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Sub-Project has been added successfully",data:"Project has been added successfully"});
            }
          } catch (ex) {
              throw ex;
          }
      }
  
      @Put('/updateSubProject/:id')
      //@UseGuards(JwtAuthGuard)
      async updatesubproject(@Param('id') id: number,@Body() data :UpdateSubProjectDto,@Res() res: Response) {
            try {
                if (!id) throw new HttpException('Please enter a valid id', HttpStatus.BAD_REQUEST)
            var mydata = await this.projectService.updatesubproject(id,data);
            return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Sub-Project 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')
        removesubproject(@Param('id') id: string) {
        return this.projectService.removesub(+id);
         }


         
      @Get('/subproject/:project_name/:client_name')
      // @UseGuards(JwtAuthGuard)
      async getSubProject(@Param('project_name') project_name: string,@Param('client_name') client_name: string,@Res() res?: Response) {
          try {
          var response = await this.projectService.getSubProject(project_name,client_name);
          if(response){
            return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Sub-Project detail",data:response});
            }else{
            return res.status(HttpStatus.BAD_REQUEST).json({statusCode:res.statusCode,statusMessage:"Something went wrong",data:""});
            }
          } catch (ex) {
            ex;  
          }
          
      }
        
            
      @ApiBearerAuth('jwt')
      // @UseGuards(JwtAuthGuard)
      @Delete('/subproject/:id')
      async delete_subproject(@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.projectService.delete_subproject(id);
            if(execute_delete_query == "deleted"){
              return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Sub-Project has been deleted",data:"Sub-Project has been deleted"});
              }else{
              return res.status(HttpStatus.BAD_REQUEST).json({statusCode:res.statusCode,statusMessage:"Something went wrong",data:""});
              }
          } catch (ex) {
              throw ex;
          }
      }
      

      //SubSubproject

      @Get('/getSubSubProject/:project_name/:client_name')
      // @UseGuards(JwtAuthGuard)
      async getSubSubProject(@Param('project_name') project_name: string,@Param('client_name') client_name: string,@Res() res?: Response) {
          try {
          var response = await this.projectService.getSubSubProject(project_name,client_name);
          if(response){
            return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Sub-Sub Project detail",data:response});
            }else{
            return res.status(HttpStatus.BAD_REQUEST).json({statusCode:res.statusCode,statusMessage:"Something went wrong",data:""});
            }
          } catch (ex) {
            ex;  
          }
          
      }


      @Post('/addSubSubproject')
   
      async addSubSubproject(@Body() data: CreateSubSubProjectDto,@Headers() headers: Headers,@Res() res?: Response) {
          try {
              var response = await this.projectService.addSubSubproject(data);
              if(response == "alreadyexist"){
                return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Sub-Sub-Project Already Exist",data:"Sub-Sub Project Already Exist"});
            }else{
                return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Sub-Sub-Project has been added successfully",data:"Sub-Sub Project has been added successfully"});
            }
          } catch (ex) {
              throw ex;
          }
      }
  
      @Put('/updateSubSubProject/:id')
      //@UseGuards(JwtAuthGuard)
      async updateSubSubProject(@Param('id') id: number,@Body() data :UpdateSubSubProjectDto,@Res() res: Response) {
            try {
                if (!id) throw new HttpException('Please enter a valid id', HttpStatus.BAD_REQUEST)
            var mydata = await this.projectService.updateSubSubProject(id,data);
            return res.status(HttpStatus.OK).json({statusCode:res.statusCode,statusMessage:"Sub-Sub Project has been updated",data:mydata});
            } catch (ex) {
            return res.status(HttpStatus.BAD_REQUEST).json({statusCode:res.statusCode,statusMessage:"Something went wrong",data:ex});
            }
            
        }


        @Delete('/updateSubSubProject/:id')
        removeSubSub(@Param('id') id: string) {
        return this.projectService.remove(+id);
         }

         
     
}
