#!/bin/bash
#
# Church Music Database - Service Management Script
# Convenient wrapper for managing systemd services
#

GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'

BACKEND="church-music-backend"
FRONTEND="church-music-frontend"

show_help() {
    echo -e "${GREEN}Church Music Database - Service Manager${NC}"
    echo ""
    echo "Usage: $0 [COMMAND]"
    echo ""
    echo "Commands:"
    echo "  status      - Show status of both services"
    echo "  start       - Start both services"
    echo "  stop        - Stop both services"
    echo "  restart     - Restart both services"
    echo "  logs        - Show live logs (backend)"
    echo "  logs-fe     - Show live logs (frontend)"
    echo "  enable      - Enable auto-start on boot"
    echo "  disable     - Disable auto-start on boot"
    echo "  health      - Check if services are responding"
    echo ""
    echo "Examples:"
    echo "  $0 status"
    echo "  $0 restart"
    echo "  $0 logs"
    echo ""
}

check_health() {
    echo -e "${YELLOW}Checking service health...${NC}"
    echo ""
    
    # Check backend
    if curl -s http://localhost:8080/api/health > /dev/null 2>&1; then
        echo -e "${GREEN}✓ Backend API (port 8080): HEALTHY${NC}"
    else
        echo -e "${RED}✗ Backend API (port 8080): NOT RESPONDING${NC}"
    fi
    
    # Check frontend
    if curl -s http://localhost:5100 > /dev/null 2>&1; then
        echo -e "${GREEN}✓ Frontend (port 5100): HEALTHY${NC}"
    else
        echo -e "${RED}✗ Frontend (port 5100): NOT RESPONDING${NC}"
    fi
    echo ""
}

case "$1" in
    status)
        echo -e "${GREEN}=== Backend Service ===${NC}"
        sudo systemctl status $BACKEND --no-pager -l
        echo ""
        echo -e "${GREEN}=== Frontend Service ===${NC}"
        sudo systemctl status $FRONTEND --no-pager -l
        ;;
    start)
        echo -e "${YELLOW}Starting services...${NC}"
        sudo systemctl start $BACKEND
        sudo systemctl start $FRONTEND
        sleep 2
        echo -e "${GREEN}✓ Services started${NC}"
        check_health
        ;;
    stop)
        echo -e "${YELLOW}Stopping services...${NC}"
        sudo systemctl stop $BACKEND
        sudo systemctl stop $FRONTEND
        echo -e "${GREEN}✓ Services stopped${NC}"
        ;;
    restart)
        echo -e "${YELLOW}Restarting services...${NC}"
        sudo systemctl restart $BACKEND
        sudo systemctl restart $FRONTEND
        sleep 2
        echo -e "${GREEN}✓ Services restarted${NC}"
        check_health
        ;;
    logs)
        echo -e "${GREEN}Showing backend logs (Ctrl+C to exit)...${NC}"
        sudo journalctl -u $BACKEND -f --no-pager
        ;;
    logs-fe)
        echo -e "${GREEN}Showing frontend logs (Ctrl+C to exit)...${NC}"
        sudo journalctl -u $FRONTEND -f --no-pager
        ;;
    enable)
        echo -e "${YELLOW}Enabling auto-start on boot...${NC}"
        sudo systemctl enable $BACKEND
        sudo systemctl enable $FRONTEND
        echo -e "${GREEN}✓ Services will start automatically on boot${NC}"
        ;;
    disable)
        echo -e "${YELLOW}Disabling auto-start on boot...${NC}"
        sudo systemctl disable $BACKEND
        sudo systemctl disable $FRONTEND
        echo -e "${GREEN}✓ Auto-start disabled${NC}"
        ;;
    health)
        check_health
        ;;
    *)
        show_help
        exit 1
        ;;
esac
