package repository

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

type HostRepository interface {
	GetHost(ctx context.Context, id int64) (*model.Host, error)
	GetHostConfig(ctx context.Context, hostId int) ([]*model.Host, error)
	GetProductConfigOption(ctx context.Context, id []int) ([]v1.ProductConfigOption, error)
	GetProductConfigOptionSub(ctx context.Context, id []int) ([]v1.ProductConfigOptionSub, error)
	GetDomainById(ctx context.Context, id int) (string, error)
	// 获取指定用户指定套餐的到期时间
	GetExpireTime(ctx context.Context, uid int64, hostId int64) (string, error)
	// 获取指定到期时间
	GetAlmostExpired(ctx context.Context, hostId []int,addTime int64) ([]v1.GetAlmostExpireHostResponse, error)
	// 获取到期时间区间
	GetExpireTimeRange(ctx context.Context,startTime int64,endTime int64) (int64, error)
	// 获取指定套餐的到期时间
	GetExpireTimeByHostId(ctx context.Context, hostIds []int) ([]v1.GetAlmostExpireHostResponse, error)
}

func NewHostRepository(
	repository *Repository,
) HostRepository {
	return &hostRepository{
		Repository: repository,
	}
}

type hostRepository struct {
	*Repository
}

func (r *hostRepository) GetHost(ctx context.Context, id int64) (*model.Host, error) {
	var host model.Host

	return &host, nil
}

func (r *hostRepository) GetHostConfig(ctx context.Context, hostId int) ([]*model.Host, error) {
	var res []*model.Host
	if err := r.DB(ctx).Where("relid = ?", hostId).Find(&res).Error; err != nil {
		return nil, err
	}
	return res, nil
}

func (r *hostRepository) GetProductConfigOption(ctx context.Context, id []int) ([]v1.ProductConfigOption, error) {
	var res []v1.ProductConfigOption
	if err := r.DB(ctx).Table("shd_product_config_options").Where("id IN ?", id).Find(&res).Error; err != nil {
		return nil, err
	}
	return res, nil
}

func (r *hostRepository) GetProductConfigOptionSub(ctx context.Context, id []int) ([]v1.ProductConfigOptionSub, error) {
	var res []v1.ProductConfigOptionSub
	if err := r.DB(ctx).Table("shd_product_config_options_sub").Where("id IN ?", id).Find(&res).Error; err != nil {
		return nil, err
	}
	return res, nil
}

func (r *hostRepository) GetDomainById(ctx context.Context, id int) (string, error) {
	var res string
	if err := r.DB(ctx).Table("shd_host").
		Where("id = ?", id).
		Pluck("domain", &res).Error; err != nil {
		return "", err
	}
	return res, nil
}

// 获取到期时间
func (r *hostRepository) GetExpireTime(ctx context.Context, uid int64, hostId int64) (string, error) {
	var nextDueDate string
	err := r.DB(ctx).Table("shd_host").
		Select("nextduedate").
		Where("id = ?", hostId).
		Where("uid = ?", uid).
		Scan(&nextDueDate).Error

	if err != nil {
		return "", err
	}

	return nextDueDate, nil
}

// 获取指定到期时间
func (r *hostRepository) GetAlmostExpired(ctx context.Context, hostId []int,addTime int64) ([]v1.GetAlmostExpireHostResponse, error) {
	var res []v1.GetAlmostExpireHostResponse
	expiredTime := time.Now().Unix() + addTime
	if err := r.DB(ctx).Table("shd_host").
		Where("id IN ?", hostId).
		Where("nextduedate < ?", expiredTime).
		Select("id", "nextduedate").
		Scan(&res).Error; err != nil {
		return nil, err
	}
	return res, nil
}


// 获取到期时间区间
func (r *hostRepository) GetExpireTimeRange(ctx context.Context,startTime int64,endTime int64) (int64, error) {
	var res int64
	if err := r.DB(ctx).Table("shd_host").
		Where("nextduedate > ?", startTime).
		Where("nextduedate < ?", endTime).
		Count(&res).Error; err != nil {
		return 0, err
	}
	return res, nil
}

// 获取指定套餐的到期时间
func (r *hostRepository) GetExpireTimeByHostId(ctx context.Context, hostIds []int) ([]v1.GetAlmostExpireHostResponse, error) {
	var res []v1.GetAlmostExpireHostResponse
	if err := r.DB(ctx).Table("shd_host").
		Where("id IN ?", hostIds).
		Select("id,nextduedate").
		Scan(&res).Error; err != nil {
		return nil, err
	}
	return res, nil
}