import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  UsePipes,
  Res,
  BadRequestException,
  HttpStatus,
  ValidationPipe,
  UseGuards,
  Query,
  Headers,
  NotFoundException,
} from '@nestjs/common';
import { StreaksService } from './streaks.service';
import { CreateStreakDto } from './dto/create-streak.dto';
import { UpdateStreakDto } from './dto/update-streak.dto';
import { lan } from 'src/lan';
import {
  errorResponse,
  successResponse,
  validationErrorMessage,
} from 'src/common/errors/response-config';
import { Response as ExpressResponse } from 'express';
import { saveError } from 'src/api-logs/api-error-log';
import { InjectRepository } from '@nestjs/typeorm';
import { ApiLog } from 'src/api-logs/entities/api-log.entity';
import { Repository } from 'typeorm';
import { AuthMiddleware } from 'src/common/middleware/auth.middleware';

@Controller('streaks')
@UseGuards(AuthMiddleware)
export class StreaksController {
  constructor(
    private readonly streaksService: StreaksService,
    @InjectRepository(ApiLog)
    private readonly apiLogRepository: Repository<ApiLog>,
  ) {}

  @Post()
  @UsePipes(
    new ValidationPipe({
      exceptionFactory: validationErrorMessage,
      transform: true,
      transformOptions: {
        enableImplicitConversion: true,
      },
    }),
  )
  async create(
    @Body() createStreakDto: CreateStreakDto,
    @Res() res: ExpressResponse,
  ) {
    try {
      await this.streaksService.create(createStreakDto);
      res.send(successResponse(201, lan('common.request_submitted')));
    } catch (error) {
      if (error instanceof BadRequestException) {
        res
          .status(HttpStatus.BAD_REQUEST)
          .send(errorResponse(400, error.message));
      } else {
        res
          .status(HttpStatus.INTERNAL_SERVER_ERROR)
          .send(errorResponse(500, lan('common.internal_server_error'), error));
      }

      saveError(error, this.apiLogRepository);
    }
  }

  @Get()
  async findAll(
    @Query('take') take: number,
    @Query('skip') skip: number,
    @Query('key') key: string,
    @Query('order') order: string,
    @Res() res: ExpressResponse,
    @Headers() headers: any,
  ) {
    try {
      const allAchievement = await this.streaksService.findAll(
        take,
        skip,
        key,
        order,
        headers,
      );

      res
        .status(HttpStatus.OK)
        .send(
          successResponse(
            200,
            lan('common.data_retrieved_successfully'),
            allAchievement,
          ),
        );
    } catch (error) {
      res
        .status(HttpStatus.INTERNAL_SERVER_ERROR)
        .send(errorResponse(500, lan('common.internal_server_error'), error));

      saveError(error, this.apiLogRepository);
    }
  }

  @Get('user')
  async findAllAchievement(
    @Query('take') take: number,
    @Query('skip') skip: number,
    @Query('key') key: string,
    @Query('order') order: string,
    @Query('search') search: string,
    @Query('recent') recent: boolean,
    @Query('province') province: string,
    @Res() res: ExpressResponse,
    @Headers() headers: any,
  ) {
    try {
      const allAchievement = await this.streaksService.findAllStreak(
        take,
        skip,
        key,
        order,
        search,
        recent,
        province,
      );

      res
        .status(HttpStatus.OK)
        .send(
          successResponse(
            200,
            lan('common.data_retrieved_successfully'),
            allAchievement,
          ),
        );
    } catch (error) {
      res
        .status(HttpStatus.INTERNAL_SERVER_ERROR)
        .send(errorResponse(500, lan('common.internal_server_error'), error));

      saveError(error, this.apiLogRepository);
    }
  }

  @Get(':id')
  async findOne(@Param('id') id: string, @Res() res: ExpressResponse) {
    try {
      const data = await this.streaksService.findOne(id);
      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.NOT_FOUND)
          .send(errorResponse(404, error.message));
      } else {
        res
          .status(HttpStatus.INTERNAL_SERVER_ERROR)
          .send(errorResponse(500, lan('common.internal_server_error'), error));
      }

      saveError(error, this.apiLogRepository);
    }
  }

  @Get('user/record')
  async findAllUserAchievement(
    @Query('take') take: number,
    @Query('skip') skip: number,
    @Query('user_id') userId: string,
    @Query('date') date: string,
    @Res() res: ExpressResponse,
  ) {
    try {
      const allAchievement = await this.streaksService.findUserStreak(
        take,
        skip,
        userId,
      );

      res
        .status(HttpStatus.OK)
        .send(
          successResponse(
            200,
            lan('common.data_retrieved_successfully'),
            allAchievement,
          ),
        );
    } catch (error) {
      saveError(error, this.apiLogRepository);
      if (error instanceof NotFoundException) {
        res
          .status(HttpStatus.NOT_FOUND)
          .send(errorResponse(404, error.message));
      } else {
        res
          .status(HttpStatus.INTERNAL_SERVER_ERROR)
          .send(errorResponse(500, lan('common.internal_server_error'), error));
      }
    }
  }

  @Delete(':id')
  async remove(@Param('id') id: string, @Res() res: ExpressResponse) {
    try {
      await this.streaksService.remove(id);
      res
        .status(HttpStatus.OK)
        .send(successResponse(200, lan('common.data_deleted')));
    } 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));
      }

      saveError(error, this.apiLogRepository);
    }
  }
}
