#!/bin/bash

# Ansible Admin Service Management Script
# Easy way to manage the ansible-admin service

SERVICE_NAME="ansible-admin"

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Function to show usage
show_usage() {
    echo -e "${BLUE}Ansible Admin Service Manager${NC}"
    echo ""
    echo "Usage: $0 {start|stop|restart|status|logs|install}"
    echo ""
    echo "Commands:"
    echo "  start   - Start the ansible-admin service"
    echo "  stop    - Stop the ansible-admin service"
    echo "  restart - Restart the ansible-admin service"
    echo "  status  - Show service status"
    echo "  logs    - Show service logs (follow mode)"
    echo "  install - Install the service (requires sudo)"
    echo ""
    echo "Examples:"
    echo "  $0 start"
    echo "  $0 status"
    echo "  sudo $0 install"
}

# Function to check if service exists
service_exists() {
    systemctl list-units --type=service | grep -q "$SERVICE_NAME"
}

# Function to start service
start_service() {
    if service_exists; then
        echo -e "${BLUE}Starting ansible-admin service...${NC}"
        sudo systemctl start "$SERVICE_NAME"
        if [[ $? -eq 0 ]]; then
            echo -e "${GREEN}✅ Service started successfully${NC}"
            echo -e "${BLUE}🌐 Web interface: http://localhost:8000${NC}"
        else
            echo -e "${RED}❌ Failed to start service${NC}"
        fi
    else
        echo -e "${RED}❌ Service not installed. Run 'sudo $0 install' first${NC}"
    fi
}

# Function to stop service
stop_service() {
    if service_exists; then
        echo -e "${BLUE}Stopping ansible-admin service...${NC}"
        sudo systemctl stop "$SERVICE_NAME"
        if [[ $? -eq 0 ]]; then
            echo -e "${GREEN}✅ Service stopped successfully${NC}"
        else
            echo -e "${RED}❌ Failed to stop service${NC}"
        fi
    else
        echo -e "${YELLOW}⚠️  Service not installed or already stopped${NC}"
    fi
}

# Function to restart service
restart_service() {
    if service_exists; then
        echo -e "${BLUE}Restarting ansible-admin service...${NC}"
        sudo systemctl restart "$SERVICE_NAME"
        if [[ $? -eq 0 ]]; then
            echo -e "${GREEN}✅ Service restarted successfully${NC}"
            echo -e "${BLUE}🌐 Web interface: http://localhost:8000${NC}"
        else
            echo -e "${RED}❌ Failed to restart service${NC}"
        fi
    else
        echo -e "${RED}❌ Service not installed. Run 'sudo $0 install' first${NC}"
    fi
}

# Function to show status
show_status() {
    if service_exists; then
        echo -e "${BLUE}Ansible Admin Service Status:${NC}"
        sudo systemctl status "$SERVICE_NAME" --no-pager -l
    else
        echo -e "${RED}❌ Service not installed${NC}"
    fi
}

# Function to show logs
show_logs() {
    if service_exists; then
        echo -e "${BLUE}Ansible Admin Service Logs (press Ctrl+C to exit):${NC}"
        sudo journalctl -u "$SERVICE_NAME" -f
    else
        echo -e "${RED}❌ Service not installed${NC}"
    fi
}

# Function to install service
install_service() {
    echo -e "${BLUE}Installing ansible-admin service...${NC}"
    sudo ./install-service.sh
}

# Main script logic
case "$1" in
    start)
        start_service
        ;;
    stop)
        stop_service
        ;;
    restart)
        restart_service
        ;;
    status)
        show_status
        ;;
    logs)
        show_logs
        ;;
    install)
        install_service
        ;;
    *)
        show_usage
        exit 1
        ;;
esac
