package repository

import (
	"context"

	"apskel-pos-be/internal/entities"

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

type PaymentMethodRepository interface {
	Create(ctx context.Context, paymentMethod *entities.PaymentMethod) error
	GetByID(ctx context.Context, id uuid.UUID) (*entities.PaymentMethod, error)
	GetByOrganizationID(ctx context.Context, organizationID uuid.UUID) ([]*entities.PaymentMethod, error)
	GetActiveByOrganizationID(ctx context.Context, organizationID uuid.UUID) ([]*entities.PaymentMethod, error)
	Update(ctx context.Context, paymentMethod *entities.PaymentMethod) error
	Delete(ctx context.Context, id uuid.UUID) error
	List(ctx context.Context, filters map[string]interface{}, limit, offset int) ([]*entities.PaymentMethod, int64, error)
	Count(ctx context.Context, filters map[string]interface{}) (int64, error)
	ExistsByName(ctx context.Context, organizationID uuid.UUID, name string, excludeID *uuid.UUID) (bool, error)
}

type PaymentMethodRepositoryImpl struct {
	db *gorm.DB
}

func NewPaymentMethodRepositoryImpl(db *gorm.DB) *PaymentMethodRepositoryImpl {
	return &PaymentMethodRepositoryImpl{
		db: db,
	}
}

func (r *PaymentMethodRepositoryImpl) Create(ctx context.Context, paymentMethod *entities.PaymentMethod) error {
	return r.db.WithContext(ctx).Create(paymentMethod).Error
}

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

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

func (r *PaymentMethodRepositoryImpl) GetActiveByOrganizationID(ctx context.Context, organizationID uuid.UUID) ([]*entities.PaymentMethod, error) {
	var paymentMethods []*entities.PaymentMethod
	err := r.db.WithContext(ctx).Where("organization_id = ? AND is_active = ?", organizationID, true).Find(&paymentMethods).Error
	return paymentMethods, err
}

func (r *PaymentMethodRepositoryImpl) Update(ctx context.Context, paymentMethod *entities.PaymentMethod) error {
	return r.db.WithContext(ctx).Save(paymentMethod).Error
}

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

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

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

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

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

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

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

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

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

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