//go:build wireinject
// +build wireinject

package wire

import (
	"context"
	"opencatd-open/internal/controller"
	proxy "opencatd-open/internal/controller/proxy"
	team_controller "opencatd-open/internal/controller/team"
	"opencatd-open/internal/dao"
	"opencatd-open/pkg/config"
	"sync"

	service "opencatd-open/internal/service"
	teamService "opencatd-open/internal/service/team"

	"github.com/google/wire"
	"gorm.io/gorm"
)

// 定义 provider set
var userSet = wire.NewSet(
	dao.NewUserDAO,
	wire.Bind(new(dao.UserRepository), new(*dao.UserDAO)),
	teamService.NewUserService,
)

var keySet = wire.NewSet(
	dao.NewApiKeyDAO,
	wire.Bind(new(dao.ApiKeyRepository), new(*dao.ApiKeyDAO)),
	teamService.NewApiKeyService,
)

var tokenSet = wire.NewSet(
	dao.NewTokenDAO,
	wire.Bind(new(dao.TokenRepository), new(*dao.TokenDAO)),
	teamService.NewTokenService,
)

var usageSet = wire.NewSet(
	dao.NewUsageDAO,
	wire.Bind(new(dao.UsageRepository), new(*dao.UsageDAO)),
	dao.NewDailyUsageDAO,
	wire.Bind(new(dao.DailyUsageRepository), new(*dao.DailyUsageDAO)),
	teamService.NewUsageService,
)

// 初始化 TeamHandler
func InitTeamHandler(ctx context.Context, cfg *config.Config, db *gorm.DB) (*team_controller.Team, error) {
	wire.Build(userSet, keySet, tokenSet, usageSet, team_controller.NewTeam)
	return nil, nil
}

// var userApi = wire.NewSet(
// 	dao.NewUserDAO,
// 	wire.Bind(new(dao.UserRepository), new(*dao.UserDAO)),
// 	service.NewUserService,
// )

// var keyApi = wire.NewSet(
// 	dao.NewApiKeyDAO,
// 	wire.Bind(new(dao.ApiKeyRepository), new(*dao.ApiKeyDAO)),
// 	service.NewApiKeyService,
// )

// var tokenApi = wire.NewSet(
// 	dao.NewTokenDAO,
// 	wire.Bind(new(dao.TokenRepository), new(*dao.TokenDAO)),
// 	service.NewTokenService,
// )

// func InitAPIHandler(ctx context.Context, db *gorm.DB) (*controller.Api, error) {
// 	wire.Build(userApi, keyApi, tokenApi, controller.NewApi)
// 	return nil, nil
// }

var repositorySet = wire.NewSet(
	dao.NewUserDAO,
	wire.Bind(new(dao.UserRepository), new(*dao.UserDAO)),
	dao.NewTokenDAO,
	wire.Bind(new(dao.TokenRepository), new(*dao.TokenDAO)),
	dao.NewApiKeyDAO,
	wire.Bind(new(dao.ApiKeyRepository), new(*dao.ApiKeyDAO)),

	dao.NewUsageDAO,
	wire.Bind(new(dao.UsageRepository), new(*dao.UsageDAO)),
	dao.NewDailyUsageDAO,
	wire.Bind(new(dao.DailyUsageRepository), new(*dao.DailyUsageDAO)),
)

var serviceSet = wire.NewSet(
	service.NewUserService,
	service.NewTokenService,
	service.NewApiKeyService,
	service.NewWebAuthnService,

	service.NewUsageService,
)

func InitAPIHandler(ctx context.Context, cfg *config.Config, db *gorm.DB) (*controller.Api, error) {
	wire.Build(
		repositorySet,
		serviceSet,
		controller.NewApi,
	)
	return nil, nil
}

var proxySet = wire.NewSet(
	dao.NewUserDAO,
	dao.NewApiKeyDAO,
	dao.NewTokenDAO,
	dao.NewUsageDAO,
	dao.NewDailyUsageDAO,
)

func InitProxyHandler(ctx context.Context, cfg *config.Config, db *gorm.DB, wg *sync.WaitGroup) (*proxy.Proxy, error) {
	wire.Build(proxySet, proxy.NewProxy)
	return nil, nil
}
