import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  UseGuards,
  Req,
  Res,
  Query,
} from "@nestjs/common";
import { ApiBearerAuth, ApiQuery, ApiTags } from "@nestjs/swagger";
import { Request, Response } from "express";
import { PerformanceService } from "./performance.service";
import { CreatePerformanceDto } from "./dto/create-performance.dto";
import { UpdatePerformanceDto } from "./dto/update-performance.dto";
import { UserAuthGuard } from "src/common/guards/jwt-auth.guard";
import { UserDecorator } from "src/common/decorator/user.decorator";

@ApiTags("Performance")
@ApiBearerAuth()
@Controller("performance")
export class PerformanceController {
  constructor(private readonly performanceService: PerformanceService) {}

  // POST /performance — log one exercise for a date (upsert)
  @UseGuards(UserAuthGuard)
  @Post()
  async create(
    @Body() dto: CreatePerformanceDto,
    @Req() req: Request,
    @Res() res: Response,
    @UserDecorator() user: any,
  ) {
    return this.performanceService.create(dto, req, res, user);
  }

  // GET /performance/personal-records — best set per exercise across all sessions
  @UseGuards(UserAuthGuard)
  @Get("personal-records")
  async getPersonalRecords(
    @Req() req: Request,
    @Res() res: Response,
    @UserDecorator() user: any,
  ) {
    return this.performanceService.getPersonalRecords(req, res, user);
  }

  // GET /performance/personal-records/:exerciseId — PR for one specific exercise
// @Get("history/:exerciseId")
// getPersonalRecordHistory(
//   @Param("exerciseId") exerciseId: string,
//   @Req() req: Request,
//   @Res() res: Response,
//      @UserDecorator() user: any,
// ) {
//   return this.performanceService.getPersonalRecordHistory(
//     exerciseId,
//     req,
//     res,
//     user,
//   );
// }

  // GET /performance/exercise/:exerciseId/history — full history for one exercise (Deadlift screen)
  @UseGuards(UserAuthGuard)
  @Get("history/:exerciseId")
  @ApiQuery({ name: "page", required: false, example: 1 })
  @ApiQuery({ name: "limit", required: false, example: 10 })
  async getExerciseHistory(
    @Param("exerciseId") exerciseId: string,
    @Query("page") page = "1",
    @Query("limit") limit = "10",
    @Req() req: Request,
    @Res() res: Response,
    @UserDecorator() user: any,
  ) {
    return this.performanceService.getExerciseHistory(exerciseId, +page, +limit, res, req, user);
  }

  // GET /performance?date=YYYY-MM-DD — all exercises logged on a specific date
  @UseGuards(UserAuthGuard)
  @Get()
  @ApiQuery({ name: "date", required: true, example: "2025-06-17" })
  async getByDate(
    @Query("date") date: string,
    @Req() req: Request,
    @Res() res: Response,
    @UserDecorator() user: any,
  ) {
    return this.performanceService.getByDate(date, req, res, user);
  }

  // PATCH /performance/:id — update sets for a specific exercise log
  @UseGuards(UserAuthGuard)
  @Patch(":id")
  async update(
    @Param("id") id: string,
    @Body() dto: UpdatePerformanceDto,
    @Req() req: Request,
    @Res() res: Response,
    @UserDecorator() user: any,
  ) {
    return this.performanceService.update(id, dto, req, res, user);
  }

  
  // DELETE /performance/:id — soft delete one exercise log
  @UseGuards(UserAuthGuard)
  @Delete(":id")
  async remove(
    @Param("id") id: string,
    @Req() req: Request,
    @Res() res: Response,
    @UserDecorator() user: any,
  ) {
    return this.performanceService.remove(id, req, res, user);
  }
}
