package repository

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

type CompetitorMonitorRepository struct {
	db *gorm.DB
}

func NewCompetitorMonitorRepository(db *gorm.DB) *CompetitorMonitorRepository {
	return &CompetitorMonitorRepository{db: db}
}

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

func (r *CompetitorMonitorRepository) ListAllActive() ([]model.CompetitorMonitor, error) {
	var monitors []model.CompetitorMonitor
	err := r.db.Where("is_active = ?", true).Find(&monitors).Error
	return monitors, err
}

func (r *CompetitorMonitorRepository) Create(monitor *model.CompetitorMonitor) error {
	return r.db.Create(monitor).Error
}

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