import { Body, Controller, Delete, Get, Param, ParseIntPipe, Patch, Post, Put, Query, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { ProductFamiliesService } from './product-families.service';
import { OrganizeService } from './organize.service';
import {
  AutoGroupDto,
  CreateCustomMemberDto,
  CreateProductFamilyDto,
  DeletePriceOverridesDto,
  PatchProductFamilyDto,
  PutPriceOverridesDto,
  QueryProductFamilyDto,
  UpdateFamilyMembersDto,
} from './dto/product-family.dto';

@ApiTags('product-families')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Controller('product-families')
export class ProductFamiliesController {
  constructor(
    private readonly service: ProductFamiliesService,
    private readonly organize: OrganizeService,
  ) {}

  @Get()
  @ApiOperation({ summary: 'List product families with keyword & pagination' })
  list(@Query() query: QueryProductFamilyDto) {
    return this.service.list(query);
  }

  @Post('organize')
  @ApiOperation({
    summary:
      '整理原产品库（显式人工动作）：回填解析列 → 派生标签（人工接管不动）→ 自动建族 → 全量重算矩阵',
  })
  organizeAll() {
    return this.organize.organize();
  }

  @Post('auto-group')
  @ApiOperation({ summary: 'Auto-group unassigned origin goods by 3-segment name key' })
  autoGroup(@Body() dto: AutoGroupDto) {
    return this.service.autoGroup(dto.apply ?? false);
  }

  @Post()
  @ApiOperation({ summary: 'Create a product family (optionally attach members)' })
  create(@Body() dto: CreateProductFamilyDto) {
    return this.service.create(dto);
  }

  @Get(':id')
  @ApiOperation({ summary: 'Family detail with members & overrides' })
  detail(@Param('id', ParseIntPipe) id: string) {
    return this.service.detail(BigInt(id));
  }

  @Patch(':id')
  @ApiOperation({ summary: 'Patch canonical fields / autoManaged / primary link' })
  patch(@Param('id', ParseIntPipe) id: string, @Body() dto: PatchProductFamilyDto) {
    return this.service.patch(BigInt(id), dto);
  }

  @Post(':id/recompute')
  @ApiOperation({ summary: 'Manually recompute union & price matrix' })
  recompute(@Param('id', ParseIntPipe) id: string) {
    return this.service.recomputeNow(BigInt(id));
  }

  @Post(':id/members')
  @ApiOperation({ summary: 'Add / remove SDS member links' })
  updateMembers(@Param('id', ParseIntPipe) id: string, @Body() dto: UpdateFamilyMembersDto) {
    return this.service.updateMembers(BigInt(id), dto);
  }

  @Post(':id/members/custom')
  @ApiOperation({ summary: 'Create a custom (manual) member inside the family' })
  createCustomMember(@Param('id', ParseIntPipe) id: string, @Body() dto: CreateCustomMemberDto) {
    return this.service.createCustomMember(BigInt(id), dto);
  }

  @Get(':id/price-overrides')
  @ApiOperation({ summary: 'List manual price overrides with derived-price comparison' })
  listOverrides(@Param('id', ParseIntPipe) id: string) {
    return this.service.listPriceOverrides(BigInt(id));
  }

  @Put(':id/price-overrides')
  @ApiOperation({ summary: 'Batch upsert manual price overrides' })
  putOverrides(@Param('id', ParseIntPipe) id: string, @Body() dto: PutPriceOverridesDto) {
    return this.service.putPriceOverrides(BigInt(id), dto.items);
  }

  @Delete(':id/price-overrides')
  @ApiOperation({ summary: 'Remove overrides to restore derived prices' })
  deleteOverrides(@Param('id', ParseIntPipe) id: string, @Body() dto: DeletePriceOverridesDto) {
    return this.service.deletePriceOverrides(BigInt(id), dto);
  }
}
