package app

import (
	"context"
	"strconv"

	"in-server/app/main/api/internal/svc"
	"in-server/app/main/api/internal/types"
	"in-server/app/main/model"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetAppConfigLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetAppConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetAppConfigLogic {
	return &GetAppConfigLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetAppConfigLogic) GetAppConfig() (resp *types.GetAppConfigResp, err error) {
	retentionDays := int64(0)
	cfg, cfgErr := l.svcCtx.QueryCleanupConfigModel.FindOneByConfigKey(l.ctx, "retention_days")
	if cfgErr == nil && cfg.Status == 1 {
		if v, parseErr := strconv.ParseInt(cfg.ConfigValue, 10, 64); parseErr == nil && v >= 0 {
			retentionDays = v
		}
	} else if cfgErr != nil && cfgErr != model.ErrNotFound {
		l.Errorf("获取清理配置失败: %v", cfgErr)
	}

	return &types.GetAppConfigResp{
		QueryRetentionDays:   retentionDays,
		WechatH5LoginEnabled: l.svcCtx.Config.WechatH5.Enabled,
	}, nil
}
