package service

import (
	"github.com/google/uuid"
	"github.com/rs/zerolog"
	"netidhub-saas-be/app/database/entity"
	"netidhub-saas-be/app/module/subscription/mapper"
	"netidhub-saas-be/app/module/subscription/repository"
	"netidhub-saas-be/app/module/subscription/request"
	"netidhub-saas-be/app/module/subscription/response"
	usersRepository "netidhub-saas-be/app/module/users/repository"
	"netidhub-saas-be/utils/paginator"
)

// SubscriptionService
type subscriptionService struct {
	Repo      repository.SubscriptionRepository
	UsersRepo usersRepository.UsersRepository
	Log       zerolog.Logger
}

// SubscriptionService define interface of ISubscriptionService
type SubscriptionService interface {
	All(clientId *uuid.UUID, req request.SubscriptionQueryRequest) (subscription []*response.SubscriptionResponse, paging paginator.Pagination, err error)
	Show(clientId *uuid.UUID, id uint) (subscription *response.SubscriptionResponse, err error)
	Save(clientId *uuid.UUID, req request.SubscriptionCreateRequest) (subscription *entity.Subscription, err error)
	Update(clientId *uuid.UUID, id uint, req request.SubscriptionUpdateRequest) (err error)
	Delete(clientId *uuid.UUID, id uint) error
}

// NewSubscriptionService init SubscriptionService
func NewSubscriptionService(repo repository.SubscriptionRepository, log zerolog.Logger, usersRepo usersRepository.UsersRepository) SubscriptionService {

	return &subscriptionService{
		Repo:      repo,
		Log:       log,
		UsersRepo: usersRepo,
	}
}

// All implement interface of SubscriptionService
func (_i *subscriptionService) All(clientId *uuid.UUID, req request.SubscriptionQueryRequest) (subscriptions []*response.SubscriptionResponse, paging paginator.Pagination, err error) {
	results, paging, err := _i.Repo.GetAll(clientId, req)
	if err != nil {
		return
	}

	for _, result := range results {
		subscriptions = append(subscriptions, mapper.SubscriptionResponseMapper(result))
	}

	return
}

func (_i *subscriptionService) Show(clientId *uuid.UUID, id uint) (subscription *response.SubscriptionResponse, err error) {
	result, err := _i.Repo.FindOne(clientId, id)
	if err != nil {
		return nil, err
	}

	return mapper.SubscriptionResponseMapper(result), nil
}

func (_i *subscriptionService) Save(clientId *uuid.UUID, req request.SubscriptionCreateRequest) (subscription *entity.Subscription, err error) {
	_i.Log.Info().Interface("data", req).Msg("")
	newReq := req.ToEntity()

	// Set ClientId on entity
	newReq.ClientId = clientId

	return _i.Repo.Create(newReq)
}

func (_i *subscriptionService) Update(clientId *uuid.UUID, id uint, req request.SubscriptionUpdateRequest) (err error) {
	_i.Log.Info().Interface("data", req).Msg("")

	// Set ClientId on entity
	entity := req.ToEntity()
	entity.ClientId = clientId

	return _i.Repo.Update(clientId, id, entity)
}

func (_i *subscriptionService) Delete(clientId *uuid.UUID, id uint) error {
	result, err := _i.Repo.FindOne(clientId, id)
	if err != nil {
		return err
	}

	result.IsActive = false
	return _i.Repo.Update(clientId, id, result)
}
