import {
  Controller,
  Get,
  Post,
  Put,
  Param,
  Body,
  Query,
  UseGuards,
  NotFoundException,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { MerchantOrderService } from './order.service';
import { SellerJwtAuthGuard } from '@/common/guards/seller-jwt-auth.guard';
import { CurrentSeller } from '@/common/decorators/current-seller.decorator';
import { MerchantProfileService } from '../profile/profile.service';
import { QueryOrderDto } from './dto/order.dto';

@ApiTags('订单管理(商家)')
@Controller('merchant/orders')
@UseGuards(SellerJwtAuthGuard)
@ApiBearerAuth()
export class MerchantOrderController {
  constructor(
    private readonly orderService: MerchantOrderService,
    private readonly profileService: MerchantProfileService,
  ) {}

  @Get()
  @ApiOperation({ summary: '商家订单列表' })
  async findMine(
    @CurrentSeller('sub') sellerId: number,
    @Query() query: QueryOrderDto,
  ) {
    const merchant = await this.profileService.findBySellerId(sellerId);
    if (!merchant) throw new NotFoundException('店铺不存在');
    return this.orderService.findByMerchant(merchant.id, query);
  }

  @Get(':orderNo')
  @ApiOperation({ summary: '商家订单详情' })
  async getDetail(
    @CurrentSeller('sub') sellerId: number,
    @Param('orderNo') orderNo: string,
  ) {
    const merchant = await this.profileService.findBySellerId(sellerId);
    if (!merchant) throw new NotFoundException('店铺不存在');
    const order = await this.orderService.findOne(orderNo);
    if (order.merchantId !== merchant.id) {
      throw new NotFoundException('订单不存在');
    }
    return order;
  }

  @Post('detail')
  @ApiOperation({ summary: '商家订单详情(POST)' })
  async getDetailPost(
    @CurrentSeller('sub') sellerId: number,
    @Body('orderNo') orderNo: string,
  ) {
    const merchant = await this.profileService.findBySellerId(sellerId);
    if (!merchant) throw new NotFoundException('店铺不存在');
    const order = await this.orderService.findOne(orderNo);
    if (order.merchantId !== merchant.id) {
      throw new NotFoundException('订单不存在');
    }
    return order;
  }

  @Put(':orderNo/confirm')
  @ApiOperation({ summary: '确认订单' })
  async confirm(
    @CurrentSeller('sub') sellerId: number,
    @Param('orderNo') orderNo: string,
  ) {
    const merchant = await this.profileService.findBySellerId(sellerId);
    if (!merchant) throw new NotFoundException('店铺不存在');
    return this.orderService.confirm(merchant.id, orderNo);
  }

  @Put(':orderNo/reject')
  @ApiOperation({ summary: '拒绝订单' })
  async reject(
    @CurrentSeller('sub') sellerId: number,
    @Param('orderNo') orderNo: string,
    @Body('reason') reason: string,
  ) {
    const merchant = await this.profileService.findBySellerId(sellerId);
    if (!merchant) throw new NotFoundException('店铺不存在');
    return this.orderService.reject(merchant.id, orderNo, reason);
  }

  @Put(':orderNo/checkin')
  @ApiOperation({ summary: '办理入住' })
  async checkin(
    @CurrentSeller('sub') sellerId: number,
    @Param('orderNo') orderNo: string,
  ) {
    const merchant = await this.profileService.findBySellerId(sellerId);
    if (!merchant) throw new NotFoundException('店铺不存在');
    return this.orderService.checkin(merchant.id, orderNo);
  }

  @Put(':orderNo/checkout')
  @ApiOperation({ summary: '确认离店' })
  async checkout(
    @CurrentSeller('sub') sellerId: number,
    @Param('orderNo') orderNo: string,
  ) {
    const merchant = await this.profileService.findBySellerId(sellerId);
    if (!merchant) throw new NotFoundException('店铺不存在');
    return this.orderService.checkout(merchant.id, orderNo);
  }
}
