import {
  Body, Controller, Delete, Get, HttpCode, HttpStatus,
  Param, ParseUUIDPipe, Patch, Post, Query,
} from '@nestjs/common';
import { RuleEngineCreate, RuleEngineDelete, RuleEngineUpdate } from '../../../common/rule-engine-guards';
import { StaffReference } from '../../../common/booking-guards';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { CreateYardDto } from '../dto/create-yard.dto';
import { ListYardsQueryDto } from '../dto/list-rule-engine-query.dto';
import { MoveOrderDto } from '../dto/move-order.dto';
import { ReorderItemsDto } from '../dto/reorder-items.dto';
import { UpdateYardDto } from '../dto/update-yard.dto';
import { YardsService } from '../services/yards.service';

@ApiTags('yards')
@Controller('yards')
@ApiBearerAuth()
export class YardsController {
  constructor(private readonly service: YardsService) {}

  @Get()
  // Reference read: every staff form/search needs the yard list (origin /
  // destination pickers), so login is the only requirement.
  @StaffReference()
  @ApiOperation({ summary: 'List yards' })
  findAll(@Query() query: ListYardsQueryDto) {
    return this.service.findAll(query);
  }

  @Post('reorder')
  @RuleEngineUpdate('yards')
  @HttpCode(HttpStatus.NO_CONTENT)
  @ApiOperation({ summary: 'Bulk reorder yards by ID list' })
  reorder(@Body() dto: ReorderItemsDto) {
    return this.service.reorder(dto);
  }

  @Post(':id/move-order')
  @RuleEngineUpdate('yards')
  @HttpCode(HttpStatus.NO_CONTENT)
  @ApiOperation({ summary: 'Move a yard up or down in display order' })
  moveOrder(@Param('id', ParseUUIDPipe) id: string, @Body() dto: MoveOrderDto) {
    return this.service.moveOrder(id, dto.direction);
  }

  @Get(':id')
  @StaffReference()
  @ApiOperation({ summary: 'Get a yard by ID' })
  findOne(@Param('id', ParseUUIDPipe) id: string) {
    return this.service.findById(id);
  }

  @Post()
  @RuleEngineCreate('yards')
  @ApiOperation({ summary: 'Create a yard' })
  create(@Body() dto: CreateYardDto) {
    return this.service.create(dto);
  }

  @Patch(':id')
  @RuleEngineUpdate('yards')
  @ApiOperation({ summary: 'Update a yard' })
  update(@Param('id', ParseUUIDPipe) id: string, @Body() dto: UpdateYardDto) {
    return this.service.update(id, dto);
  }

  @Delete(':id')
  @RuleEngineDelete('yards')
  @HttpCode(HttpStatus.NO_CONTENT)
  @ApiOperation({ summary: 'Soft-delete a yard' })
  remove(@Param('id', ParseUUIDPipe) id: string) {
    return this.service.remove(id);
  }
}
