package guild

import (
	"context"
	"errors"
	"time"

	"gorm.io/gorm"

	"bindbox-game/internal/repository/mysql/model"
)

func (s *service) JoinGuild(ctx context.Context, guildID int64, in JoinGuildInput) error {
	lastExit, err := s.readDB.GuildMembers.WithContext(ctx).
		Where(s.readDB.GuildMembers.UserID.Eq(in.UserID)).
		Where(s.readDB.GuildMembers.Status.Eq(2)).
		Order(s.readDB.GuildMembers.ID.Desc()).
		Limit(1).Find()
	if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
		return err
	}
	if len(lastExit) > 0 {
		exitAt := lastExit[0].StartTime
		if time.Since(exitAt) < 24*time.Hour {
			return errors.New("recently left a guild, join limited within 24h")
		}
	}
	exists, err := s.readDB.GuildMembers.WithContext(ctx).
		Where(s.readDB.GuildMembers.GuildID.Eq(guildID)).
		Where(s.readDB.GuildMembers.UserID.Eq(in.UserID)).
		Where(s.readDB.GuildMembers.Status.Eq(1)).Count()
	if err != nil {
		return err
	}
	if exists > 0 {
		return errors.New("already a member")
	}

	m := &model.GuildMembers{
		GuildID:    guildID,
		UserID:     in.UserID,
		Role:       "member",
		StartTime:  time.Now(),
		Status:     1,
		JoinStatus: 2,
	}
	return s.writeDB.GuildMembers.WithContext(ctx).Create(m)
}
