package repository

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

	"github.com/rs/zerolog"
)

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

// CampaignsRepository define interface of ICampaignsRepository
type CampaignsRepository interface {
	GetAll(req request.CampaignsQueryRequest) (campaignss []*entity.Campaigns, paging paginator.Pagination, err error)
	FindOne(id uint) (campaigns *entity.Campaigns, err error)
	Create(campaigns *entity.Campaigns) (campaignsReturn *entity.Campaigns, err error)
	Update(id uint, campaigns *entity.Campaigns) (err error)
	Delete(id uint) (err error)
}

func NewCampaignsRepository(db *database.Database, log zerolog.Logger) CampaignsRepository {
	return &campaignsRepository{
		DB:  db,
		Log: log,
	}
}

// implement interface of ICampaignsRepository
func (_i *campaignsRepository) GetAll(req request.CampaignsQueryRequest) (campaignss []*entity.Campaigns, paging paginator.Pagination, err error) {
	var count int64

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

	if req.Title != nil && *req.Title != "" {
		title := strings.ToLower(*req.Title)
		query = query.Where("LOWER(title) LIKE ?", "%"+strings.ToLower(title)+"%")
	}
	if req.CampaignTypeID != nil {
		query = query.Where("campaign_type_id = ?", req.CampaignTypeID)
	}
	if req.Status != nil && *req.Status != "" {
		query = query.Where("status = ?", req.Status)
	}
	if req.CreatorID != nil {
		query = query.Where("creator_id = ?", req.CreatorID)
	}
	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(&campaignss).Error
	if err != nil {
		return
	}

	paging = *req.Pagination

	return
}

func (_i *campaignsRepository) FindOne(id uint) (campaigns *entity.Campaigns, err error) {
	query := _i.DB.DB.Where("id = ?", id).Preload("CampaignType").Preload("Creator")

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

	return campaigns, nil
}

func (_i *campaignsRepository) Create(campaigns *entity.Campaigns) (campaignsReturn *entity.Campaigns, err error) {
	result := _i.DB.DB.Create(campaigns)
	return campaigns, result.Error
}

func (_i *campaignsRepository) Update(id uint, campaigns *entity.Campaigns) (err error) {
	campaignsMap, err := utilSvc.StructToMap(campaigns)
	if err != nil {
		return err
	}
	query := _i.DB.DB.Model(&entity.Campaigns{}).Where(&entity.Campaigns{ID: id})
	return query.Updates(campaignsMap).Error
}

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

