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

@ApiTags('商家信息管理')
@Controller('merchant/profile')
@UseGuards(SellerJwtAuthGuard)
@ApiBearerAuth()
export class MerchantProfileController {
  constructor(private readonly profileService: MerchantProfileService) {}

  @Post('apply')
  @ApiOperation({ summary: '申请商家入驻' })
  async apply(
    @CurrentSeller('sub') sellerId: number,
    @Body() dto: ApplyMerchantDto,
  ) {
    return this.profileService.apply(sellerId, dto);
  }

  @Get('mine')
  @ApiOperation({ summary: '获取我的店铺信息' })
  async getMine(@CurrentSeller('sub') sellerId: number) {
    return this.profileService.findBySellerId(sellerId);
  }

  @Put('update')
  @ApiOperation({ summary: '更新店铺信息' })
  async update(
    @CurrentSeller('sub') sellerId: number,
    @Body() dto: UpdateMerchantDto,
  ) {
    const merchant = await this.profileService.findBySellerId(sellerId);
    if (!merchant) throw new Error('店铺不存在');
    return this.profileService.update(merchant.id, dto);
  }

  @Get('detail/:id')
  @ApiOperation({ summary: '获取商家详情(公开)' })
  async findById(@Param('id') id: number) {
    return this.profileService.findById(id);
  }
}
