package postgres

import (
	"context"

	"gorm.io/gorm"

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

// PollingAlertRuleStore 告警规则存储
type PollingAlertRuleStore struct {
	db *gorm.DB
}

// NewPollingAlertRuleStore 创建告警规则存储实例
func NewPollingAlertRuleStore(db *gorm.DB) *PollingAlertRuleStore {
	return &PollingAlertRuleStore{db: db}
}

// Create 创建告警规则
func (s *PollingAlertRuleStore) Create(ctx context.Context, rule *model.PollingAlertRule) error {
	return s.db.WithContext(ctx).Create(rule).Error
}

// GetByID 根据ID获取告警规则
func (s *PollingAlertRuleStore) GetByID(ctx context.Context, id uint) (*model.PollingAlertRule, error) {
	var rule model.PollingAlertRule
	if err := s.db.WithContext(ctx).First(&rule, id).Error; err != nil {
		return nil, err
	}
	return &rule, nil
}

// List 获取告警规则列表
func (s *PollingAlertRuleStore) List(ctx context.Context) ([]*model.PollingAlertRule, error) {
	var rules []*model.PollingAlertRule
	if err := s.db.WithContext(ctx).Order("id ASC").Find(&rules).Error; err != nil {
		return nil, err
	}
	return rules, nil
}

// ListEnabled 获取启用的告警规则
func (s *PollingAlertRuleStore) ListEnabled(ctx context.Context) ([]*model.PollingAlertRule, error) {
	var rules []*model.PollingAlertRule
	if err := s.db.WithContext(ctx).Where("status = 1").Order("id ASC").Find(&rules).Error; err != nil {
		return nil, err
	}
	return rules, nil
}

// Update 更新告警规则
func (s *PollingAlertRuleStore) Update(ctx context.Context, rule *model.PollingAlertRule) error {
	return s.db.WithContext(ctx).Save(rule).Error
}

// Delete 删除告警规则
func (s *PollingAlertRuleStore) Delete(ctx context.Context, id uint) error {
	return s.db.WithContext(ctx).Delete(&model.PollingAlertRule{}, id).Error
}

// PollingAlertHistoryStore 告警历史存储
type PollingAlertHistoryStore struct {
	db *gorm.DB
}

// NewPollingAlertHistoryStore 创建告警历史存储实例
func NewPollingAlertHistoryStore(db *gorm.DB) *PollingAlertHistoryStore {
	return &PollingAlertHistoryStore{db: db}
}

// Create 创建告警历史
func (s *PollingAlertHistoryStore) Create(ctx context.Context, history *model.PollingAlertHistory) error {
	return s.db.WithContext(ctx).Create(history).Error
}

// List 获取告警历史列表
func (s *PollingAlertHistoryStore) List(ctx context.Context, page, pageSize int, ruleID *uint) ([]*model.PollingAlertHistory, int64, error) {
	var histories []*model.PollingAlertHistory
	var total int64

	query := s.db.WithContext(ctx).Model(&model.PollingAlertHistory{})
	if ruleID != nil {
		query = query.Where("rule_id = ?", *ruleID)
	}

	if err := query.Count(&total).Error; err != nil {
		return nil, 0, err
	}

	offset := (page - 1) * pageSize
	if err := query.Order("created_at DESC").Offset(offset).Limit(pageSize).Find(&histories).Error; err != nil {
		return nil, 0, err
	}

	return histories, total, nil
}

// GetLatestByRuleID 获取规则最近一条告警
func (s *PollingAlertHistoryStore) GetLatestByRuleID(ctx context.Context, ruleID uint) (*model.PollingAlertHistory, error) {
	var history model.PollingAlertHistory
	if err := s.db.WithContext(ctx).Where("rule_id = ?", ruleID).Order("created_at DESC").First(&history).Error; err != nil {
		return nil, err
	}
	return &history, nil
}

// UpdateNotificationStatus 更新通知发送状态
func (s *PollingAlertHistoryStore) UpdateNotificationStatus(ctx context.Context, id uint, status string) error {
	return s.db.WithContext(ctx).Model(&model.PollingAlertHistory{}).
		Where("id = ?", id).
		Update("notification_status", status).Error
}
