import { BadRequestException, Controller, Get, Param, Query } from '@nestjs/common';
import { CurrencyService } from './services/currency.service';
import { CountryService } from './services/country.service';
import { GeoRegionService } from './services/geo-region.service';
import { UnitOfMeasureService } from './services/unit-of-measure.service';
import { DictionaryService } from './services/dictionary.service';

/**
 * Platform Master API — PR1 范围 (L1b 字典)
 *
 * 只读 API：所有字典都是 seed 数据，CRUD 在 PR2+ 加管理页（按需）。
 */
@Controller('platform-master')
export class PlatformMasterController {
  constructor(
    private readonly currency: CurrencyService,
    private readonly country: CountryService,
    private readonly geoRegion: GeoRegionService,
    private readonly uom: UnitOfMeasureService,
    private readonly dict: DictionaryService,
  ) {}

  @Get('currencies')
  listCurrencies(@Query('enabledOnly') enabledOnly?: string) {
    return this.currency.list({ enabledOnly: enabledOnly !== 'false' });
  }

  @Get('currencies/:code')
  getCurrency(@Param('code') code: string) {
    return this.currency.findByCode(code);
  }

  @Get('countries')
  listCountries(@Query('region') region?: string) {
    return this.country.list(region);
  }

  @Get('countries/:code')
  getCountry(@Param('code') code: string) {
    return this.country.findByCode(code);
  }

  @Get('geo-regions')
  listGeoRegions() {
    return this.geoRegion.list();
  }

  @Get('geo-regions/:code')
  getGeoRegion(@Param('code') code: string) {
    return this.geoRegion.findByCode(code);
  }

  @Get('units-of-measure')
  listUoM(@Query('category') category?: string) {
    return this.uom.list(category);
  }

  @Get('units-of-measure/:code')
  getUoM(@Param('code') code: string) {
    return this.uom.findByCode(code);
  }

  @Get('dictionaries/categories')
  listDictCategories() {
    return this.dict.listCategories();
  }

  @Get('dictionaries')
  listDictByCategory(@Query('category') category: string) {
    if (!category || !category.trim()) {
      throw new BadRequestException('category is required (e.g. label_type / tariff_type / declaration_type / service_issue_type / industry)');
    }
    return this.dict.listByCategory(category);
  }

  @Get('dictionaries/:category/:code')
  getDictItem(@Param('category') category: string, @Param('code') code: string) {
    return this.dict.findByCode(category, code);
  }
}
