package service

import (
	"errors"
	"opencatd-open/internal/dao"
	"opencatd-open/internal/model"
	"time"

	"gorm.io/gorm"
)

var _ ApiKeyService = (*ApiKeyServiceImpl)(nil)

type ApiKeyService interface {
	Create(apiKey *model.ApiKey) error
	GetByID(id int64) (*model.ApiKey, error)
	GetByName(name string) (*model.ApiKey, error)
	GetByApiKey(apiKeyValue string) (*model.ApiKey, error)
	Update(apiKey *model.ApiKey) error
	Delete(id int64) error
	List(limit, offset int, status string) ([]*model.ApiKey, error)
	ListWithFilters(limit, offset int, filters map[string]interface{}) ([]*model.ApiKey, int64, error)
	BatchEnable(ids []int64) error
	BatchDisable(ids []int64) error
	BatchDelete(ids []int64) error
	Count() (int64, error)
}

type ApiKeyServiceImpl struct {
	db         *gorm.DB
	apiKeyRepo dao.ApiKeyRepository
}

func NewApiKeyService(db *gorm.DB, apiKeyDao dao.ApiKeyRepository) ApiKeyService {
	return &ApiKeyServiceImpl{apiKeyRepo: apiKeyDao, db: db}
}

func (s *ApiKeyServiceImpl) Create(apiKey *model.ApiKey) error {
	if apiKey == nil {
		return errors.New("apiKey不能为空")
	}
	if apiKey.Name == nil {
		return errors.New("apiKey名称不能为空")
	}
	if apiKey.ApiKey == nil {
		return errors.New("apiKey值不能为空")
	}
	apiKey.CreatedAt = time.Now().Unix()
	apiKey.UpdatedAt = time.Now().Unix()

	return s.apiKeyRepo.Create(apiKey)
}

func (s *ApiKeyServiceImpl) GetByID(id int64) (*model.ApiKey, error) {
	if id <= 0 {
		return nil, errors.New("id 必须大于 0")
	}
	return s.apiKeyRepo.GetByID(id)
}

func (s *ApiKeyServiceImpl) GetByName(name string) (*model.ApiKey, error) {
	if name == "" {
		return nil, errors.New("name 不能为空")
	}
	return s.apiKeyRepo.GetByName(name)
}

func (s *ApiKeyServiceImpl) GetByApiKey(apiKeyValue string) (*model.ApiKey, error) {
	if apiKeyValue == "" {
		return nil, errors.New("apiKeyValue 不能为空")
	}
	return s.apiKeyRepo.GetByApiKey(apiKeyValue)
}

func (s *ApiKeyServiceImpl) Update(apiKey *model.ApiKey) error {
	if apiKey == nil {
		return errors.New("apiKey不能为空")
	}
	if apiKey.ID <= 0 {
		return errors.New("apiKey ID 必须大于 0")
	}
	return s.apiKeyRepo.Update(apiKey)
}

func (s *ApiKeyServiceImpl) Delete(id int64) error {
	if id <= 0 {
		return errors.New("id 必须大于 0")
	}
	return s.apiKeyRepo.BatchDelete([]int64{id})
}

func (s *ApiKeyServiceImpl) List(offset, limit int, status string) ([]*model.ApiKey, error) {
	if offset < 0 {
		offset = 0
	}
	if limit <= 0 {
		limit = 20 // 设置默认值
	}
	return s.apiKeyRepo.List(offset, limit, status)
}

func (s *ApiKeyServiceImpl) ListWithFilters(offset, limit int, filters map[string]interface{}) ([]*model.ApiKey, int64, error) {
	if offset < 0 {
		offset = 0
	}
	if limit <= 0 {
		limit = 20 // 设置默认值
	}

	return s.apiKeyRepo.ListWithFilters(offset, limit, filters)
}

func (s *ApiKeyServiceImpl) Enable(id int64) error {
	if id <= 0 {
		return errors.New("id 必须大于 0")
	}
	return s.apiKeyRepo.BatchEnable([]int64{id})
}

func (s *ApiKeyServiceImpl) Disable(id int64) error {
	if id <= 0 {
		return errors.New("id 必须大于 0")
	}
	return s.apiKeyRepo.BatchDisable([]int64{id})
}

func (s *ApiKeyServiceImpl) BatchEnable(ids []int64) error {
	if len(ids) == 0 {
		return errors.New("ids 不能为空")
	}
	return s.apiKeyRepo.BatchEnable(ids)
}

func (s *ApiKeyServiceImpl) BatchDisable(ids []int64) error {
	if len(ids) == 0 {
		return errors.New("ids 不能为空")
	}
	return s.apiKeyRepo.BatchDisable(ids)
}

func (s *ApiKeyServiceImpl) BatchDelete(ids []int64) error {
	if len(ids) == 0 {
		return errors.New("ids 不能为空")
	}
	return s.apiKeyRepo.BatchDelete(ids)
}

func (s *ApiKeyServiceImpl) Count() (int64, error) {
	return s.apiKeyRepo.Count()
}
