{ pkgs ? import <nixpkgs> {} }:

pkgs.mkShell {
  buildInputs = with pkgs; [
    # PHP and tools
    php84
    php84Packages.composer

    # Node.js and npm
    nodejs_22

    # Container tools
    podman
    podman-compose

    # Database client (for direct DB access)
    mariadb.client

    # Utilities
    git
    curl
    gnumake
  ];

  shellHook = ''
    export PATH="$HOME/.config/composer/vendor/bin:$PATH"

    export USER_ID=$(id -u)
    export GROUP_ID=$(id -g)
    export PODMAN_USERNS=keep-id

    # Compose file location
    COMPOSE_FILE="$PWD/docker/dev/docker-compose.yml"

    # ===================
    # ALIASES
    # ===================
    alias pc='podman-compose -f $COMPOSE_FILE'

    # ===================
    # DEV COMMANDS
    # ===================
    dev-up() {
      echo "Starting services..."
      PODMAN_USERNS=keep-id podman-compose -f $COMPOSE_FILE up -d "$@"
      echo ""
      podman-compose -f $COMPOSE_FILE ps
      echo ""
      echo "App available at: http://localhost:8001"
    }

    dev-down() {
      if [[ "$1" == "-v" ]]; then
        echo "Stopping services and removing volumes..."
        podman-compose -f $COMPOSE_FILE down -v
      else
        echo "Stopping services..."
        podman-compose -f $COMPOSE_FILE down
      fi
    }

    dev-restart() {
      echo "Restarting services..."
      podman-compose -f $COMPOSE_FILE restart "$@"
    }

    dev-rebuild() {
      echo "Rebuilding services (down -v + up --build)..."
      podman-compose -f $COMPOSE_FILE down -v
      PODMAN_USERNS=keep-id podman-compose -f $COMPOSE_FILE up -d --build "$@"
      echo ""
      podman-compose -f $COMPOSE_FILE ps
      echo ""
      echo "App available at: http://localhost:8001"
    }

    dev-logs() {
      podman-compose -f $COMPOSE_FILE logs -f app "$@"
    }

    dev-logs-db() {
      podman-compose -f $COMPOSE_FILE logs -f db "$@"
    }

    dev-shell() {
      podman-compose -f $COMPOSE_FILE exec app sh
    }

    dev-artisan() {
      podman-compose -f $COMPOSE_FILE exec app php artisan "$@"
    }

    dev-composer() {
      podman-compose -f $COMPOSE_FILE exec app composer "$@"
    }

    dev-test() {
      podman-compose -f $COMPOSE_FILE exec -T app php vendor/bin/phpunit "$@"
    }

    dev-db() {
      podman-compose -f $COMPOSE_FILE exec db mariadb \
        -u "''${DB_USERNAME:-anagram}" \
        -p"''${DB_PASSWORD:-anagram}" \
        "''${DB_DATABASE:-anagram_dev}" "$@"
    }

    # ===================
    # WELCOME MESSAGE
    # ===================
    echo ""
    echo "╔═══════════════════════════════════════════════════════════╗"
    echo "║             Anagram Finder Web Dev Environment            ║"
    echo "╚═══════════════════════════════════════════════════════════╝"
    echo ""
    echo "  Podman:  $(podman --version | cut -d' ' -f3)"
    echo ""
    echo "Commands:"
    echo "  dev-up [services]   Start all or specific services"
    echo "  dev-down [-v]       Stop services (-v removes volumes)"
    echo "  dev-rebuild         Fresh start (down -v + up --build)"
    echo "  dev-restart         Restart services"
    echo "  dev-logs            Tail app logs"
    echo "  dev-logs-db         Tail database logs"
    echo "  dev-shell           Shell into app container"
    echo "  dev-artisan <cmd>   Run artisan command"
    echo "  dev-composer <cmd>  Run composer in container"
    echo "  dev-test [path]     Run PHPUnit suite"
    echo "  dev-db [args]       MariaDB client on the dev database"
    echo ""
    echo "Services:"
    echo "  app     Laravel + Vite    http://localhost:8001"
    echo "  db      MariaDB           localhost:3308"
    echo ""

    # ===================
    # AUTO-START PROMPT
    # ===================
    if [ -f "$COMPOSE_FILE" ] && [ -t 0 ]; then
      read -p "Start development containers? (y/N) " -n 1 -r
      echo
      if [[ $REPLY =~ ^[Yy]$ ]]; then
        dev-up
      fi
    fi
  '';
}
