package provider

import (
	"io/fs"
	"regexp"
	"strings"
)

// todo this is a TERRIBLE name, figure something better out.
type ResourceOrData struct {
	Name       string
	Service    *Service
	GoPath     string
	GoFileName string
	TestPaths  []string

	IsTyped     bool
	IsGenerated bool

	SdkAzureSdkGo bool
	SdkKermit     bool
	SdkPandora    bool
	SdkGiovanni   bool

	UsesBuiltInParse bool

	// nwe base layer
}

func (s *Service) GetResourceOrDataFor(file fs.DirEntry, content string) ResourceOrData {
	fileName := file.Name()

	name := "azurerm_" + strings.ReplaceAll(fileName, "_data_source.go", "")
	name = "azurerm_" + strings.ReplaceAll(name, "_resource.go", "")

	e := ResourceOrData{
		Name:       name,
		Service:    s,
		GoPath:     s.Path + "/" + fileName,
		GoFileName: fileName,
		// TestsPath: s.Path + "/" + strings.Replace(name, "_resource.go", "_resource_test.go", -1),
	}

	// todo there is a better way to do this
	// map regex -> *property to flip

	// is typed
	isTypedHastfSchema := regexp.MustCompile("`tfschema:\"[a-zA-Z_]*\"`")
	if isTypedHastfSchema.MatchString(content) {
		e.IsTyped = true
	}

	isTypedResourceRegex := regexp.MustCompile("_ sdk.Resource[a-zA-Z]* += ")
	if isTypedResourceRegex.MatchString(content) {
		e.IsTyped = true
	}

	isTypedDataSourceRegex := regexp.MustCompile("_ sdk.DataSource[a-zA-Z]* += ")
	if isTypedDataSourceRegex.MatchString(content) {
		e.IsTyped = true
	}

	// is using pandora SDK
	isPandoraRegex := regexp.MustCompile("\"github.com/hashicorp/go-azure-sdk/")
	if isPandoraRegex.MatchString(content) {
		e.SdkPandora = true
	}

	// is using track 1 sdk
	isTrack1Regex := regexp.MustCompile("\"github.com/Azure/azure-sdk-for-go/")
	if isTrack1Regex.MatchString(content) {
		e.SdkAzureSdkGo = true
	}

	// is using giovanni sdk
	isGiovanniRegex := regexp.MustCompile("\"github.com/tombuildsstuff/giovanni/")
	if isGiovanniRegex.MatchString(content) {
		e.SdkGiovanni = true
	}

	// is using kermit  sdk
	isKermitRegex := regexp.MustCompile("\"github.com/tombuildsstuff/kermit/")
	if isKermitRegex.MatchString(content) {
		e.SdkKermit = true
	}

	// is autogenerated
	isGeneratedRegex := regexp.MustCompile("// NOTE: this file is generated")
	if isGeneratedRegex.MatchString(content) {
		e.IsGenerated = true
	}

	// uses built in parse (ie not pandora parse function)
	isBuiltInParse := regexp.MustCompile("= parse.")
	if isBuiltInParse.MatchString(content) {
		e.UsesBuiltInParse = true
	}

	return e
}
