FROM hyperf/hyperf:8.3-alpine-v3.19-swoole-v6

USER root

# Install system deps
RUN apk add --no-cache \
    postgresql-dev \
    php83-pdo_pgsql \
    php83-pgsql \
    7zip \
    curl

# Install Node
RUN curl -fsSL https://unofficial-builds.nodejs.org/download/release/v20.19.0/node-v20.19.0-linux-x64-musl.tar.gz \
    | tar -xz -C /usr/local --strip-components=1 \
    && node -v \
    && npm -v

# Enable PHP extensions
RUN echo "extension=pdo_pgsql.so" > /etc/php83/conf.d/01_pdo_pgsql.ini && \
    echo "extension=pgsql.so" > /etc/php83/conf.d/00_pgsql.ini

WORKDIR /var/app

# ✅ Copy composer files FIRST (for caching + correctness)
COPY composer.json composer.lock ./
RUN composer install --no-dev --optimize-autoloader

# ✅ Copy package files and install frontend deps
COPY package*.json ./
RUN npm install

# ✅ Copy the rest of the app
COPY . .

# Build frontend
RUN npm run build

EXPOSE 9501

CMD ["php", "artisan", "serve"]