# ==========================================
# Multi-stage Dockerfile voor Jouwdriedee.nl (Coolify / CasaOS)
# ==========================================

# 1. Frontend Build stage (Vite & Tailwind CSS)
FROM node:20-alpine AS node_builder
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

# 2. PHP Production runtime stage
FROM dunglas/frankenphp:php8.3-alpine AS runner

ENV SERVER_NAME="http://"
ENV APP_ENV="production"
ENV APP_DEBUG="false"

WORKDIR /app

# Installeer benodigde PHP extensies voor Laravel & SQLite/MySQL
RUN install-php-extensions \
    pdo_mysql \
    pdo_sqlite \
    sqlite3 \
    gd \
    intl \
    zip \
    bcmath \
    opcache

# Installeer Composer
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer

# Kopieer applicatiebestanden
COPY . /app
COPY --from=node_builder /app/public/build /app/public/build

# Installeer backend dependencies
RUN composer install --no-dev --no-interaction --prefer-dist --optimize-autoloader

# Zorg voor de juiste opslagpermissies en database bestand
RUN mkdir -p /app/storage /app/bootstrap/cache /app/database \
    && touch /app/database/database.sqlite \
    && chown -R www-data:www-data /app/storage /app/bootstrap/cache /app/database \
    && chmod -R 775 /app/storage /app/bootstrap/cache /app/database

EXPOSE 80

CMD ["sh", "-c", "php artisan storage:link && mkdir -p /app/database /app/storage/framework/cache /app/storage/framework/sessions /app/storage/framework/views /app/storage/logs && touch /app/database/database.sqlite && chmod -R 777 /app/database /app/storage && php artisan migrate --force && php artisan db:seed --force && frankenphp run --config /etc/caddy/Caddyfile"]

