package waf

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

type AllowAndDenyIpRepository interface {
	GetAllowAndDenyIp(ctx context.Context, id int64) (*model.AllowAndDenyIp, error)
	AddAllowAndDenyIps(ctx context.Context, req model.AllowAndDenyIp) (int, error)
	EditAllowAndDenyIps(ctx context.Context, req model.AllowAndDenyIp) error
	DeleteAllowAndDenyIps(ctx context.Context, id int64) error
	GetAllowAndDenyIpsAllByHostId(ctx context.Context, hostId int64) ([]*model.AllowAndDenyIp, error)
	GetIpCount(ctx context.Context,hostId int64,ip string) (int64, error)
	GetIpCountListId(ctx context.Context,hostId int64) ([]int, error)
}

func NewAllowAndDenyIpRepository(
	repository *repository.Repository,
) AllowAndDenyIpRepository {
	return &allowAndDenyIpRepository{
		Repository: repository,
	}
}

type allowAndDenyIpRepository struct {
	*repository.Repository
}

func (r *allowAndDenyIpRepository) GetAllowAndDenyIp(ctx context.Context, id int64) (*model.AllowAndDenyIp, error) {
	var res model.AllowAndDenyIp
	if err := r.DB(ctx).Where("id = ?", id).First(&res).Error; err != nil {
		return nil, err
	}
	return &res, nil
}

func (r *allowAndDenyIpRepository) AddAllowAndDenyIps(ctx context.Context, req model.AllowAndDenyIp) (int, error) {
	if err := r.Db.WithContext(ctx).Create(&req).Error; err != nil {
		return 0, fmt.Errorf("create error: %v", err)
	}
	return req.Id, nil
}

func (r *allowAndDenyIpRepository) EditAllowAndDenyIps(ctx context.Context, req model.AllowAndDenyIp) error {
	allowAndDenyIp := map[string]interface{}{
		"allow_or_deny": req.AllowOrDeny,
	}
	if err := r.Db.WithContext(ctx).Where("id = ?", req.Id).Updates(&req).Updates(allowAndDenyIp).Error; err != nil {
		return fmt.Errorf("update error: %v", err)
	}
	return nil
}

func (r *allowAndDenyIpRepository) DeleteAllowAndDenyIps(ctx context.Context, id int64) error {
	if err := r.Db.WithContext(ctx).Where("id = ?", id).Delete(&model.AllowAndDenyIp{}).Error; err != nil {
		return fmt.Errorf("delete error: %v", err)
	}
	return nil
}

func (r *allowAndDenyIpRepository) GetAllowAndDenyIpsAllByHostId(ctx context.Context, hostId int64) ([]*model.AllowAndDenyIp, error) {
	var res []*model.AllowAndDenyIp
	if err := r.DB(ctx).Model(&model.AllowAndDenyIp{}).Where("host_id = ?", hostId).Find(&res).Error; err != nil {
		return nil, err
	}
	return res, nil
}

func (r *allowAndDenyIpRepository) GetIpCount(ctx context.Context,hostId int64,ip string) (int64, error) {
	var count int64
	if err := r.DB(ctx).Model(&model.AllowAndDenyIp{}).Where("host_id = ?", hostId).Where("ip = ?", ip).Count(&count).Error; err != nil {
		return 0, err
	}
	return count, nil
}

func (r *allowAndDenyIpRepository) GetIpCountListId(ctx context.Context,hostId int64) ([]int, error) {
	var res []int
	if err := r.DB(ctx).Model(&model.AllowAndDenyIp{}).Where("host_id = ?", hostId).Pluck("id", &res).Error; err != nil {
		return nil, err
	}
	return res, nil
}