package repository

import (
	"context"
	"github.com/go-nunu/nunu-layout-advanced/internal/model"
)

type GameShieldPublicIpRepository interface {
	GetGameShieldPublicIp(ctx context.Context, id int64) (*model.GameShieldPublicIp, error)
	GetGameShieldPublicUserIp(ctx context.Context, id int) (string, error)
	GetGameShieldPublicUserIpByNew(ctx context.Context) (model.GameShieldPublicIp, error)
	UpdateGameShieldPublicUserIp(ctx context.Context, id int, updateTime string) error
	GetGameShieldPublicUserIpByOld(ctx context.Context) (model.GameShieldPublicIp, error)
}

func NewGameShieldPublicIpRepository(
	repository *Repository,
) GameShieldPublicIpRepository {
	return &gameShieldPublicIpRepository{
		Repository: repository,
	}
}

type gameShieldPublicIpRepository struct {
	*Repository
}

func (r *gameShieldPublicIpRepository) GetGameShieldPublicIp(ctx context.Context, id int64) (*model.GameShieldPublicIp, error) {
	var gameShieldPublicIp model.GameShieldPublicIp

	return &gameShieldPublicIp, nil
}

func (r *gameShieldPublicIpRepository) GetGameShieldPublicUserIp(ctx context.Context, id int) (string, error) {
	var ip string
	if err := r.DB(ctx).Model(&model.GameShieldPublicIp{}).Select("ip").Where("id = ?", id).Scan(&ip).Error; err != nil {
		return "", err
	}
	return ip, nil
}

func (r *gameShieldPublicIpRepository) GetGameShieldPublicUserIpByNew(ctx context.Context) (model.GameShieldPublicIp, error) {
	var res model.GameShieldPublicIp
	if err := r.DB(ctx).Model(&model.GameShieldPublicIp{}).Select("ip").
		Where("update_time IS NULL").
		Order("id ASC").
		Scan(&res).Error; err != nil {
		return model.GameShieldPublicIp{}, err
	}
	return res, nil
}

func (r *gameShieldPublicIpRepository) UpdateGameShieldPublicUserIp(ctx context.Context, id int, updateTime string) error {
	if err := r.DB(ctx).Model(&model.GameShieldPublicIp{}).
		Where("id = ?", id).
		Update("update_time", updateTime).Error; err != nil {
		return err
	}
	return nil
}

func (r *gameShieldPublicIpRepository) GetGameShieldPublicUserIpByOld(ctx context.Context) (model.GameShieldPublicIp, error) {
	var res model.GameShieldPublicIp
	if err := r.DB(ctx).Model(&model.GameShieldPublicIp{}).
		Order("update_time ASC").
		Scan(&res).Error; err != nil {
		return model.GameShieldPublicIp{}, err
	}
	return res, nil
}
