package storage

import (
	"gorm.io/gorm"
	"gorm.io/gorm/clause"

	"mengyastore-backend/internal/database"
)

type WishlistStore struct {
	db *gorm.DB
}

func NewWishlistStore(db *gorm.DB) (*WishlistStore, error) {
	return &WishlistStore{db: db}, nil
}

func (s *WishlistStore) Get(accountID string) ([]string, error) {
	var rows []database.WishlistRow
	if err := s.db.Where("account_id = ?", accountID).Find(&rows).Error; err != nil {
		return nil, err
	}
	ids := make([]string, len(rows))
	for i, r := range rows {
		ids[i] = r.ProductID
	}
	return ids, nil
}

func (s *WishlistStore) Add(accountID, productID string) error {
	return s.db.Clauses(clause.OnConflict{DoNothing: true}).
		Create(&database.WishlistRow{AccountID: accountID, ProductID: productID}).Error
}

func (s *WishlistStore) Remove(accountID, productID string) error {
	return s.db.Where("account_id = ? AND product_id = ?", accountID, productID).
		Delete(&database.WishlistRow{}).Error
}
