#!/usr/bin/env bash
set -e

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

# Get script directory and project root
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"

# Detect Docker or Podman
CONTAINER_CLI=""
# Check if docker is actually podman in disguise
if command -v docker &> /dev/null && docker --version 2>&1 | grep -q "podman"; then
    CONTAINER_CLI="podman"
    echo -e "${GREEN}Using Podman (via docker alias)${NC}"
elif command -v docker &> /dev/null && docker info &> /dev/null; then
    CONTAINER_CLI="docker"
    echo -e "${GREEN}Using Docker${NC}"
elif command -v podman &> /dev/null; then
    CONTAINER_CLI="podman"
    echo -e "${GREEN}Using Podman${NC}"
else
    echo -e "${RED}✗ Neither Docker nor Podman found. Please install one of them first.${NC}"
    echo -e "${YELLOW}Install Docker: https://docs.docker.com/get-docker/${NC}"
    echo -e "${YELLOW}Install Podman: https://podman.io/getting-started/installation${NC}"
    exit 1
fi

echo -e "${GREEN}=== Dish Planner Development Setup ===${NC}\n"

# Check if .env exists in backend
if [ ! -f "$PROJECT_ROOT/backend/.env" ]; then
    echo -e "${YELLOW}Creating backend/.env from .env.example...${NC}"
    cp "$PROJECT_ROOT/backend/.env.example" "$PROJECT_ROOT/backend/.env"
    echo -e "${GREEN}✓ Created backend/.env${NC}\n"
fi

# Ensure APP_PORT is set for Podman (can't use privileged port 80)
if [ "$CONTAINER_CLI" = "podman" ] && ! grep -q "^APP_PORT=" "$PROJECT_ROOT/backend/.env"; then
    echo -e "${YELLOW}Setting APP_PORT=8000 for Podman (non-privileged port)...${NC}"
    echo "APP_PORT=8000" >> "$PROJECT_ROOT/backend/.env"
fi

# Backend setup
echo -e "${GREEN}=== Backend Setup ===${NC}"
cd "$PROJECT_ROOT/backend"

# Install dependencies if vendor doesn't exist
if [ ! -d "vendor" ]; then
    echo -e "${YELLOW}No vendor directory found. Installing dependencies with Docker...${NC}"
    # Use a standalone PHP/Composer Docker image to install dependencies
    # This is the recommended Laravel approach for first-time setup
    # Configure for Docker or Podman
    VOLUME_OPTS="$PROJECT_ROOT/backend:/var/www/html"
    USER_OPTS="-u $(id -u):$(id -g)"
    EXTRA_OPTS=""

    if [ "$CONTAINER_CLI" = "podman" ]; then
        # Podman on SELinux systems needs :Z and --userns=keep-id to maintain user ID
        VOLUME_OPTS="$VOLUME_OPTS:Z"
        USER_OPTS=""
        EXTRA_OPTS="--userns=keep-id"
    fi

    $CONTAINER_CLI run --rm \
        $USER_OPTS \
        $EXTRA_OPTS \
        -v "$VOLUME_OPTS" \
        -w /var/www/html \
        docker.io/laravelsail/php84-composer:latest \
        composer install --ignore-platform-reqs

    echo -e "${GREEN}✓ Backend dependencies installed${NC}\n"
else
    echo -e "${GREEN}✓ Backend dependencies already installed${NC}\n"
fi

# Fix docker-compose.yml for Podman (needs full registry paths)
if [ "$CONTAINER_CLI" = "podman" ]; then
    echo -e "${YELLOW}Patching docker-compose.yml for Podman compatibility...${NC}"
    sed -i "s|image: 'mysql/mysql-server:|image: 'docker.io/mysql/mysql-server:|g" docker-compose.yml
fi

# Start Laravel Sail (database + backend services)
echo -e "${YELLOW}Starting Laravel Sail containers...${NC}"
if ./vendor/bin/sail up -d 2>&1 | tee /tmp/sail-up.log; then
    echo -e "${GREEN}✓ Containers started${NC}\n"
else
    echo -e "${RED}✗ Failed to start containers. Check /tmp/sail-up.log for details${NC}"
    exit 1
fi

# Wait for database to be ready
echo -e "${YELLOW}Waiting for database to be ready...${NC}"
sleep 5

# Run migrations
echo -e "${YELLOW}Running database migrations...${NC}"
./vendor/bin/sail artisan migrate --force

# Check if database has data
TABLE_COUNT=$(./vendor/bin/sail artisan tinker --execute="echo \DB::table('users')->count();")
if [ "$TABLE_COUNT" -eq "0" ]; then
    echo -e "${YELLOW}Database is empty. Run seeders? (y/n)${NC}"
    read -r response
    if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
        ./vendor/bin/sail artisan db:seed
        echo -e "${GREEN}✓ Database seeded${NC}\n"
    fi
fi

echo -e "${GREEN}✓ Backend setup complete${NC}"
echo -e "${GREEN}Backend API running at: http://localhost:8000${NC}\n"

# Frontend setup
echo -e "${GREEN}=== Frontend Setup ===${NC}"
cd "$PROJECT_ROOT/frontend"

if [ ! -d "node_modules" ]; then
    echo -e "${YELLOW}Installing frontend dependencies (npm)...${NC}"
    npm install
    echo -e "${GREEN}✓ Frontend dependencies installed${NC}\n"
else
    echo -e "${GREEN}✓ Frontend dependencies already installed${NC}\n"
fi

echo -e "${GREEN}✓ Frontend setup complete${NC}\n"

# Display summary
echo -e "${GREEN}=== Development Environment Ready ===${NC}"
echo -e "${GREEN}Backend API:${NC} http://localhost:8000"
echo -e "${GREEN}Frontend:${NC} http://localhost:5173 (start with: cd frontend && npm run dev)"
echo -e "${GREEN}Database:${NC} MySQL on localhost:3306"
echo -e ""
echo -e "${YELLOW}To start the frontend dev server, run:${NC}"
echo -e "  cd frontend && npm run dev"
echo -e ""
echo -e "${YELLOW}To stop backend services:${NC}"
echo -e "  cd backend && ./vendor/bin/sail down"
