package data

import (
	"context"

	pb "bsm/full/module/base/initial/pb"
	"git.apinb.com/bsm-sdk/core/errcode"
	"git.apinb.com/bsm-sdk/core/printer"
)

func Country(ctx context.Context, in *pb.Empty) (reply *pb.CountryReply, err error) {
	// 查询启用的国家，按排序字段和名称排序
	countries, err := GetCountryByCache()
	if err != nil {
		printer.Error("GetCountryByCache", err)
		return nil, errcode.ErrDB
	}

	// 转换为响应格式
	var data []*pb.CountryItem
	for _, country := range countries {
		data = append(data, &pb.CountryItem{
			Id:             uint32(country.Id),
			Iso2:           country.Iso2,
			Iso3:           country.Iso3,
			NumCode:        country.NumCode,
			PhoneCode:      country.PhoneCode,
			Currency:       country.Currency,
			CurrencySymbol: country.CurrencySymbol,
			Region:         country.Region,
			Timezones:      country.Timezones,
			Translations:   country.Translations,
			Name:           country.Name,
			LocalName:      country.LocalName,
			Enabled:        country.Enabled,
			SortOrder:      country.SortOrder,
		})
	}

	return &pb.CountryReply{
		Countries: data,
	}, nil
}
