package mysql

import (
	"context"

	"gorm.io/gorm"

	v1 "github.com/pingcap-inc/security/model/v1"
)

type alert struct {
	db *gorm.DB
}

func newAlert(ds *datastore) *alert {
	return &alert{db: ds.db}
}

func (a *alert) MGetByAlertIDs(_ context.Context, aids []string) ([]*v1.Alert, error) {
	var res []*v1.Alert
	d := a.db.
		Where("alertID IN(?)", aids).
		Find(&res)

	return res, d.Error
}

func (a *alert) MGet30DByAlertIDs(_ context.Context, aids []string) ([]*v1.Alert, error) {
	var res []*v1.Alert
	d := a.db.
		Where("alertID IN(?)", aids).
		Where("createdAt > DATE_SUB(NOW(), INTERVAL 30 DAY)").
		Find(&res)

	return res, d.Error
}

func (a *alert) MCreate(_ context.Context, list []*v1.Alert) error {
	return a.db.Create(list).Error
}
