package repository

import (
	"github.com/zs/InsightReply/internal/model"
	"gorm.io/gorm"
)

type CustomStrategyRepository struct {
	db *gorm.DB
}

func NewCustomStrategyRepository(db *gorm.DB) *CustomStrategyRepository {
	return &CustomStrategyRepository{db: db}
}

func (r *CustomStrategyRepository) ListByUserID(userID string) ([]model.UserCustomStrategy, error) {
	var strategies []model.UserCustomStrategy
	err := r.db.Where("user_id = ? AND is_active = ?", userID, true).Order("sort_order asc, created_at desc").Find(&strategies).Error
	return strategies, err
}

func (r *CustomStrategyRepository) Create(strategy *model.UserCustomStrategy) error {
	return r.db.Create(strategy).Error
}

func (r *CustomStrategyRepository) Update(strategy *model.UserCustomStrategy) error {
	return r.db.Save(strategy).Error
}

func (r *CustomStrategyRepository) Delete(id string, userID string) error {
	return r.db.Where("id = ? AND user_id = ?", id, userID).Delete(&model.UserCustomStrategy{}).Error
}

func (r *CustomStrategyRepository) GetByIDAndUser(id string, userID string) (*model.UserCustomStrategy, error) {
	var strategy model.UserCustomStrategy
	err := r.db.Where("id = ? AND user_id = ?", id, userID).First(&strategy).Error
	return &strategy, err
}
