package guild

import (
	"context"

	"bindbox-game/internal/pkg/logger"
	"bindbox-game/internal/repository/mysql"
	"bindbox-game/internal/repository/mysql/dao"
	"bindbox-game/internal/repository/mysql/model"
)

type Service interface {
	CreateGuild(ctx context.Context, in CreateGuildInput) (*model.Guild, error)
	ModifyGuild(ctx context.Context, id int64, in ModifyGuildInput) error
	DeleteGuild(ctx context.Context, id int64) error
	GetGuild(ctx context.Context, id int64) (*model.Guild, error)
	ListGuilds(ctx context.Context, in ListGuildsInput) (items []*model.Guild, total int64, err error)
	ListMembers(ctx context.Context, guildID int64, page, pageSize int) (items []*model.GuildMembers, total int64, err error)
	JoinGuild(ctx context.Context, guildID int64, in JoinGuildInput) error
	LeaveGuild(ctx context.Context, guildID int64, userID int64) error
}

type service struct {
	logger  logger.CustomLogger
	readDB  *dao.Query
	writeDB *dao.Query
}

func New(l logger.CustomLogger, db mysql.Repo) Service {
	return &service{logger: l, readDB: dao.Use(db.GetDbR()), writeDB: dao.Use(db.GetDbW())}
}

type CreateGuildInput struct {
	Name         string
	OwnerID      int64
	Description  string
	JoinMode     int32
	ConsumeLimit int64
	AvatarURL    string
	IsOpen       int32
}

type ModifyGuildInput struct {
	Name         string
	Description  string
	JoinMode     int32
	ConsumeLimit int64
	AvatarURL    string
	IsOpen       int32
	Status       int32
}

type ListGuildsInput struct {
	Name     string
	IsOpen   *int32
	Status   *int32
	JoinMode *int32
	Page     int
	PageSize int
}

type JoinGuildInput struct {
	UserID int64
}
