// Package impl is a implementation package for kratos.
package impl

import (
	"context"

	"github.com/google/wire"

	"github.com/aide-family/moon/cmd/palace/internal/data/query/eventgen"
	"github.com/aide-family/moon/cmd/palace/internal/data/query/systemgen"
	"github.com/aide-family/moon/cmd/palace/internal/data/query/teamgen"
	"github.com/aide-family/moon/pkg/merr"
	"github.com/aide-family/moon/pkg/middler/permission"
	"github.com/aide-family/moon/pkg/plugin/gorm"
)

// ProviderSetImpl is a set of providers.
var ProviderSetImpl = wire.NewSet(
	NewUserRepo,
	NewMemberRepo,
	NewCaptchaRepo,
	NewCacheRepo,
	NewOAuthRepo,
	NewTransaction,
	NewTeamRepo,
	NewDashboardRepo,
	NewDashboardChartRepo,
	NewServerRepo,
	NewTeamDictRepo,
	NewTeamHook,
	NewTeamConfigEmailRepo,
	NewTeamConfigSMSRepo,
	NewMenuRepo,
	NewTeamRole,
	NewRoleRepo,
	NewAuditRepo,
	NewOperateLogRepo,
	NewInviteRepo,
	NewTeamNotice,
	NewTeamMetricDatasourceRepo,
	NewTeamStrategyGroupRepo,
	NewTeamStrategyRepo,
	NewTeamStrategyMetricRepo,
	NewSendMessageLog,
	NewSendMessageRepo,
	NewRealtime,
	NewTeamDatasourceMetricMetadataRepo,
	NewTimeEngineRule,
	NewTimeEngine,
	NewTeamStrategyMetricLevelRepo,
	NewEventBus,
)

type MainDB interface {
	GetMainDB() gorm.DB
}

type BizDB interface {
	GetBizDB(teamID uint32) (gorm.DB, error)
}

type EventDB interface {
	GetEventDB(teamID uint32) (gorm.DB, error)
}

func getMainQuery(ctx context.Context, m MainDB) *systemgen.Query {
	db := GetMainDBTransaction(ctx, m)
	return systemgen.Use(db)
}

func getTeamBizQueryWithTeamID(ctx context.Context, b BizDB) (*teamgen.Query, uint32) {
	teamID, ok := permission.GetTeamIDByContext(ctx)
	if !ok {
		panic(merr.ErrorPermissionDenied("team id not found"))
	}
	bizTranceDB := GetBizTransactionDB(ctx, b)
	return teamgen.Use(bizTranceDB), teamID
}

func getTeamBizQuery(ctx context.Context, b BizDB) *teamgen.Query {
	query, _ := getTeamBizQueryWithTeamID(ctx, b)
	return query
}

func getTeamEventQueryWithTeamID(ctx context.Context, e EventDB) (*eventgen.Query, uint32) {
	teamID, ok := permission.GetTeamIDByContext(ctx)
	if !ok {
		panic(merr.ErrorPermissionDenied("team id not found"))
	}
	eventTranceDB := GetEventDBTransaction(ctx, e)
	return eventgen.Use(eventTranceDB), teamID
}

func getTeamEventQuery(ctx context.Context, e EventDB) *eventgen.Query {
	query, _ := getTeamEventQueryWithTeamID(ctx, e)
	return query
}
