{ pkgs ? import <nixpkgs> {} }:

pkgs.mkShell {
  buildInputs = with pkgs; [
    # PHP and tools
    php83
    php83Packages.composer

    # Node.js and npm
    nodejs_22

    # Container tools
    podman
    podman-compose

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

    # Redis client
    redis

    # Utilities
    git
    curl
    gnumake
  ];

  shellHook = ''
    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:8000"
    }

    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)..."
      podman-compose -f $COMPOSE_FILE down -v
      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:8000"
    }

    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 "$@"
    }

    # ===================
    # BUILD COMMANDS
    # ===================
    base-build() {
      local image="forge.lvl0.xyz/lvl0/fedi-feed-router-base:latest"

      # Check if logged in, prompt if not
      if ! podman login --get-login forge.lvl0.xyz &>/dev/null; then
        echo "Not logged in to forge.lvl0.xyz"
        podman login forge.lvl0.xyz || return 1
      fi

      echo "Building base image: $image"
      if ! podman build -t "$image" -f docker/build/Dockerfile.base .; then
        echo "Build failed!"
        return 1
      fi

      echo ""
      echo "Pushing to registry..."
      if ! podman push "$image"; then
        echo "Push failed!"
        return 1
      fi

      echo ""
      echo "Done! Base image pushed: $image"
    }

    # ===================
    # WELCOME MESSAGE
    # ===================
    echo ""
    echo "╔═══════════════════════════════════════════════════════════╗"
    echo "║                   FFR 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)"
    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 "  base-build          Build and push base image"
    echo ""
    echo "Services:"
    echo "  app     Laravel + Vite    http://localhost:8000"
    echo "  db      MariaDB           localhost:3307"
    echo "  redis   Redis             localhost:6380"
    echo ""
  '';
}
