#!/bin/bash
# BuyerKiosk Docker Development Helper
# Usage: ./docker-dev.sh [command]

set -e

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

print_header() {
    echo -e "${BLUE}═══════════════════════════════════════════════════════════${NC}"
    echo -e "${BLUE}  🐳 BuyerKiosk Docker Development${NC}"
    echo -e "${BLUE}═══════════════════════════════════════════════════════════${NC}"
}

case "${1:-help}" in
    up|start)
        print_header
        echo -e "${GREEN}Starting all containers...${NC}"
        docker-compose up -d
        echo ""
        echo -e "${GREEN}✅ Services started!${NC}"
        echo -e "   🌐 Web App:    http://localhost:8080"
        echo -e "   📊 Adminer:    http://localhost:8081"
        echo -e "   📧 Mailhog:    http://localhost:8025"
        echo -e "   🔴 Redis:      localhost:6380"
        echo -e "   🗄️  MySQL:      localhost:3307"
        ;;

    down|stop)
        print_header
        echo -e "${YELLOW}Stopping all containers...${NC}"
        docker-compose down
        echo -e "${GREEN}✅ Containers stopped.${NC}"
        ;;

    restart)
        print_header
        echo -e "${YELLOW}Restarting containers...${NC}"
        docker-compose restart
        echo -e "${GREEN}✅ Containers restarted.${NC}"
        ;;

    build)
        print_header
        echo -e "${GREEN}Building containers (no cache)...${NC}"
        docker-compose build --no-cache
        echo -e "${GREEN}✅ Build complete.${NC}"
        ;;

    logs)
        docker-compose logs -f ${2:-web}
        ;;

    shell|bash)
        print_header
        echo -e "${GREEN}Opening shell in web container...${NC}"
        docker-compose exec web bash
        ;;

    mysql)
        print_header
        echo -e "${GREEN}Connecting to MySQL...${NC}"
        docker-compose exec mysql mysql -u kiosk_db -pkiosk_dev_password
        ;;

    redis)
        print_header
        echo -e "${GREEN}Connecting to Redis CLI...${NC}"
        docker-compose exec redis redis-cli
        ;;

    composer)
        shift
        docker-compose exec web bash -c "cd /var/www/html/userfrosting && composer $*"
        ;;

    test)
        print_header
        echo -e "${GREEN}Running tests...${NC}"
        docker-compose exec web bash -c "cd /var/www/html && ./test.sh ${2:-}"
        ;;

    migrate)
        print_header
        echo -e "${GREEN}Running database migrations...${NC}"
        docker-compose exec web bash -c "cd /var/www/html/userfrosting && php conductor run"
        ;;

    css)
        print_header
        echo -e "${GREEN}Building CSS...${NC}"
        docker-compose exec web bash -c "cd /var/www/html/userfrosting && php conductor build-css ${2:---minify}"
        ;;

    css-watch)
        print_header
        echo -e "${GREEN}Watching CSS for changes...${NC}"
        docker-compose exec web bash -c "cd /var/www/html/userfrosting && php conductor build-css --watch"
        ;;

    worker)
        print_header
        echo -e "${GREEN}Starting background worker...${NC}"
        docker-compose exec web bash -c "cd /var/www/html/userfrosting && php bin/task worker:start --queues=high,default,low"
        ;;

    queue-status)
        docker-compose exec web bash -c "cd /var/www/html/userfrosting && php bin/task queue:status --detailed"
        ;;

    clean)
        print_header
        echo -e "${YELLOW}Cleaning up Docker resources...${NC}"
        docker-compose down -v --remove-orphans
        echo -e "${GREEN}✅ Cleanup complete. Volumes removed.${NC}"
        ;;

    reset)
        print_header
        echo -e "${RED}⚠️  This will delete all data and rebuild from scratch!${NC}"
        read -p "Are you sure? (y/N) " -n 1 -r
        echo
        if [[ $REPLY =~ ^[Yy]$ ]]; then
            docker-compose down -v --remove-orphans
            docker-compose build --no-cache
            docker-compose up -d
            echo ""
            echo -e "${GREEN}✅ Reset complete!${NC}"
        else
            echo -e "${YELLOW}Cancelled.${NC}"
        fi
        ;;

    status)
        print_header
        docker-compose ps
        ;;

    help|*)
        print_header
        echo ""
        echo "Usage: ./docker-dev.sh [command]"
        echo ""
        echo "Commands:"
        echo -e "  ${GREEN}up, start${NC}      Start all containers"
        echo -e "  ${GREEN}down, stop${NC}     Stop all containers"
        echo -e "  ${GREEN}restart${NC}        Restart containers"
        echo -e "  ${GREEN}build${NC}          Rebuild containers (no cache)"
        echo -e "  ${GREEN}logs [svc]${NC}     Tail logs (default: web)"
        echo -e "  ${GREEN}shell, bash${NC}    Open shell in web container"
        echo -e "  ${GREEN}mysql${NC}          Connect to MySQL CLI"
        echo -e "  ${GREEN}redis${NC}          Connect to Redis CLI"
        echo -e "  ${GREEN}composer [cmd]${NC} Run composer command"
        echo -e "  ${GREEN}test [opts]${NC}    Run test suite"
        echo -e "  ${GREEN}migrate${NC}        Run database migrations"
        echo -e "  ${GREEN}css${NC}            Build CSS (minified)"
        echo -e "  ${GREEN}css-watch${NC}      Watch CSS for changes"
        echo -e "  ${GREEN}worker${NC}         Start background worker"
        echo -e "  ${GREEN}queue-status${NC}   Show queue depths"
        echo -e "  ${GREEN}status${NC}         Show container status"
        echo -e "  ${GREEN}clean${NC}          Stop and remove volumes"
        echo -e "  ${GREEN}reset${NC}          Full reset (destroy & rebuild)"
        echo ""
        ;;
esac
