package repository

import (
	"apskel-pos-be/internal/entities"
	"context"

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

type OutletRepositoryImpl struct {
	db *gorm.DB
}

func NewOutletRepositoryImpl(db *gorm.DB) *OutletRepositoryImpl {
	return &OutletRepositoryImpl{
		db: db,
	}
}

func (r *OutletRepositoryImpl) Create(ctx context.Context, outlet *entities.Outlet) error {
	return r.db.WithContext(ctx).Create(outlet).Error
}

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

func (r *OutletRepositoryImpl) GetWithOrders(ctx context.Context, id uuid.UUID) (*entities.Outlet, error) {
	var outlet entities.Outlet
	err := r.db.WithContext(ctx).Preload("Orders").First(&outlet, "id = ?", id).Error
	if err != nil {
		return nil, err
	}
	return &outlet, nil
}

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

func (r *OutletRepositoryImpl) GetByOrganizationIDWithPagination(ctx context.Context, organizationID uuid.UUID, limit, offset int) ([]*entities.Outlet, int64, error) {
	var outlets []*entities.Outlet
	var total int64

	query := r.db.WithContext(ctx).Where("organization_id = ?", organizationID)

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

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

func (r *OutletRepositoryImpl) Update(ctx context.Context, outlet *entities.Outlet) error {
	return r.db.WithContext(ctx).Save(outlet).Error
}

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

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

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

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

	for key, value := range filters {
		query = query.Where(key+" = ?", value)
	}

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

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

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

	for key, value := range filters {
		query = query.Where(key+" = ?", value)
	}

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