package postgres

import (
	"context"

	"github.com/break/junhong_cmp_fiber/internal/model"
	"gorm.io/gorm"
)

type ResourceTagStore struct {
	db *gorm.DB
}

func NewResourceTagStore(db *gorm.DB) *ResourceTagStore {
	return &ResourceTagStore{db: db}
}

func (s *ResourceTagStore) ListByResource(ctx context.Context, resourceType string, resourceID uint) ([]*model.ResourceTag, error) {
	var list []*model.ResourceTag
	if err := s.db.WithContext(ctx).
		Where("resource_type = ? AND resource_id = ?", resourceType, resourceID).
		Find(&list).Error; err != nil {
		return nil, err
	}
	return list, nil
}

func (s *ResourceTagStore) Create(ctx context.Context, item *model.ResourceTag) error {
	return s.db.WithContext(ctx).Create(item).Error
}
