package logic

import (
	"context"

	"github.com/zeromicro/go-zero/core/logx"
	"sundynix-micro-go/app/file/model"
	"sundynix-micro-go/app/file/rpc/file"
	"sundynix-micro-go/app/file/rpc/internal/svc"
)

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

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

func (l *GetDefaultStorageConfigLogic) GetDefaultStorageConfig(in *file.GetDefaultStorageConfigReq) (*file.GetDefaultStorageConfigResp, error) {
	conf := &model.StorageConfig{}
	err := l.svcCtx.DB.Where("is_default = ?", 1).First(conf).Error
	if err != nil {
		return nil, err // let the factory handle the error
	}

	return &file.GetDefaultStorageConfigResp{
		Config: &file.StorageConfigInfo{
			Id:              conf.ID,
			Type:            conf.Type,
			Name:            conf.Name,
			Endpoint:        conf.Endpoint,
			AccessKeyId:     conf.AccessKeyId,
			AccessKeySecret: conf.AccessKeySecret,
			BucketName:      conf.BucketName,
			BucketUrl:       conf.BucketUrl,
			Region:          conf.Region,
			IsDefault:       int32(conf.IsDefault),
			Status:          int32(conf.Status),
			Remark:          conf.Remark,
		},
	}, nil
}
