.PHONY: setup update lint test mocks jenkins_setup jenkins proto proto_lint run

ENVIRONMENT ?= "development"
PORT ?= "8080"
AWS_REGION ?= "us-west-2"
ALERT_PREFERENCES_TABLE_NAME ?= "sauron-staging-alert_preferences"
DYNAMODB_TABLE_NAME ?= "sauron-staging-channel_activity"
ROLLBAR_TOKEN_SECRET_KEY ?= "token"
ROLLBAR_TOKEN_SECRET_NAME ?= "sauron/staging/rollbar_token"
STATSD_HOST ?= "statsd.central.twitch.a2z.com:8125"
LIVELINE_HOST ?= "https://main.staging.liveline.twitch.a2z.com"
PUBSUB_HOST ?= "http://pubsub-broker-darklaunch.internal.twitch.tv"
USERS_HOST ?= "https://users-service.dev.us-west2.twitch.tv"
GOBIN := $(CURDIR)/.build
PATH := $(GOBIN):$(PATH)

PDMS_CALLER_ROLE_ARN ?= "arn:aws:iam::895799599216:role/PDMSLambda-CallerRole-18451FI19HSXT"
PDMS_LAMBDA_ARN ?= "arn:aws:lambda:us-west-2:895799599216:function:PDMSLambda-LambdaFunction-IT8I1PE1YR81:live"

setup:
	brew install protobuf
	brew tap alecthomas/homebrew-tap
	brew install golangci/tap/golangci-lint
	brew install clang-format
	brew install dep
	brew install terraform
	go get -u github.com/vektra/mockery/...
	go get -u github.com/smartystreets/goconvey/...
	go get -u github.com/golang/protobuf/protoc-gen-go/...
	git -C $(GOPATH)/src/github.com/golang/protobuf checkout v1.3.5
	go install github.com/golang/protobuf/protoc-gen-go
	go get -u github.com/twitchtv/twirp/protoc-gen-twirp/...

update:
	dep ensure

lint:
	golangci-lint run --enable gosec --out-format checkstyle 2>&1 | tee golangci-report.xml

test:
	go test -cover -race -v ./...

mocks:
	mockery --all --dir=internal/clients --case=underscore --output=internal/mocks
	mockery --name Manager --dir=internal/alerts --case=underscore --output=internal/mocks

jenkins_setup:
	@wget -O - -q https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(GOBIN) v1.24.0

jenkins: jenkins_setup lint test

proto_lint:
	clang-format -style="{BasedOnStyle: Google, IndentWidth: 4}" -verbose -i rpc/sauron/*.proto

proto:
	protoc --twirp_out=. --go_out=. ./rpc/sauron/service.proto

goimports:
	find . -name \*.go -not -path ./vendor/\* -not -path ./rpc/\* -exec goimports -w {} \;

codegen:
	go run cmd/codegen/*.go handler_definitions

# Connects to the staging pubsub broker to test pubsub events in staging.
# Listen using the following, replacing <ID> with the channelID you want to listen on:
# { "type": "LISTEN", "nonce": "abc", "data": { "topics": ["pubsubtest.dashboard-activity-feed.<ID>"], "auth_token": "" } }
pubsub_test_staging:
	wscat --connect wss://pubsub-edge-darklaunch.twitch.tv

# Connects to the prod pubsub broker to test pubsub events in prod
# Listen using the following, replacing <ID> with the channelID you want to listen on:
# { "type": "LISTEN", "nonce": "abc", "data": { "topics": ["dashboard-activity-feed.<ID>"], "auth_token": "" } }
pubsub_test_prod:
	wscat --connect wss://prod.pubsub-edge.twitch.a2z.com

run:
	ENVIRONMENT=$(ENVIRONMENT) \
	PORT=$(PORT) \
	AWS_REGION=$(AWS_REGION) \
	ALERT_PREFERENCES_TABLE_NAME=$(ALERT_PREFERENCES_TABLE_NAME) \
	DYNAMODB_TABLE_NAME=$(DYNAMODB_TABLE_NAME) \
	ROLLBAR_TOKEN_SECRET_KEY=$(ROLLBAR_TOKEN_SECRET_KEY) \
	ROLLBAR_TOKEN_SECRET_NAME=$(ROLLBAR_TOKEN_SECRET_NAME) \
	STATSD_HOST=$(STATSD_HOST) \
	LIVELINE_HOST=$(LIVELINE_HOST) \
	PUBSUB_HOST=$(PUBSUB_HOST) \
	USERS_SERVICE_HOST=$(USERS_HOST) \
	PDMS_CALLER_ROLE_ARN=$(PDMS_CALLER_ROLE_ARN) \
	PDMS_LAMBDA_ARN=$(PDMS_LAMBDA_ARN) \
	go run ./cmd/sauronserver/main.go
