package postgres

import (
	"context"

	"github.com/break/junhong_cmp_fiber/internal/model"
	"github.com/redis/go-redis/v9"
	"gorm.io/gorm"
)

type OrderItemStore struct {
	db    *gorm.DB
	redis *redis.Client
}

func NewOrderItemStore(db *gorm.DB, redis *redis.Client) *OrderItemStore {
	return &OrderItemStore{
		db:    db,
		redis: redis,
	}
}

func (s *OrderItemStore) BatchCreate(ctx context.Context, items []*model.OrderItem) error {
	if len(items) == 0 {
		return nil
	}
	return s.db.WithContext(ctx).Create(&items).Error
}

func (s *OrderItemStore) ListByOrderID(ctx context.Context, orderID uint) ([]*model.OrderItem, error) {
	var items []*model.OrderItem
	if err := s.db.WithContext(ctx).Where("order_id = ?", orderID).Find(&items).Error; err != nil {
		return nil, err
	}
	return items, nil
}

func (s *OrderItemStore) ListByOrderIDs(ctx context.Context, orderIDs []uint) ([]*model.OrderItem, error) {
	if len(orderIDs) == 0 {
		return nil, nil
	}
	var items []*model.OrderItem
	if err := s.db.WithContext(ctx).Where("order_id IN ?", orderIDs).Find(&items).Error; err != nil {
		return nil, err
	}
	return items, nil
}
