package repository

import (
	"fmt"
	"netidhub-saas-be/app/database"
	"netidhub-saas-be/app/database/entity"
	"netidhub-saas-be/app/module/magazine_files/request"
	"netidhub-saas-be/utils/paginator"
	utilSvc "netidhub-saas-be/utils/service"
	"strings"
)

type magazineFilesRepository struct {
	DB *database.Database
}

// MagazineFilesRepository define interface of IMagazineFilesRepository
type MagazineFilesRepository interface {
	GetAll(req request.MagazineFilesQueryRequest) (magazineFiless []*entity.MagazineFiles, paging paginator.Pagination, err error)
	FindOne(id uint) (magazineFiles *entity.MagazineFiles, err error)
	FindByMagazine(magazineId uint) (magazineFiles []*entity.MagazineFiles, err error)
	FindByFilename(filename string) (magazineFiles *entity.MagazineFiles, err error)
	Create(magazineFiles *entity.MagazineFiles) (err error)
	Update(id uint, magazineFiles *entity.MagazineFiles) (err error)
	Delete(id uint) (err error)
}

func NewMagazineFilesRepository(db *database.Database) MagazineFilesRepository {
	return &magazineFilesRepository{
		DB: db,
	}
}

// implement interface of IMagazineFilesRepository
func (_i *magazineFilesRepository) GetAll(req request.MagazineFilesQueryRequest) (magazineFiless []*entity.MagazineFiles, paging paginator.Pagination, err error) {
	var count int64

	query := _i.DB.DB.Model(&entity.MagazineFiles{})
	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.MagazineId != nil {
		query = query.Where("magazine_id = ?", req.MagazineId)
	}
	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(&magazineFiless).Error
	if err != nil {
		return
	}

	paging = *req.Pagination

	return
}

func (_i *magazineFilesRepository) FindByMagazine(magazineId uint) (magazineFiles []*entity.MagazineFiles, err error) {
	if err := _i.DB.DB.Where("magazine_id = ?", magazineId).Find(&magazineFiles).Error; err != nil {
		return nil, err
	}

	return magazineFiles, nil
}

func (_i *magazineFilesRepository) FindByFilename(magazineFilename string) (magazineFiles *entity.MagazineFiles, err error) {

	if err := _i.DB.DB.Where("file_name = ?", magazineFilename).First(&magazineFiles).Error; err != nil {
		return nil, err
	}

	return magazineFiles, nil
}

func (_i *magazineFilesRepository) FindOne(id uint) (magazineFiles *entity.MagazineFiles, err error) {
	if err := _i.DB.DB.First(&magazineFiles, id).Error; err != nil {
		return nil, err
	}

	return magazineFiles, nil
}

func (_i *magazineFilesRepository) Create(magazineFiles *entity.MagazineFiles) (err error) {
	return _i.DB.DB.Create(magazineFiles).Error
}

func (_i *magazineFilesRepository) Update(id uint, magazineFiles *entity.MagazineFiles) (err error) {
	magazineFilesMap, err := utilSvc.StructToMap(magazineFiles)
	if err != nil {
		return err
	}
	return _i.DB.DB.Model(&entity.MagazineFiles{}).
		Where(&entity.MagazineFiles{ID: id}).
		Updates(magazineFilesMap).Error
}

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