import { Body, Controller, Get, Param, Patch, Req, UseGuards } from '@nestjs/common';
import { JwtAuthGuard } from '../common/guards/jwt-auth.guard';
import { Roles } from '../common/decorators/roles.decorator';
import { RolesGuard } from '../common/guards/roles.guard';
import { OrdersService } from './orders.service';

@Controller('orders')
@UseGuards(JwtAuthGuard, RolesGuard)
export class OrdersController {
  constructor(private readonly ordersService: OrdersService) {}

  @Get()
  list(@Req() req: { user: { sub: string; role: string } }) {
    return this.ordersService.list(req.user as never);
  }

  @Get('flow-settings')
  @Roles('RESTAURANT')
  getRestaurantFlowSettings(@Req() req: { user: { sub: string } }) {
    return this.ordersService.getRestaurantFlowSettings(req.user.sub);
  }

  @Patch('flow-settings')
  @Roles('RESTAURANT')
  updateRestaurantFlowSettings(
    @Req() req: { user: { sub: string } },
    @Body()
    body: {
      acceptMode?: 'MANUAL' | 'AUTOMATIC';
      autoRequestDeliveryWhenReady?: boolean;
      driverPresenceWindowMinutes?: number;
    },
  ) {
    return this.ordersService.updateRestaurantFlowSettings(req.user.sub, body);
  }

  @Patch(':id/accept')
  @Roles('RESTAURANT')
  acceptByRestaurant(@Req() req: { user: { sub: string; role: string } }, @Param('id') id: string) {
    return this.ordersService.acceptRestaurantOrder(req.user as never, id);
  }

  @Patch(':id/ready')
  @Roles('RESTAURANT')
  markReady(@Req() req: { user: { sub: string; role: string } }, @Param('id') id: string) {
    return this.ordersService.markRestaurantOrderReady(req.user as never, id);
  }

  @Patch(':id/request-delivery')
  @Roles('RESTAURANT')
  requestDelivery(@Req() req: { user: { sub: string; role: string } }, @Param('id') id: string) {
    return this.ordersService.requestRestaurantDelivery(req.user as never, id);
  }

  @Patch(':id/cancel')
  @Roles('ADMIN', 'SUPER_ADMIN')
  cancel(@Param('id') id: string, @Body() body: { reason?: string }) {
    return this.ordersService.cancel(id, body.reason);
  }
}
