package repository

import (
	"fmt"
	"github.com/google/uuid"
	"github.com/rs/zerolog"
	"netidhub-saas-be/app/database"
	"netidhub-saas-be/app/database/entity"
	"netidhub-saas-be/app/module/subscription/request"
	"netidhub-saas-be/utils/paginator"
	"strings"
)

type subscriptionRepository struct {
	DB  *database.Database
	Log zerolog.Logger
}

// SubscriptionRepository define interface of ISubscriptionRepository
type SubscriptionRepository interface {
	GetAll(clientId *uuid.UUID, req request.SubscriptionQueryRequest) (subscriptions []*entity.Subscription, paging paginator.Pagination, err error)
	FindOne(clientId *uuid.UUID, id uint) (subscription *entity.Subscription, err error)
	Create(subscription *entity.Subscription) (subscriptionReturn *entity.Subscription, err error)
	Update(clientId *uuid.UUID, id uint, subscription *entity.Subscription) (err error)
	Delete(clientId *uuid.UUID, id uint) (err error)
}

func NewSubscriptionRepository(db *database.Database, logger zerolog.Logger) SubscriptionRepository {
	return &subscriptionRepository{
		DB:  db,
		Log: logger,
	}
}

// implement interface of ISubscriptionRepository
func (_i *subscriptionRepository) GetAll(clientId *uuid.UUID, req request.SubscriptionQueryRequest) (subscriptions []*entity.Subscription, paging paginator.Pagination, err error) {
	var count int64

	query := _i.DB.DB.Model(&entity.Subscription{})

	// Add ClientId filter
	if clientId != nil {
		query = query.Where("client_id = ?", clientId)
	}

	query = query.Where("is_active = ?", true)

	if req.Email != nil && *req.Email != "" {
		email := strings.ToLower(*req.Email)
		query = query.Where("LOWER(email) LIKE ?", "%"+strings.ToLower(email)+"%")
	}
	query.Count(&count)

	if req.Pagination.SortBy != "" {
		direction := "ASC"
		if req.Pagination.Sort == "desc" {
			direction = "DESC"
		}
		query.Order(fmt.Sprintf("%s %s", req.Pagination.SortBy, direction))
	}

	req.Pagination.Count = count
	req.Pagination = paginator.Paging(req.Pagination)

	err = query.Offset(req.Pagination.Offset).Limit(req.Pagination.Limit).Find(&subscriptions).Error
	if err != nil {
		return
	}

	paging = *req.Pagination

	return
}

func (_i *subscriptionRepository) FindOne(clientId *uuid.UUID, id uint) (subscription *entity.Subscription, err error) {
	query := _i.DB.DB.Where("id = ?", id)

	// Add ClientId filter
	if clientId != nil {
		query = query.Where("client_id = ?", clientId)
	}

	if err := query.First(&subscription).Error; err != nil {
		return nil, err
	}

	return subscription, nil
}

func (_i *subscriptionRepository) Create(subscription *entity.Subscription) (subscriptionReturn *entity.Subscription, err error) {
	result := _i.DB.DB.Create(subscription)
	return subscription, result.Error
}

func (_i *subscriptionRepository) Update(clientId *uuid.UUID, id uint, subscription *entity.Subscription) (err error) {
	query := _i.DB.DB.Model(&entity.Subscription{}).Where(&entity.Subscription{ID: id})
	if clientId != nil {
		query = query.Where("client_id = ?", clientId)
	}
	return query.Updates(subscription).Error
}

func (_i *subscriptionRepository) Delete(clientId *uuid.UUID, id uint) error {
	query := _i.DB.DB.Model(&entity.Subscription{}).Where("id = ?", id)
	if clientId != nil {
		query = query.Where("client_id = ?", clientId)
	}
	return query.Delete(&entity.Subscription{}).Error
}
