package parse

import (
	"regexp"
	"strconv"
)

var orgIDPatterns = []*regexp.Regexp{
	regexp.MustCompile(`/orgs/(?P<org_id>\d+)`),
}

var projectIDPatterns = []*regexp.Regexp{
	regexp.MustCompile(`/projects/(?P<project_id>\d+)`),
}

var clusterIDPatterns = []*regexp.Regexp{
	regexp.MustCompile(`/clusters/(?P<cluster_id>\d+)`),
	regexp.MustCompile(`/recycle-bin/(?P<cluster_id>\d+)`),
}

func ParseOrgIDFromPath(path string) (uint64, bool) {
	for _, re := range orgIDPatterns {
		if index := re.SubexpIndex("org_id"); index != -1 {
			res := re.FindStringSubmatch(path)
			if res != nil {
				orgID, _ := strconv.ParseUint(res[index], 10, 64)
				return orgID, true
			}
		}
	}

	return 0, false
}

func ParseProjectIDFromPath(path string) (uint64, bool) {
	for _, re := range projectIDPatterns {
		if index := re.SubexpIndex("project_id"); index != -1 {
			res := re.FindStringSubmatch(path)
			if res != nil {
				projectId, _ := strconv.ParseUint(res[index], 10, 64)
				return projectId, true
			}
		}
	}
	return 0, false
}

func ParseClusterIDFromPath(path string) (uint64, bool) {
	for _, re := range clusterIDPatterns {
		if index := re.SubexpIndex("cluster_id"); index != -1 {
			res := re.FindStringSubmatch(path)
			if res != nil {
				clusterID, _ := strconv.ParseUint(res[index], 10, 64)
				return clusterID, true
			}
		}
	}
	return 0, false
}
