# --- Data Connect / Backend ---

.PHONY: dataconnect-enable-apis dataconnect-init dataconnect-deploy dataconnect-sql-migrate dataconnect-generate-sdk dataconnect-sync dataconnect-bootstrap-db check-gcloud-beta

# Enable all required APIs for Firebase Data Connect + Cloud SQL
dataconnect-enable-apis:
	@echo "--> Enabling Firebase & Data Connect APIs on project [$(GCP_PROJECT_ID)]..."
	@gcloud services enable firebase.googleapis.com --project=$(GCP_PROJECT_ID)
	@gcloud services enable firebasedataconnect.googleapis.com --project=$(GCP_PROJECT_ID)
	@gcloud services enable sqladmin.googleapis.com --project=$(GCP_PROJECT_ID)
	@gcloud services enable iam.googleapis.com --project=$(GCP_PROJECT_ID)
	@gcloud services enable cloudresourcemanager.googleapis.com --project=$(GCP_PROJECT_ID)
	@gcloud services enable secretmanager.googleapis.com --project=$(GCP_PROJECT_ID)
	@echo "✅ APIs enabled for project [$(GCP_PROJECT_ID)]."

# Initialize Firebase Data Connect (interactive wizard).
dataconnect-init:
	@echo "--> Initializing Firebase Data Connect for alias [$(FIREBASE_ALIAS)] (project: $(GCP_PROJECT_ID))..."
	@firebase init dataconnect --project $(FIREBASE_ALIAS)
	@echo "✅ Data Connect initialization command executed. Follow the interactive steps in the CLI."

# Deploy Data Connect schemas (GraphQL → Cloud SQL)
dataconnect-deploy:
	@echo "--> Deploying Firebase Data Connect schemas to [$(ENV)] (project: $(FIREBASE_ALIAS))..."
	@firebase deploy --only dataconnect --project=$(FIREBASE_ALIAS)
	@echo "✅ Data Connect deployment completed for [$(ENV)]."

# Apply pending SQL migrations for Firebase Data Connect
dataconnect-sql-migrate:
	@echo "--> Applying Firebase Data Connect SQL migrations to [$(ENV)] (project: $(FIREBASE_ALIAS))..."
	@firebase dataconnect:sql:migrate --project=$(FIREBASE_ALIAS)
	@echo "✅ Data Connect SQL migration completed for [$(ENV)]."

# Generate Data Connect client SDK for frontend-web and internal-api-harness
dataconnect-generate-sdk:
	@echo "--> Generating Firebase Data Connect SDK for web frontend and API harness..."
	@firebase dataconnect:sdk:generate --project=$(FIREBASE_ALIAS)
	@echo "✅ Data Connect SDK generation completed for [$(ENV)]."

# Unified backend schema update workflow (schema -> deploy -> SDK)
dataconnect-sync:
	@echo "--> [1/3] Deploying Data Connect..."
	@firebase deploy --only dataconnect --project=$(FIREBASE_ALIAS)
	@echo "--> [2/3] Applying SQL migrations..."
	@firebase dataconnect:sql:migrate --project=$(FIREBASE_ALIAS)
	@echo "--> [3/3] Regenerating SDK..."
	@firebase dataconnect:sdk:generate --project=$(FIREBASE_ALIAS)
	@echo "✅ Data Connect SQL, deploy, and SDK generation completed for [$(ENV)]."

# Execute seed in Firebase Data Connect
dataconnect-seed:
	@echo "--> Exec seed in Firebase Data Connect..."
	@firebase dataconnect:execute seeds/seed_min.graphql --project=$(FIREBASE_ALIAS)
	@echo "✅ Seed executed successfully."

# Run tests for Data Connect deployment and migrations
dataconnect-test:
	@echo "--> Running Data Connect tests..."
	@echo "--> [1/3] Deploying Data Connect..."
	@firebase deploy --only dataconnect --project=$(FIREBASE_ALIAS) --dry-run 
	@echo "--> [2/3] Applying SQL migrations..." 
	@firebase dataconnect:sql:diff --project=$(FIREBASE_ALIAS)
	@echo "✅ Data Connect tests completed."

# -------------------------------------------------------------------
# ONE-TIME FULL SETUP FOR CLOUD SQL + DATA CONNECT
# -------------------------------------------------------------------

# Check if gcloud and beta group are available
check-gcloud-beta:
	@command -v gcloud >/dev/null 2>&1 || { \
		echo "❌ gcloud CLI not found. Please install it: https://cloud.google.com/sdk/docs/install"; \
		exit 1; \
	}
	@gcloud beta --help >/dev/null 2>&1 || { \
		echo "❌ 'gcloud beta' is not available. Run 'gcloud components update' or reinstall the SDK."; \
		exit 1; \
	}
	@echo "✅ gcloud CLI and 'gcloud beta' are available."

dataconnect-bootstrap-db: check-gcloud-beta
	@echo "🔍 Checking if Cloud SQL instance krow-sql already exists in [$(GCP_PROJECT_ID)]..."
	@if gcloud sql instances describe krow-sql --project=$(GCP_PROJECT_ID) >/dev/null 2>&1; then \
		echo "⚠️ Cloud SQL instance 'krow-sql' already exists in project $(GCP_PROJECT_ID)."; \
		echo "   If you really need to recreate it, delete the instance manually first."; \
		exit 1; \
	fi

	@echo "⚠️  Creating Cloud SQL instance krow-sql (tier: $(SQL_TIER)) (ONLY RUN THIS ONCE PER PROJECT)..."
	gcloud sql instances create krow-sql \
		--database-version=POSTGRES_15 \
		--tier=$(SQL_TIER) \
		--region=us-central1 \
		--storage-size=10 \
		--storage-auto-increase \
		--availability-type=zonal \
		--backup-start-time=03:00 \
		--project=$(GCP_PROJECT_ID)

	@echo "⚠️  Creating Cloud SQL database krow_db..."
	gcloud sql databases create krow_db \
		--instance=krow-sql \
		--project=$(GCP_PROJECT_ID)

	@echo "⚠️  Creating Firebase Data Connect service identity..."
	gcloud beta services identity create \
		--service=firebasedataconnect.googleapis.com \
		--project=$(GCP_PROJECT_ID)

	@echo "⚠️  Enabling IAM authentication on Cloud SQL instance krow-sql..."
	gcloud sql instances patch krow-sql \
		--project=$(GCP_PROJECT_ID) \
		--database-flags=cloudsql.iam_authentication=on \
		--quiet

	@echo "⚠️  Linking Data Connect service (krow-workforce-db) with Cloud SQL..."
	firebase dataconnect:sql:setup krow-workforce-db --project=$(FIREBASE_ALIAS)

	@echo "⚠️  Deploying initial Data Connect configuration..."
	@firebase deploy --only dataconnect --project=$(FIREBASE_ALIAS)

	@echo "⚠️  Generating initial Data Connect SDK..."
	@firebase dataconnect:sdk:generate --project=$(FIREBASE_ALIAS)

	@echo "🎉 Cloud SQL + Data Connect bootstrap completed successfully!"
