import {
  BadRequestException,
  Body,
  Controller,
  Delete,
  Get,
  HttpStatus,
  NotFoundException,
  Param,
  Patch,
  Res,
  UseGuards,
} from '@nestjs/common';
import { Response as ExpressResponse } from 'express';
import {
  errorResponse,
  successResponse,
} from 'src/common/errors/response-config';
import { AuthMiddleware } from 'src/common/middleware/auth.middleware';
import { lan } from 'src/lan';
import { ContentManagementsService } from './content_managements.service';
import { UpdateContentManagementDto } from './dto/update-content_management.dto';

@Controller('content-managements')
export class ContentManagementsController {
  constructor(
    private readonly contentManagementsService: ContentManagementsService,
  ) {}

  @Get(':type')
  @UseGuards(AuthMiddleware)
  async findOne(@Param('type') type: string, @Res() res: ExpressResponse) {
    try {
      const data = await this.contentManagementsService.findType(type);

      if (data) {
        res
          .status(HttpStatus.OK)
          .send(
            successResponse(
              200,
              lan('common.data_retrieved_successfully'),
              data,
            ),
          );
      }
    } catch (error) {
      if (
        error instanceof NotFoundException ||
        error instanceof BadRequestException
      ) {
        res
          .status(HttpStatus.BAD_REQUEST)
          .send(errorResponse(404, error.message));
      } else {
        res
          .status(HttpStatus.INTERNAL_SERVER_ERROR)
          .send(errorResponse(500, lan('common.internal_server_error'), error));
      }
    }
  }

  @Get(':type/users')
  async findContent(@Param('type') type: string, @Res() res: ExpressResponse) {
    try {
      const data = await this.contentManagementsService.findType(type);

      if (data) {
        res
          .status(HttpStatus.OK)
          .send(
            successResponse(
              200,
              lan('common.data_retrieved_successfully'),
              data,
            ),
          );
      }
    } catch (error) {
      if (
        error instanceof NotFoundException ||
        error instanceof BadRequestException
      ) {
        res
          .status(HttpStatus.BAD_REQUEST)
          .send(errorResponse(404, error.message));
      } else {
        res
          .status(HttpStatus.INTERNAL_SERVER_ERROR)
          .send(errorResponse(500, lan('common.internal_server_error'), error));
      }
    }
  }

  @Patch(':type')
  @UseGuards(AuthMiddleware)
  async update(
    @Param('type') type: string,
    @Body() updateContentManagement: UpdateContentManagementDto,
    @Res() res: ExpressResponse,
  ) {
    try {
      await this.contentManagementsService.update(
        type,
        updateContentManagement,
      );

      res
        .status(HttpStatus.OK)
        .send(successResponse(200, lan('common.data_updated')));
    } catch (error) {
      if (
        error instanceof NotFoundException ||
        error instanceof BadRequestException
      ) {
        res
          .status(HttpStatus.BAD_REQUEST)
          .send(errorResponse(404, error.message));
      } else {
        res
          .status(HttpStatus.INTERNAL_SERVER_ERROR)
          .send(errorResponse(500, lan('common.internal_server_error'), error));
      }
    }
  }

  @Delete(':type')
  @UseGuards(AuthMiddleware)
  async remove(@Param('type') type: string, @Res() res: ExpressResponse) {
    try {
      const contentData = await this.contentManagementsService.removeContent(
        type,
      );

      res
        .status(HttpStatus.OK)
        .send(successResponse(200, lan('common.data_deleted'), contentData));
    } catch (error) {
      if (
        error instanceof NotFoundException ||
        error instanceof BadRequestException
      ) {
        res
          .status(HttpStatus.BAD_REQUEST)
          .send(errorResponse(404, error.message));
      } else {
        res
          .status(HttpStatus.INTERNAL_SERVER_ERROR)
          .send(errorResponse(500, lan('common.internal_server_error'), error));
      }
    }
  }
}
