package pg

import (
	"context"

	"github.com/Masterminds/squirrel"

	"go.sour.is/pkg/mercury"
)

// Notify stores the attributes for a registry space
type Notify struct {
	Name   string `json:"name" view:"mercury_notify_vw"`
	Match  string `json:"match"`
	Event  string `json:"event"`
	Method string `json:"-" db:"method"`
	URL    string `json:"-" db:"url"`
}

// GetNotify get list of rules
func (pgm *postgresHandler) GetNotify(event string) (lis mercury.ListNotify, err error) {

	rows, err := squirrel.Select("name", "match", "event", "method", "url").
		From("mercury_notify_vw").
		Where(squirrel.Eq{"event": event}).
		PlaceholderFormat(squirrel.Dollar).
		RunWith(pgm.db).
		QueryContext(context.TODO())

	if err != nil {
		return nil, err
	}

	defer rows.Close()
	for rows.Next() {
		var s mercury.Notify
		err = rows.Scan(&s.Name, &s.Match, &s.Event, &s.Method, &s.URL)
		if err != nil {
			return nil, err
		}
		lis = append(lis, s)
	}

	return lis, rows.Err()
}
