# ---- Stage 1: Build frontend (Vite/React) ----
FROM node:20-alpine AS build

WORKDIR /app

# Install dependencies first (better layer caching)
COPY package*.json ./
RUN npm install

# Copy the rest of the source and build
COPY . .
RUN npm run build

# ---- Stage 2: PHP + Apache to serve built files + api.php ----
FROM php:8.2-apache

# Enable Apache mod_rewrite (useful for SPA routing)
RUN a2enmod rewrite

# Install MySQL PDO extension
RUN docker-php-ext-install pdo pdo_mysql

# Copy built frontend assets from Stage 1
COPY --from=build /app/dist/ /var/www/html/

# Copy api.php (and any other PHP backend files) into web root
COPY api.php /var/www/html/api.php

# Set permissions
RUN chown -R www-data:www-data /var/www/html

EXPOSE 80