#!/bin/bash

# Apply Kubernetes configurations
echo "Creating Kubernetes resources..."

# Create namespace if it doesn't exist
kubectl get namespace sensor-app || kubectl create namespace sensor-app

# Apply configurations
echo "Applying PersistentVolume, PersistentVolumeClaim, and StatefulSet..."
kubectl apply -f statefulset.yaml

echo "Waiting for MySQL StatefulSet to be ready..."
kubectl wait --for=condition=ready pod -l app=mysql --timeout=120s -n sensor-app

echo "Applying Deployment..."
kubectl apply -f deployment.yaml

echo "Applying Service..."
kubectl apply -f service.yaml

echo "Waiting for application to be ready..."
kubectl wait --for=condition=ready pod -l app=floorplan-webapp --timeout=60s -n sensor-app

LOCAL_PORT=8080
echo ""
echo "Application started successfully!"
echo "Setting up port forwarding from localhost:$LOCAL_PORT to the service..."
echo "You can access the application at: http://localhost:$LOCAL_PORT"
echo ""
echo "Press Ctrl+C to stop port forwarding when done"

# Start port forwarding
kubectl port-forward -n sensor-app service/floorplan-service $LOCAL_PORT:80