package repo

import (
	"context"
	"errors"
	"fmt"

	"gorm.io/gorm"

	"github.com/pingcap-inc/clinic_cloud_server/pkg/db"
)

type AlertQuotaRepo struct{}

func (repo *AlertQuotaRepo) Load(ctx context.Context, tenantId uint64) (*db.AlertQuota, error) {
	item := &db.AlertQuota{}
	err := db.GetDB().
		Model(&db.AlertQuota{}).
		Where("tenant_id = ?", tenantId).First(item).Error
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return &db.AlertQuota{}, nil
		}
		return nil, fmt.Errorf("load alert quota %d failed: %w", tenantId, err)
	}
	return item, nil
}
