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/advertisement/request"
	"netidhub-saas-be/utils/paginator"
	utilSvc "netidhub-saas-be/utils/service"
	"strings"
)

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

// AdvertisementRepository define interface of IAdvertisementRepository
type AdvertisementRepository interface {
	GetAll(clientId *uuid.UUID, req request.AdvertisementQueryRequest) (advertisements []*entity.Advertisement, paging paginator.Pagination, err error)
	FindOne(clientId *uuid.UUID, id uint) (advertisement *entity.Advertisement, err error)
	FindByFilename(clientId *uuid.UUID, contentFilename string) (advertisement *entity.Advertisement, err error)
	Create(advertisement *entity.Advertisement) (advertisementReturn *entity.Advertisement, err error)
	Update(clientId *uuid.UUID, id uint, advertisement *entity.Advertisement) (err error)
	Delete(clientId *uuid.UUID, 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(clientId *uuid.UUID, req request.AdvertisementQueryRequest) (advertisements []*entity.Advertisement, paging paginator.Pagination, err error) {
	var count int64

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

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

	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(clientId *uuid.UUID, id uint) (advertisement *entity.Advertisement, 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(&advertisement).Error; err != nil {
		return nil, err
	}

	return advertisement, nil
}

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

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

	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(clientId *uuid.UUID, 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})
	if clientId != nil {
		query = query.Where("client_id = ?", clientId)
	}
	return query.Updates(advertisementMap).Error
}

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