import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Query,
} from '@nestjs/common';
import { SlotService } from './slot.service';
import { CreateSlotDto, VoteCommentDto } from './dto/create-slot.dto';
import { UpdateSlotDto } from './dto/update-slot.dto';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { SearchNearSlotDto, SearchSlot, SearchSlotByCursor } from './dto/search-slot.dto';
import {
  AuthAllPayload,
  // AuthAllPayload,
  AuthPayload,
  AuthTenant,
  AuthUser,
} from 'src/auth/decorators/auth.payload';
import { Public } from 'src/auth/decorators/public.auth';

@ApiTags('Slot API')
@ApiBearerAuth()
@Controller('slot')
export class SlotController {
  constructor(private readonly slotService: SlotService) { }

  @Post()
  create(
    @Body() createSlotDto: CreateSlotDto,
    @AuthAllPayload() tenant: AuthPayload,
  ) {
    try {
      return this.slotService.create(createSlotDto, tenant);
    } catch (error) {
      console.log(error);
      throw error;
    }
  }


  @Post('vote/:id')
  vote(
    @Param('id') slot_id: number,
    @Body() voteCommentDto: VoteCommentDto,
    @AuthUser() user: AuthPayload,
  ) {
    return this.slotService.voteSlot(+slot_id, voteCommentDto, user);
  }

  @Get('vote/:id')
  detaiVote(
    @Param('id') slot_id: number,
    // @Body() voteCommentDto: VoteCommentDto,
    @AuthUser() user: AuthPayload,
  ) {
    return this.slotService.getVoteSlot(+slot_id, user);
  }

  @Get()
  @Public()
  findAll(@Query() query: SearchSlot) {
    return this.slotService.findAll(query);
  }

  @Get('/cursor')
  @Public()
  findAllByCursor(@Query() query: SearchSlotByCursor) {
    return this.slotService.listByCursor(query);
  }

  @Get('/tenant')
  findAllOfMe(
    @Query() query: SearchSlot,
    @AuthTenant() tenant: AuthPayload,
  ) {
    return this.slotService.findAll(query, tenant);
  }

  @Get('/user')
  findAllOfUser(
    @Query() query: SearchSlot,
    @AuthUser() user: AuthPayload,
  ) {
    return this.slotService.findAll(query, user);
  }

  @Get('/near')
  @Public()
  findNear(@Query() query: SearchNearSlotDto) {
    return this.slotService.userFindNearByCoordinate(query);
  }

  @Get(':id')
  @Public()
  findOne(@Param('id') id: string) {
    return this.slotService.findOne(+id);
  }

  @Patch(':id')
  update(
    @Param('id') id: string,
    @Body() updateSlotDto: UpdateSlotDto,
    @AuthAllPayload() tenant: AuthPayload,
  ) {
    return this.slotService.update(+id, updateSlotDto, tenant);
  }

  @Delete(':id')
  remove(@Param('id') id: string, @AuthTenant() tenant: AuthPayload) {
    return this.slotService.remove(+id, tenant);
  }
}
