.PHONY: build build-cpu run run-cpu run-gpu shell shell-cpu \
        pipeline run-dataset run-dataset-cpu run-dataset-gpu \
        step1 step2 step3 step4 step5 step6 step7 \
        plot-spectra clean clean-all

# Opt-in NVIDIA GPU: layer the override file (see README "Troubleshooting").
GPU_COMPOSE = docker compose -f docker-compose.yml -f docker-compose.gpu.yml

# ============================================================
# Build
# ============================================================
build:
	docker compose build msi-pipeline-gpu

build-cpu:
	docker compose build msi-pipeline-cpu

# ============================================================
# Run (all datasets)
# ============================================================
run:
	docker compose run --rm msi-pipeline-gpu bash run_full_pipeline.sh

run-cpu:
	docker compose run --rm msi-pipeline-cpu bash run_full_pipeline.sh

run-gpu:
	$(GPU_COMPOSE) run --rm msi-pipeline-gpu bash run_full_pipeline.sh

# ============================================================
# Run single dataset
# ============================================================
run-dataset:
	@if [ -z "$(D)" ]; then echo "Usage: make run-dataset D=Datura_leaf"; exit 1; fi
	docker compose run --rm msi-pipeline-gpu bash run_full_pipeline.sh $(D)

run-dataset-cpu:
	@if [ -z "$(D)" ]; then echo "Usage: make run-dataset-cpu D=Datura_leaf"; exit 1; fi
	docker compose run --rm msi-pipeline-cpu bash run_full_pipeline.sh $(D)

run-dataset-gpu:
	@if [ -z "$(D)" ]; then echo "Usage: make run-dataset-gpu D=Datura_leaf"; exit 1; fi
	$(GPU_COMPOSE) run --rm msi-pipeline-gpu bash run_full_pipeline.sh $(D)

# ============================================================
# Interactive shell
# ============================================================
shell:
	docker compose run --rm msi-pipeline-gpu bash

shell-cpu:
	docker compose run --rm msi-pipeline-cpu bash

pipeline:
	@if [ -z "$(D)" ]; then echo "Usage: make pipeline D=Datura_leaf"; exit 1; fi
	docker compose run --rm msi-pipeline-gpu bash run_full_pipeline.sh $(D)

# ============================================================
# Individual pipeline steps (for re-running after changes)
# ============================================================
step1:  # Julia preprocessing
	@if [ -z "$(D)" ]; then echo "Usage: make step1 D=Datura_leaf"; exit 1; fi
	docker compose run --rm msi-pipeline-gpu julia --threads auto --project=/app/environment \
		/app/scripts_julia/julia_msi_bridge.jl /app/ai_data/$(D) $(D)

step2:  # SimCLR training
	@if [ -z "$(D)" ]; then echo "Usage: make step2 D=Datura_leaf"; exit 1; fi
	docker compose run --rm msi-pipeline-gpu python3 /app/scripts_python/CNN_proof_1.py $(D)

step3:  # Clustering evaluation
	@if [ -z "$(D)" ]; then echo "Usage: make step3 D=Datura_leaf"; exit 1; fi
	docker compose run --rm msi-pipeline-gpu python3 /app/scripts_python/evaluate_clustering.py $(D)

step4:  # t-SNE
	@if [ -z "$(D)" ]; then echo "Usage: make step4 D=Datura_leaf K=5"; exit 1; fi
	docker compose run --rm msi-pipeline-gpu python3 /app/scripts_python/graph_tsne_msi.py $(D) $(K)

step5:  # Validation
	@if [ -z "$(D)" ]; then echo "Usage: make step5 D=Datura_leaf K=5"; exit 1; fi
	docker compose run --rm msi-pipeline-gpu python3 /app/scripts_python/quantitative_validation.py $(D) $(K)

step6:  # Galleries
	@if [ -z "$(D)" ]; then echo "Usage: make step6 D=Datura_leaf"; exit 1; fi
	docker compose run --rm msi-pipeline-gpu python3 /app/scripts_python/visualize_clusters.py $(D)

step7:  # Mass spectra
	@if [ -z "$(D)" ]; then echo "Usage: make step7 D=Datura_leaf"; exit 1; fi
	docker compose run --rm msi-pipeline-gpu julia --project=/app/environment \
		/app/scripts_julia/plot_mass_spectra.jl $(D) /app/ai_data/$(D)

# ============================================================
# Plots
# ============================================================
plot-spectra:
	@if [ -z "$(D)" ]; then echo "Usage: make plot-spectra D=Datura_leaf"; exit 1; fi
	docker compose run --rm msi-pipeline-gpu julia --project=/app/environment \
		/app/scripts_julia/plot_mass_spectra.jl $(D) /app/ai_data/$(D)

# ============================================================
# Cleanup (outputs only; datasets are symlinked and never removed)
# ============================================================
clean:
	rm -rf outputs/ion_images/*
	rm -rf outputs/ion_images_viridis/*
	rm -rf outputs/figures/*
	rm -rf outputs/models/*
	rm -rf outputs/*/figures
	rm -rf outputs/*/galleries

clean-all: clean
	docker system prune -a --volumes -f