package delivery_address_log

import (
	"context"
	"github.com/UpmeshLTD/urus-aio/infrastructure/common"
	"github.com/UpmeshLTD/urus-aio/infrastructure/db/entities"
	"gorm.io/gorm"
)

type Repository interface {
	GetList(ctx context.Context, db *gorm.DB, cartID string, sort string) ([]*entities.DeliveryAddressLog, error)
	Create(ctx context.Context, db *gorm.DB, log *entities.DeliveryAddressLog) error
}

type repositoryImpl struct {
}

func NewRepository() Repository {
	return &repositoryImpl{}
}

func (r repositoryImpl) GetList(ctx context.Context, db *gorm.DB, cartID string, sort string) ([]*entities.DeliveryAddressLog, error) {
	var logs []*entities.DeliveryAddressLog

	builder := db.Where("cart_id", cartID)
	err := builder.Scopes(common.Paginate(
		&entities.DeliveryAddressLog{},
		&common.Pagination{
			Limit: -1,
			Sort:  sort,
		},
		builder,
	)).Model(&entities.DeliveryAddressLog{}).Find(&logs).Error
	if err != nil {
		return nil, err
	}

	return logs, nil
}

func (r repositoryImpl) Create(ctx context.Context, db *gorm.DB, log *entities.DeliveryAddressLog) error {
	return db.Model(&entities.DeliveryAddressLog{}).Create(&log).Error
}
