import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Patch,
  Post,
  Query,
  Req,
  UseGuards,
} from "@nestjs/common";
import { UsersService } from "./users.service";
import { AccountType } from "@prisma/client";
import { CreateUserDto } from "./dto/create-user.dto";
import { UpdateUserDto } from "./dto/update-user.dto";
import { AccountTypesGuard } from "../common/guards/accountTypes.guard";
import { AccountTypes } from "../common/decorators/accountType.decorator";

@Controller("users")
@UseGuards(AccountTypesGuard)
@AccountTypes(AccountType.ADMIN)
export class UsersController {
  constructor(
    private readonly usersService: UsersService,
  ) { }

  @Post()
  @UseGuards(AccountTypesGuard)
  @AccountTypes(AccountType.ADMIN)
  create(@Body() createUserDto: CreateUserDto) {
    return this.usersService.create(createUserDto);
  }

  @Get()
  @UseGuards(AccountTypesGuard)
  @AccountTypes(AccountType.ADMIN)
  findAll(@Query("limit") limit?: string, @Query("page") page?: string, @Query("q") query?: string, @Query("role") role?: string, @Query("isActive") isActive?: string, @Query('warehouseId') warehouseId?: string,) {
    const pageNumber = parseInt(page ?? "1");
    const perPage = parseInt(limit ?? "10");

    return this.usersService.findAll(pageNumber, perPage, query, role, isActive, warehouseId);
  }

  @Get("search")
  @UseGuards(AccountTypesGuard)
  @AccountTypes(AccountType.ADMIN)
  searchUser(
    @Query("q") query: string,
    @Query("limit") limit?: string,
    @Query("page") page?: string,
    @Query("role") role?: string,
    @Query("isActive") isActive?: string
  ) {
    const pageNumber = parseInt(page ?? "1");
    const perPage = parseInt(limit ?? "10");
    return this.usersService.searchUser(query, pageNumber, perPage, role, isActive);
  }

  @Patch("status/change/:id")
  changeStatus(@Param("id") id: string) {
    return this.usersService.changeStatus(id);
  }

  @Get("profile")
  getProfile(@Req() req) {
    const userId = req.user?.sub;
    return this.usersService.findOne(userId);
  }

  @Get(":id")
  @UseGuards(AccountTypesGuard)
  @AccountTypes(AccountType.ADMIN)
  findOne(@Param("id") id: string) {
    return this.usersService.findOne(id);
  }

  // Admin-only: update another user by id (used by the Users → Edit screen).
  // This is NOT the self-profile update — that lives at PATCH /auth/profile
  // and derives the user from req.user.sub.
  @Patch(":id")
  @UseGuards(AccountTypesGuard)
  @AccountTypes(AccountType.ADMIN)
  updateUser(@Param("id") id: string, @Body() updateUserDto: UpdateUserDto) {
    return this.usersService.update(id, updateUserDto);
  }

  @Delete("profile")
  @UseGuards(AccountTypesGuard)
  remove(@Req() req) {
    const userId = req.user?.sub;
    return this.usersService.remove(userId);
  }

  @Delete(":id")
  @UseGuards(AccountTypesGuard)
  @AccountTypes(AccountType.ADMIN)
  removeUser(@Param("id") id: string) {
    return this.usersService.remove(id);
  }



}