package utils

import (
	"regexp"
	"strconv"
)

var orgIDPatterns = []string{
	`/api/v1/orgs/(?P<org_id>\d+)`,
	`/api/userdebug/v1/(?P<org_id>\d+)`,
	`/api/v1/authtest/(?P<org_id>\d+)`, // for test
}

func ParseOrgIDFromPath(path string) uint64 {
	for _, pattern := range orgIDPatterns {
		re := regexp.MustCompile(pattern)

		if index := re.SubexpIndex("org_id"); index != -1 {
			res := re.FindStringSubmatch(path)
			if res != nil {
				orgID, _ := strconv.ParseUint(res[index], 10, 64)
				return orgID
			}
		}
	}
	return 0
}
