package guild

import (
	"context"
	"time"

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

func (s *service) CreateGuild(ctx context.Context, in CreateGuildInput) (*model.Guild, error) {
	g := &model.Guild{
		Name:         in.Name,
		OwnerID:      in.OwnerID,
		Description:  in.Description,
		JoinMode:     in.JoinMode,
		ConsumeLimit: in.ConsumeLimit,
		AvatarURL:    in.AvatarURL,
		IsOpen:       in.IsOpen,
		Status:       1,
	}
	if err := s.writeDB.Guild.WithContext(ctx).Create(g); err != nil {
		return nil, err
	}
	m := &model.GuildMembers{
		GuildID:    g.ID,
		UserID:     in.OwnerID,
		Role:       "owner",
		StartTime:  time.Now(),
		Status:     1,
		JoinStatus: 2,
	}
	if err := s.writeDB.GuildMembers.WithContext(ctx).Create(m); err != nil {
		return nil, err
	}
	return g, nil
}
