package repository

import (
	"context"

	"apskel-pos-be/internal/entities"

	"github.com/google/uuid"
	"gorm.io/gorm"
)

type CategoryRepositoryImpl struct {
	db *gorm.DB
}

func NewCategoryRepositoryImpl(db *gorm.DB) *CategoryRepositoryImpl {
	return &CategoryRepositoryImpl{
		db: db,
	}
}

func (r *CategoryRepositoryImpl) Create(ctx context.Context, category *entities.Category) error {
	return r.db.WithContext(ctx).Create(category).Error
}

func (r *CategoryRepositoryImpl) GetByID(ctx context.Context, id uuid.UUID) (*entities.Category, error) {
	var category entities.Category
	err := r.db.WithContext(ctx).First(&category, "id = ?", id).Error
	if err != nil {
		return nil, err
	}
	return &category, nil
}

func (r *CategoryRepositoryImpl) GetWithProducts(ctx context.Context, id uuid.UUID) (*entities.Category, error) {
	var category entities.Category
	err := r.db.WithContext(ctx).Preload("Products").First(&category, "id = ?", id).Error
	if err != nil {
		return nil, err
	}
	return &category, nil
}

func (r *CategoryRepositoryImpl) GetByOrganization(ctx context.Context, organizationID uuid.UUID) ([]*entities.Category, error) {
	var categories []*entities.Category
	err := r.db.WithContext(ctx).Where("organization_id = ?", organizationID).Find(&categories).Error
	return categories, err
}

func (r *CategoryRepositoryImpl) GetByBusinessType(ctx context.Context, businessType string) ([]*entities.Category, error) {
	var categories []*entities.Category
	err := r.db.WithContext(ctx).Where("business_type = ?", businessType).Find(&categories).Error
	return categories, err
}

func (r *CategoryRepositoryImpl) Update(ctx context.Context, category *entities.Category) error {
	return r.db.WithContext(ctx).Save(category).Error
}

func (r *CategoryRepositoryImpl) Delete(ctx context.Context, id uuid.UUID) error {
	return r.db.WithContext(ctx).Delete(&entities.Category{}, "id = ?", id).Error
}

func (r *CategoryRepositoryImpl) List(ctx context.Context, filters map[string]interface{}, limit, offset int) ([]*entities.Category, int64, error) {
	var categories []*entities.Category
	var total int64

	query := r.db.WithContext(ctx).Model(&entities.Category{})

	for key, value := range filters {
		switch key {
		case "search":
			searchValue := "%" + value.(string) + "%"
			query = query.Where("name ILIKE ? OR description ILIKE ?", searchValue, searchValue)
		default:
			query = query.Where(key+" = ?", value)
		}
	}

	if err := query.Count(&total).Error; err != nil {
		return nil, 0, err
	}

	err := query.Limit(limit).Offset(offset).Find(&categories).Error
	return categories, total, err
}

func (r *CategoryRepositoryImpl) Count(ctx context.Context, filters map[string]interface{}) (int64, error) {
	var count int64
	query := r.db.WithContext(ctx).Model(&entities.Category{})

	for key, value := range filters {
		switch key {
		case "search":
			searchValue := "%" + value.(string) + "%"
			query = query.Where("name ILIKE ? OR description ILIKE ?", searchValue, searchValue)
		default:
			query = query.Where(key+" = ?", value)
		}
	}

	err := query.Count(&count).Error
	return count, err
}

func (r *CategoryRepositoryImpl) GetByName(ctx context.Context, organizationID uuid.UUID, name string) (*entities.Category, error) {
	var category entities.Category
	err := r.db.WithContext(ctx).Where("organization_id = ? AND name = ?", organizationID, name).First(&category).Error
	if err != nil {
		return nil, err
	}
	return &category, nil
}

func (r *CategoryRepositoryImpl) ExistsByName(ctx context.Context, organizationID uuid.UUID, name string, excludeID *uuid.UUID) (bool, error) {
	query := r.db.WithContext(ctx).Model(&entities.Category{}).Where("organization_id = ? AND name = ?", organizationID, name)

	if excludeID != nil {
		query = query.Where("id != ?", *excludeID)
	}

	var count int64
	err := query.Count(&count).Error
	return count > 0, err
}
