package appleconsumeranalytics

import (
	"strings"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/service/s3"
)

var apolloToDelphiLicensor = map[string]string{
	"sony":       "sme",
	"smej":       "smejp",
	"theorchard": "theorchard",
}

func makeStreamsSourceS3Key(date string, licensor string, vendorID string) string {
	l := apolloToDelphiLicensor[licensor]
	d := strings.ReplaceAll(date, "-", "")
	if date >= "2017-11-16" && date < "2019-05-13" {
		return "apple/streams/v1_1/report_date=" + date + "/report_licensor=" + l + "/AppleMusic_Streams_" + vendorID + "_" + d + "_V1_1.txt.gz"
	}
	if date >= "2019-05-13" && date < "2019-11-11" {
		return "apple/streams/v1_2/report_date=" + date + "/report_licensor=" + l + "/AppleMusic_Streams_" + vendorID + "_" + d + "_V1_2.txt.gz"
	}
	if date >= "2019-11-11" && date < "2021-04-14"{
		return "apple/streams/v1_2/report_date=" + date + "/report_licensor=" + l + "/AppleMusic_Streams_" + vendorID + "_" + d + "_V1_2.zip"
	}
	if date >= "2021-04-14" {
		return "apple/container/v1_0/report_date=" + date + "/report_licensor=" + l + "/AppleMusic_Container_" + vendorID + "_" + d + "_V1_0.txt.gz"
	}
	return "apple/streams/v1_0/report_date=" + date + "/report_licensor=" + l + "/AppleMusic_Streams_" + vendorID + "_" + d + "_V1_0.txt.gz"
}

func makeContentSourceS3Key(date string, licensor string, vendorID string) string {
	l := apolloToDelphiLicensor[licensor]
	d := strings.ReplaceAll(date, "-", "")
	if date >= "2017-11-16" && date < "2019-05-13" {
		return "apple/content/v1_1/report_date=" + date + "/report_licensor=" + l + "/AppleMusic_Content_" + vendorID + "_" + d + "_V1_1.txt.gz"
	}
	if date >= "2019-05-13" {
		return "apple/content/v1_2/report_date=" + date + "/report_licensor=" + l + "/AppleMusic_Content_" + vendorID + "_" + d + "_V1_2.txt.gz"
	}
	return "apple/content/v1_0/report_date=" + date + "/report_licensor=" + l + "/AppleMusic_Content_" + vendorID + "_" + d + "_V1_0.txt.gz"
}

func makeContentDemographicsSourceS3Key(date string, licensor string, vendorID string) string {
	l := apolloToDelphiLicensor[licensor]
	d := strings.ReplaceAll(date, "-", "")
	return "apple/contentdemographics/v1_0/report_date=" + date + "/report_licensor=" + l + "/AppleMusic_ContentDemographics_" + vendorID + "_" + d + "_V1_0.txt.gz"
}

func makeStreamsTracksTargetS3Key(date string, licensor string, vendorID string) string {
	return "apple/" + date + "/" + "apple_" + date + "_" + licensor + "_v1_" + vendorID + "_streams_tracks.ndjson"
}

func makeStreamsContainersTargetS3Key(date string, licensor string, vendorID string) string {
	return "apple/" + date + "/" + "apple_" + date + "_" + licensor + "_v1_" + vendorID + "_streams_containers.ndjson"
}

func makeStreamsContainerTracksTargetS3Key(date string, licensor string, vendorID string) string {
	return "apple/" + date + "/" + "apple_" + date + "_" + licensor + "_v1_" + vendorID + "_streams_container_tracks.ndjson"
}

func makeStreamsSummaryTargetS3Key(date string, licensor string, vendorID string) string {
	return "apple/" + date + "/" + "apple_" + date + "_" + licensor + "_v1_" + vendorID + "_streams_summary.ndjson"
}

func makeContentDemographicsTargetS3Key(date string, licensor string, vendorID string) string {
	return "apple/" + date + "/" + "apple_" + date + "_" + licensor + "_v1_" + vendorID + "_demographics_tracks.ndjson"
}

func sourceFileExists(s3Service *s3.S3, s3SourceBucket, key string) bool {
	return s3FileExists(s3Service, s3SourceBucket, key)
}

func targetFileExists(s3Service *s3.S3, s3TargetBucket, key string) bool {
	return s3FileExists(s3Service, s3TargetBucket, key)
}

func s3FileExists(s3Service *s3.S3, bucket string, key string) bool {
	input := &s3.HeadObjectInput{
		Bucket: aws.String(bucket),
		Key:    aws.String(key),
	}

	result, err := s3Service.HeadObject(input)
	if err != nil {
		return false
	}
	_ = result

	return true
}
