import {
  Controller,
  Get,
  Param,
  Query,
} from '@nestjs/common';
import {
  ApiExtraModels,
  ApiOkResponse,
  ApiOperation,
  ApiParam,
  ApiQuery,
  ApiTags,
  getSchemaPath,
} from '@nestjs/swagger';
import { PublicService } from './public.service';
import {
  PublicCountryQueryDto,
  PublicHomeGoodsQueryDto,
  PublicQueryGoodDto,
  PublicTagFilterDto,
} from './dto/public-query-good.dto';
import { PublicTagDto } from './dto/public-tag.dto';
import { PublicGoodDetailDto, PublicTagGroupFilterDto } from './dto/public-good-detail.dto';
import { PublicGoodDto } from './dto/public-good.dto';

@ApiTags('public')
@ApiExtraModels(PublicTagFilterDto)
@Controller('public')
export class PublicController {
  constructor(private readonly service: PublicService) {}

  @Get('categories')
  @ApiOperation({ summary: '获取商品分类树；countryId 不传时返回全部国家' })
  getCategories(@Query() query: PublicCountryQueryDto) {
    return this.service.getCategoriesTree(query.countryId);
  }

  @Get('countries')
  @ApiOperation({ summary: 'Public list of countries that have goods' })
  getCountries() {
    return this.service.getCountries();
  }

  @Get('tags')
  @ApiOperation({ summary: 'Public list of tags that have goods' })
  getTags(): Promise<PublicTagDto[]> {
    return this.service.getTags();
  }

  @Get('tag-groups')
  @ApiOperation({ summary: '获取标签组及标签筛选项；countryId 不传时返回全部国家' })
  @ApiOkResponse({ type: [PublicTagGroupFilterDto] })
  getTagGroups(@Query() query: PublicCountryQueryDto): Promise<PublicTagGroupFilterDto[]> {
    return this.service.getTagGroups(query.countryId);
  }

  @Get('goods')
  @ApiOperation({ summary: '分页获取商品（族化契约：一族一条，goodId=族ID；无族商品不返回）' })
  @ApiQuery({
    name: 'tags',
    required: false,
    description:
      '标签筛选分组。参数值为 JSON 数组；同组 tagIds 按 OR 匹配，不同标签组按 AND 匹配',
    content: {
      'application/json': {
        schema: {
          type: 'array',
          items: { $ref: getSchemaPath(PublicTagFilterDto) },
        },
        example: [
          { tagGroupId: '1', tagIds: ['11', '12'] },
          { tagGroupId: '2', tagIds: ['25'] },
        ],
      },
    },
  })
  getGoods(@Query() query: PublicQueryGoodDto) {
    return this.service.getGoods(query);
  }

  @Get('goods/:goodId')
  @ApiOperation({ summary: '获取商品完整详情（goodId = 族 ID）' })
  @ApiParam({ name: 'goodId', type: String, example: '758', description: '产品族 ID' })
  @ApiOkResponse({ type: PublicGoodDetailDto })
  getGood(@Param('goodId') goodId: string): Promise<PublicGoodDetailDto> {
    return this.service.getGood(goodId);
  }

  @Get('home-goods')
  @ApiOperation({ summary: '获取首页商品；可按国家返回' })
  @ApiOkResponse({ type: [PublicGoodDto] })
  getHomeGoods(@Query() query: PublicHomeGoodsQueryDto): Promise<PublicGoodDto[]> {
    return this.service.getHomeGoods(query);
  }
}
