# Makefile for the Rust project

# Include and export AWS credentials if they exist
-include .aws_creds
export

export AWS_PAGER :=

# Variables
SERVICE_NAME := mcp-lightsail-rust-aws

# AWS Lightsail Configuration
AWS_REGION ?= us-east-1
LIGHTSAIL_SERVICE_NAME ?= mcp-lightsail-rust-aws
IMAGE_NAME ?= mcp-server-image

.PHONY: all build run clean release test fmt clippy check docker-build deploy-lightsail start stop status help lightsail-status endpoint push-lightsail create-deployment aws-destroy lightsail deploy

# The default target
all: build

# Build the project for development
build:
	@echo "Building the Rust project..."
	@cargo build

# Run the project
run:
	@echo "Running the Rust project..."
	@cargo run --release

# Start the server in the background
start: release
	@echo "Starting the MCP server..."
	@nohup ./target/release/$(SERVICE_NAME) > server.log 2>&1 & echo $$! > server.pid
	@echo "Server started with PID $$(cat server.pid)"

# Stop the background server
stop:
	@if [ -f server.pid ]; then \
		PID=$$(cat server.pid); \
		echo "Stopping $(SERVICE_NAME) (PID $$PID)..."; \
		kill $$PID || true; \
		rm server.pid; \
	else \
		echo "$(SERVICE_NAME) is not running."; \
	fi

# Get the status of the background server
status:
	@if [ -f server.pid ]; then \
		PID=$$(cat server.pid); \
		if ps -p $$PID > /dev/null; then \
			echo "$(SERVICE_NAME) is running (PID $$PID)."; \
		else \
			echo "$(SERVICE_NAME) PID file exists but process is not running."; \
			rm server.pid; \
		fi \
	else \
		echo "$(SERVICE_NAME) is not running."; \
	fi

# Clean the project
clean:
	@echo "Cleaning the project..."
	@cargo clean
	@rm -f server.pid server.log

# Build the project for release
release:
	@echo "Building Release..."
	@cargo build --release

# Run tests
test:
	@echo "Running tests..."
	@cargo test

# Format the code
fmt:
	@echo "Formatting code..."
	@cargo fmt --all -- --check

# Lint the code
clippy:
	@echo "Linting code..."
	@cargo clippy -- -D warnings

# Lint the code
lint:
	@echo "Linting code..."
	@cargo clippy -- -D warnings

# Check the code
check:
	@echo "Checking the code..."
	@cargo check

# Build the Docker image
docker-build:
	@echo "Building the Docker image..."
	@docker build -t $(IMAGE_NAME) .

# Deploy to Amazon Lightsail
deploy-lightsail: docker-build push-lightsail create-deployment
lightsail: deploy-lightsail
deploy: lightsail

# Target to show AWS Lightsail status
lightsail-status:
	@echo "Checking AWS Lightsail service status for $(LIGHTSAIL_SERVICE_NAME)..."
	@aws lightsail get-container-services --service-name $(LIGHTSAIL_SERVICE_NAME) --region $(AWS_REGION) \
		--query 'containerServices[0].{State:state, Power:power, Deployment:currentDeployment.state, URL:publicEndpoint.url}' \
		--output table

# Target to get the public endpoint URL
endpoint:
	@aws lightsail get-container-services --service-name $(LIGHTSAIL_SERVICE_NAME) --region $(AWS_REGION) \
		--query 'containerServices[0].url' \
		--output text

push-lightsail:
	@echo "Pushing the Docker image to Lightsail..."
	@aws lightsail push-container-image --region $(AWS_REGION) --service-name $(LIGHTSAIL_SERVICE_NAME) --label $(IMAGE_NAME) --image $(IMAGE_NAME)

create-deployment:
	@echo "Creating a new deployment for Lightsail Container Service..."
	@IMAGE_IDENTIFIER=$$(aws lightsail get-container-images --service-name $(LIGHTSAIL_SERVICE_NAME) --query "containerImages[0].image" --output text) && \
	URL=$$(aws lightsail get-container-services --service-name $(LIGHTSAIL_SERVICE_NAME) --region $(AWS_REGION) --query 'containerServices[0].url' --output text | sed 's|https://||;s|/||') && \
	aws lightsail create-container-service-deployment --region $(AWS_REGION) \
		--service-name $(LIGHTSAIL_SERVICE_NAME) \
		--containers "{\"$(LIGHTSAIL_SERVICE_NAME)\":{\"image\":\"$$IMAGE_IDENTIFIER\",\"ports\":{\"8080\":\"HTTP\"},\"environment\":{\"ALLOWED_HOSTS\":\"*,$$URL,0.0.0.0,localhost,127.0.0.1\"}}}" \
		--public-endpoint "{\"containerName\":\"$(LIGHTSAIL_SERVICE_NAME)\",\"containerPort\":8080,\"healthCheck\":{\"path\":\"/health\"}}"

aws-destroy:
	@echo "Destroying AWS resources for $(LIGHTSAIL_SERVICE_NAME)..."
	@aws lightsail delete-container-service --service-name $(LIGHTSAIL_SERVICE_NAME) --region $(AWS_REGION) 2>/dev/null || true
	@echo "AWS resources destroyed."

help:
	@echo "Makefile for the Rust project"
	@echo ""
	@echo "Usage:"
	@echo "    make <target>"
	@echo ""
	@echo "Targets:"
	@echo "    all              (default) same as 'build'"
	@echo "    build            Build the project for development"
	@echo "    run              Run the project"
	@echo "    start            Start the server in the background"
	@echo "    stop             Stop the background server"
	@echo "    status           Get the status of the background server"
	@echo "    clean            Clean the project"
	@echo "    release          Build the project for release"
	@echo "    test             Run tests"
	@echo "    fmt              Check formatting"
	@echo "    clippy           Lint the code"
	@echo "    check            Check the code"
	@echo "    docker-build     Build the Docker image"
	@echo "    deploy-lightsail Deploy to Amazon Lightsail"
	@echo "    lightsail-status Check AWS Lightsail service status"
	@echo "    endpoint         Get the public endpoint URL"
	@echo "    aws-destroy      Destroy AWS Lightsail resources"
