package repository

import (
	"campaign-pool-be/app/database"
	"campaign-pool-be/app/database/entity"
	"campaign-pool-be/app/module/campaign_destinations/request"
	"campaign-pool-be/utils/paginator"
	utilSvc "campaign-pool-be/utils/service"
	"fmt"
	"strings"

	"github.com/rs/zerolog"
)

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

// CampaignDestinationsRepository define interface of ICampaignDestinationsRepository
type CampaignDestinationsRepository interface {
	GetAll(req request.CampaignDestinationsQueryRequest) (campaignDestinationss []*entity.CampaignDestinations, paging paginator.Pagination, err error)
	FindOne(id uint) (campaignDestinations *entity.CampaignDestinations, err error)
	Create(campaignDestinations *entity.CampaignDestinations) (campaignDestinationsReturn *entity.CampaignDestinations, err error)
	Update(id uint, campaignDestinations *entity.CampaignDestinations) (err error)
	Delete(id uint) (err error)
}

func NewCampaignDestinationsRepository(db *database.Database, log zerolog.Logger) CampaignDestinationsRepository {
	return &campaignDestinationsRepository{
		DB:  db,
		Log: log,
	}
}

// implement interface of ICampaignDestinationsRepository
func (_i *campaignDestinationsRepository) GetAll(req request.CampaignDestinationsQueryRequest) (campaignDestinationss []*entity.CampaignDestinations, paging paginator.Pagination, err error) {
	var count int64

	query := _i.DB.DB.Model(&entity.CampaignDestinations{}).Preload("CampaignType")

	if req.Name != nil && *req.Name != "" {
		name := strings.ToLower(*req.Name)
		query = query.Where("LOWER(name) LIKE ?", "%"+strings.ToLower(name)+"%")
	}
	if req.CampaignTypeID != nil {
		query = query.Where("campaign_type_id = ?", req.CampaignTypeID)
	}
	if req.IsActive != nil {
		query = query.Where("is_active = ?", req.IsActive)
	}
	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))
	} else {
		direction := "DESC"
		sortBy := "created_at"
		query.Order(fmt.Sprintf("%s %s", sortBy, direction))
	}

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

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

	paging = *req.Pagination

	return
}

func (_i *campaignDestinationsRepository) FindOne(id uint) (campaignDestinations *entity.CampaignDestinations, err error) {
	query := _i.DB.DB.Where("id = ?", id).Preload("CampaignType")

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

	return campaignDestinations, nil
}

func (_i *campaignDestinationsRepository) Create(campaignDestinations *entity.CampaignDestinations) (campaignDestinationsReturn *entity.CampaignDestinations, err error) {
	result := _i.DB.DB.Create(campaignDestinations)
	return campaignDestinations, result.Error
}

func (_i *campaignDestinationsRepository) Update(id uint, campaignDestinations *entity.CampaignDestinations) (err error) {
	campaignDestinationsMap, err := utilSvc.StructToMap(campaignDestinations)
	if err != nil {
		return err
	}
	query := _i.DB.DB.Model(&entity.CampaignDestinations{}).Where(&entity.CampaignDestinations{ID: id})
	return query.Updates(campaignDestinationsMap).Error
}

func (_i *campaignDestinationsRepository) Delete(id uint) error {
	return _i.DB.DB.Delete(&entity.CampaignDestinations{}, id).Error
}

