FROM php:8.4-apache

RUN apt-get update && apt-get install -y --no-install-recommends \
        libpq-dev curl \
    && docker-php-ext-install pgsql pdo pdo_pgsql \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

RUN a2enmod rewrite headers

RUN sed -i 's/Listen 80$/Listen 8080/' /etc/apache2/ports.conf && \
    sed -i 's/<VirtualHost \*:80>/<VirtualHost *:8080>/' \
        /etc/apache2/sites-enabled/000-default.conf && \
    echo "ServerName localhost" >> /etc/apache2/apache2.conf

WORKDIR /var/www/html/assets

RUN curl -fsSL "https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" \
        -o bootstrap.min.css && \
    curl -fsSL "https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" \
        -o bootstrap.bundle.min.js && \
    curl -fsSL "https://cdn.jsdelivr.net/npm/chart.js@4.4.4/dist/chart.umd.min.js" \
        -o chart.umd.min.js && \
    curl -fsSL "https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css" \
        -o bootstrap-icons.min.css && \
    mkdir -p fonts && \
    curl -fsSL "https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/fonts/bootstrap-icons.woff2" \
        -o fonts/bootstrap-icons.woff2 && \
    curl -fsSL "https://unpkg.com/leaflet@1.9.4/dist/leaflet.js" \
        -o leaflet.js && \
    curl -fsSL "https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" \
        -o leaflet.css && \
    mkdir -p leaflet && \
    curl -fsSL "https://unpkg.com/leaflet@1.9.4/dist/images/marker-icon.png" \
        -o leaflet/marker-icon.png && \
    curl -fsSL "https://unpkg.com/leaflet@1.9.4/dist/images/marker-shadow.png" \
        -o leaflet/marker-shadow.png

# ── Drapeaux nationaux (onglets Départs / Zones) ──────────────────────────────
# Source flagcdn.com : domaine public, libre d'usage sans attribution. Les
# drapeaux d'État ne sont pas couverts par le droit d'auteur.
#
# Téléchargés ici plutôt que seulement copiés depuis src/, pour qu'un build sur
# une machine neuve fonctionne sans étape préalable. Le COPY src/ qui suit
# fusionne avec ce dossier : des drapeaux déposés à la main dans
# src/assets/flags/ restent donc pris en compte et écrasent ceux-ci.
#
# Le `|| true` final est délibéré : un drapeau manquant retombe sur le
# pictogramme « pays inconnu » côté JS, ce n'est pas un motif d'échec du build.
RUN mkdir -p flags && cd flags && \
    for c in fr de es it pt be nl lu ch at gb ie dk no se fi is pl cz sk hu ro \
             bg gr hr si rs ba al mt cy ee lv lt ua by ru tr us ca mx br ar cl \
             co pe ve cn jp kr in pk th vn sg my id ph au nz il sa qa ae kw bh \
             om jo lb eg ma dz tn ly za ng ke et gh sn ci cm ga mc sm ad li im \
             gg je bm ky aw bs pa cr do cu jm kz uz az ge am md me mk ir iq sy \
             af bd lk np mm kh la mn tw hk mo ; do \
        curl -fsSL --max-time 10 "https://flagcdn.com/24x18/${c}.png" \
             -o "${c}.png" || rm -f "${c}.png" ; \
    done ; \
    echo "$(ls -1 *.png 2>/dev/null | wc -l) drapeau(x) embarqué(s)" ; \
    true

WORKDIR /var/www/html
COPY src/ /var/www/html/

RUN sed -i 's|AllowOverride None|AllowOverride All|g' \
        /etc/apache2/apache2.conf

RUN chown -R www-data:www-data /var/www/html

EXPOSE 8080
CMD ["apache2-foreground"]
