package repository

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

	"github.com/rs/zerolog"
)

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

// AdvertisementRepository define interface of IAdvertisementRepository
type AdvertisementRepository interface {
	GetAll(req request.AdvertisementQueryRequest) (advertisements []*entity.Advertisement, paging paginator.Pagination, err error)
	FindOne(id uint) (advertisement *entity.Advertisement, err error)
	FindByFilename(contentFilename string) (advertisement *entity.Advertisement, err error)
	Create(advertisement *entity.Advertisement) (advertisementReturn *entity.Advertisement, err error)
	Update(id uint, advertisement *entity.Advertisement) (err error)
	Delete(id uint) (err error)
}

func NewAdvertisementRepository(db *database.Database, logger zerolog.Logger) AdvertisementRepository {
	return &advertisementRepository{
		DB:  db,
		Log: logger,
	}
}

// implement interface of IAdvertisementRepository
func (_i *advertisementRepository) GetAll(req request.AdvertisementQueryRequest) (advertisements []*entity.Advertisement, paging paginator.Pagination, err error) {
	var count int64

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

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

	if req.Title != nil && *req.Title != "" {
		title := strings.ToLower(*req.Title)
		query = query.Where("LOWER(title) LIKE ?", "%"+strings.ToLower(title)+"%")
	}
	if req.Description != nil && *req.Description != "" {
		description := strings.ToLower(*req.Description)
		query = query.Where("LOWER(description) LIKE ?", "%"+strings.ToLower(description)+"%")
	}
	if req.RedirectLink != nil && *req.RedirectLink != "" {
		redirectLink := strings.ToLower(*req.RedirectLink)
		query = query.Where("LOWER(redirect_link) LIKE ?", "%"+strings.ToLower(redirectLink)+"%")
	}
	if req.Placement != nil && *req.Placement != "" {
		placement := strings.ToLower(*req.Placement)
		query = query.Where("LOWER(placement) LIKE ?", "%"+strings.ToLower(placement)+"%")
	}
	if req.StatusId != nil {
		query = query.Where("status_id = ?", req.StatusId)
	}
	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(&advertisements).Error
	if err != nil {
		return
	}

	paging = *req.Pagination

	return
}

func (_i *advertisementRepository) FindOne(id uint) (advertisement *entity.Advertisement, err error) {
	query := _i.DB.DB.Where("id = ?", id)

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

	return advertisement, nil
}

func (_i *advertisementRepository) FindByFilename(contentFilename string) (advertisement *entity.Advertisement, err error) {
	query := _i.DB.DB.Where("content_file_name = ?", contentFilename)

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

	return advertisement, nil
}

func (_i *advertisementRepository) Create(advertisement *entity.Advertisement) (advertisementReturn *entity.Advertisement, err error) {
	result := _i.DB.DB.Create(advertisement)
	return advertisement, result.Error
}

func (_i *advertisementRepository) Update(id uint, advertisement *entity.Advertisement) (err error) {
	advertisementMap, err := utilSvc.StructToMap(advertisement)
	if err != nil {
		return err
	}
	query := _i.DB.DB.Model(&entity.Advertisement{}).Where(&entity.Advertisement{ID: id})
	return query.Updates(advertisementMap).Error
}

func (_i *advertisementRepository) Delete(id uint) error {
	query := _i.DB.DB.Model(&entity.Advertisement{}).Where("id = ?", id)
	return query.Delete(&entity.Advertisement{}).Error
}
