package audit

import (
	"context"
	"fmt"
	"slices"

	auditent "github.com/tidbcloud/devops/entity/audit"
	"github.com/tidbcloud/devops/pkg/service"
)

type ListAuditLogParam struct {
	Type      auditent.AuditLogType
	ObjectIDs []string

	Page    int // default 1
	PerPage int // default 50
}

func (p ListAuditLogParam) Normalize() (ListAuditLogParam, error) {
	if p.Page == 0 {
		p.Page = 1
	}
	if p.PerPage == 0 {
		p.PerPage = 50
	}

	if !slices.Contains(auditent.AuditLogTypeValues, p.Type) {
		return p, fmt.Errorf("unknown audit log type %s", p.Type)
	}

	return p, nil
}

type AuditServiceAPI interface {
	ListAuditLog(ctx context.Context, param ListAuditLogParam) ([]*auditent.AuditLog, int64, error)
	ListAuditLogTypes(ctx context.Context) ([]auditent.AuditLogType, error)
	Create(ctx context.Context, adtLog *auditent.AuditLog) error
}

type AuditService interface {
	AuditServiceAPI

	service.Service
}
