package repository

import (
	"context"

	"apskel-pos-be/internal/entities"

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

type ProductRepositoryImpl struct {
	db *gorm.DB
}

func NewProductRepositoryImpl(db *gorm.DB) *ProductRepositoryImpl {
	return &ProductRepositoryImpl{
		db: db,
	}
}

func (r *ProductRepositoryImpl) Create(ctx context.Context, product *entities.Product) error {
	return r.db.WithContext(ctx).Create(product).Error
}

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

func (r *ProductRepositoryImpl) GetWithCategory(ctx context.Context, id uuid.UUID) (*entities.Product, error) {
	var product entities.Product
	err := r.db.WithContext(ctx).Preload("Category").Preload("ProductVariants").First(&product, "id = ?", id).Error
	if err != nil {
		return nil, err
	}
	return &product, nil
}

func (r *ProductRepositoryImpl) GetWithRelations(ctx context.Context, id uuid.UUID) (*entities.Product, error) {
	var product entities.Product
	err := r.db.WithContext(ctx).
		Preload("Category").
		Preload("ProductVariants").
		Preload("Inventory").
		First(&product, "id = ?", id).Error
	if err != nil {
		return nil, err
	}
	return &product, nil
}

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

func (r *ProductRepositoryImpl) GetByCategory(ctx context.Context, categoryID uuid.UUID) ([]*entities.Product, error) {
	var products []*entities.Product
	err := r.db.WithContext(ctx).Where("category_id = ?", categoryID).Find(&products).Error
	return products, err
}

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

func (r *ProductRepositoryImpl) GetActiveByCategoryID(ctx context.Context, categoryID uuid.UUID) ([]*entities.Product, error) {
	var products []*entities.Product
	err := r.db.WithContext(ctx).Where("category_id = ? AND is_active = ?", categoryID, true).Find(&products).Error
	return products, err
}

func (r *ProductRepositoryImpl) Update(ctx context.Context, product *entities.Product) error {
	return r.db.WithContext(ctx).Save(product).Error
}

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

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

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

	for key, value := range filters {
		switch key {
		case "search":
			searchValue := "%" + value.(string) + "%"
			query = query.Where("name ILIKE ? OR description ILIKE ? OR sku ILIKE ?", searchValue, searchValue, searchValue)
		case "price_min":
			query = query.Where("price >= ?", value)
		case "price_max":
			query = query.Where("price <= ?", value)
		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(&products).Error
	return products, total, err
}

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

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

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

func (r *ProductRepositoryImpl) GetBySKU(ctx context.Context, organizationID uuid.UUID, sku string) (*entities.Product, error) {
	var product entities.Product
	err := r.db.WithContext(ctx).Where("organization_id = ? AND sku = ?", organizationID, sku).First(&product).Error
	if err != nil {
		return nil, err
	}
	return &product, nil
}

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

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

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

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

func (r *ProductRepositoryImpl) ExistsByName(ctx context.Context, organizationID uuid.UUID, name string, excludeID *uuid.UUID) (bool, error) {
	query := r.db.WithContext(ctx).Model(&entities.Product{}).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
}

func (r *ProductRepositoryImpl) UpdateActiveStatus(ctx context.Context, id uuid.UUID, isActive bool) error {
	return r.db.WithContext(ctx).Model(&entities.Product{}).
		Where("id = ?", id).
		Update("is_active", isActive).Error
}

func (r *ProductRepositoryImpl) GetLowCostProducts(ctx context.Context, organizationID uuid.UUID, maxCost float64) ([]*entities.Product, error) {
	var products []*entities.Product
	err := r.db.WithContext(ctx).Where("organization_id = ? AND cost <= ? AND is_active = ?", organizationID, maxCost, true).Find(&products).Error
	return products, err
}
