package repository

import (
	"fmt"
	"github.com/google/uuid"
	"netidhub-saas-be/app/database"
	"netidhub-saas-be/app/database/entity"
	"netidhub-saas-be/app/module/magazines/request"
	"netidhub-saas-be/utils/paginator"
	utilSvc "netidhub-saas-be/utils/service"
	"strings"
)

type magazinesRepository struct {
	DB *database.Database
}

// MagazinesRepository define interface of IMagazinesRepository
type MagazinesRepository interface {
	GetAll(clientId *uuid.UUID, req request.MagazinesQueryRequest) (magaziness []*entity.Magazines, paging paginator.Pagination, err error)
	FindOne(clientId *uuid.UUID, id uint) (magazines *entity.Magazines, err error)
	FindByFilename(clientId *uuid.UUID, thumbnailName string) (magazineReturn *entity.Magazines, err error)
	Create(magazines *entity.Magazines) (magazineReturn *entity.Magazines, err error)
	Update(clientId *uuid.UUID, id uint, magazines *entity.Magazines) (err error)
	Delete(clientId *uuid.UUID, id uint) (err error)
}

func NewMagazinesRepository(db *database.Database) MagazinesRepository {
	return &magazinesRepository{
		DB: db,
	}
}

// implement interface of IMagazinesRepository
func (_i *magazinesRepository) GetAll(clientId *uuid.UUID, req request.MagazinesQueryRequest) (magaziness []*entity.Magazines, paging paginator.Pagination, err error) {
	var count int64

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

	// 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.CreatedById != nil {
		query = query.Where("created_by_id = ?", req.CreatedById)
	}
	if req.IsPublish != nil {
		query = query.Where("is_publish = ?", req.IsPublish)
	}
	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(&magaziness).Error
	if err != nil {
		return
	}

	paging = *req.Pagination

	return
}

func (_i *magazinesRepository) FindOne(clientId *uuid.UUID, id uint) (magazines *entity.Magazines, 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(&magazines).Error; err != nil {
		return nil, err
	}

	return magazines, nil
}

func (_i *magazinesRepository) FindByFilename(clientId *uuid.UUID, thumbnailName string) (magazines *entity.Magazines, err error) {
	query := _i.DB.DB.Where("thumbnail_name = ?", thumbnailName)

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

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

	return magazines, nil
}

func (_i *magazinesRepository) Create(magazines *entity.Magazines) (magazineReturn *entity.Magazines, err error) {
	result := _i.DB.DB.Create(magazines)
	return magazines, result.Error
}

func (_i *magazinesRepository) Update(clientId *uuid.UUID, id uint, magazines *entity.Magazines) (err error) {
	magazinesMap, err := utilSvc.StructToMap(magazines)
	if err != nil {
		return err
	}
	query := _i.DB.DB.Model(&entity.Magazines{}).Where(&entity.Magazines{ID: id})
	if clientId != nil {
		query = query.Where("client_id = ?", clientId)
	}
	return query.Updates(magazinesMap).Error
}

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