package repository

import (
	"fmt"
	"narasi-ahli-be/app/database"
	"narasi-ahli-be/app/database/entity"
	"narasi-ahli-be/app/module/ai_chat_files/request"
	"narasi-ahli-be/utils/paginator"
	utilSvc "narasi-ahli-be/utils/service"
	"strings"
)

type aiChatFilesRepository struct {
	DB *database.Database
}

// AiChatFilesRepository define interface of IAiChatFilesRepository
type AiChatFilesRepository interface {
	GetAll(req request.AiChatFilesQueryRequest) (aiChatFiles []*entity.AiChatFiles, paging paginator.Pagination, err error)
	FindOne(id uint) (aiChatFiles *entity.AiChatFiles, err error)
	FindByMessageId(messageId uint) (aiChatFiles []*entity.AiChatFiles, err error)
	FindByFilename(filename string) (aiChatFiles *entity.AiChatFiles, err error)
	Create(aiChatFiles *entity.AiChatFiles) (err error)
	Update(id uint, aiChatFiles *entity.AiChatFiles) (err error)
	Delete(id uint) (err error)

}

func NewAiChatFilesRepository(db *database.Database) AiChatFilesRepository {
	return &aiChatFilesRepository{
		DB: db,
	}
}

// implement interface of IAiChatFilesRepository
func (_i *aiChatFilesRepository) GetAll(req request.AiChatFilesQueryRequest) (aiChatFiles []*entity.AiChatFiles, paging paginator.Pagination, err error) {
	var count int64

	query := _i.DB.DB.Model(&entity.AiChatFiles{})
	query = query.Where("is_active = ?", true)

	if req.MessageId != nil {
		query = query.Where("message_id = ?", req.MessageId)
	}
	if req.FileName != nil && *req.FileName != "" {
		fileName := strings.ToLower(*req.FileName)
		query = query.Where("LOWER(file_name) LIKE ?", "%"+strings.ToLower(fileName)+"%")
	}
	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(&aiChatFiles).Error
	if err != nil {
		return
	}

	paging = *req.Pagination

	return
}

func (_i *aiChatFilesRepository) FindOne(id uint) (aiChatFiles *entity.AiChatFiles, err error) {
	query := _i.DB.DB

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

	return aiChatFiles, nil
}

func (_i *aiChatFilesRepository) FindByMessageId(
	messageId uint,
) (aiChatFiles []*entity.AiChatFiles, err error) {

	query := _i.DB.DB.Where("message_id = ?", messageId)

	if err := query.
		Order("created_at ASC").
		Find(&aiChatFiles).Error; err != nil {
		return nil, err
	}

	return aiChatFiles, nil
}

func (_i *aiChatFilesRepository) FindByFilename(aiChatFilename string) (aiChatFiles *entity.AiChatFiles, err error) {
	query := _i.DB.DB.Where("file_name = ?", aiChatFilename)

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

	return aiChatFiles, nil
}

func (_i *aiChatFilesRepository) Create(aiChatFiles *entity.AiChatFiles) (err error) {
	return _i.DB.DB.Create(aiChatFiles).Error
}

func (_i *aiChatFilesRepository) Update(id uint, aiChatFiles *entity.AiChatFiles) (err error) {
	aiChatFilesMap, err := utilSvc.StructToMap(aiChatFiles)
	if err != nil {
		return err
	}
	return _i.DB.DB.Model(&entity.AiChatFiles{}).
		Where(&entity.AiChatFiles{ID: id}).
		Updates(aiChatFilesMap).Error
}

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