# 基礎映像：Ubuntu 24.04
FROM ubuntu:24.04

# 環境變數
ARG NODE_VERSION=24
ENV TZ="Asia/Taipei"
ENV LANG=zh_TW.UTF-8
ENV LC_ALL=zh_TW.UTF-8
WORKDIR /var/www/html

# 安裝系統基礎工具
RUN apt-get update && apt-get install -y \
    software-properties-common ca-certificates lsb-release apt-transport-https \
    zip unzip git curl vim wget locales supervisor \
    apache2 apache2-utils \
    mysql-client \
    python3 \
    && rm -rf /var/lib/apt/lists/*

# 設定 Locale
RUN locale-gen zh_TW.UTF-8 && update-locale LANG=zh_TW.UTF-8

# 安裝 PHP 8.3 與常用擴充
RUN add-apt-repository ppa:ondrej/php \
    && apt-get update && apt-get install -y \
    php8.3-fpm php8.3-curl php8.3-mbstring php8.3-gd php8.3-zip php8.3-xml \
    php8.3-bcmath php8.3-ldap php8.3-mysql php8.3-sqlite3 php8.3-opcache \
    && a2dismod mpm_prefork \
    && a2enmod mpm_event proxy_fcgi setenvif rewrite headers http2 ssl \
    && a2enconf php8.3-fpm \
    && rm -rf /var/lib/apt/lists/*

# 安裝 Composer
RUN curl -sS https://getcomposer.org/installer | php -- \
    --install-dir=/usr/local/bin --filename=composer

# 安裝 Node.js
RUN curl -fsSL https://deb.nodesource.com/setup_${NODE_VERSION}.x | bash - \
    && apt-get install -y nodejs \
    && npm install -g pnpm \
    && rm -rf /var/lib/apt/lists/*

# 產生預設自我簽署憑證（可透過 docker-compose 掛載覆蓋）
RUN openssl req -x509 -nodes -days 365 \
    -subj "/C=TW/ST=Taipei/L=Taipei/O=MyCompany/OU=Prod/CN=localhost" \
    -newkey rsa:2048 \
    -keyout /etc/ssl/private/ssl.key \
    -out /etc/ssl/certs/ssl.crt

# 複製 Apache 設定檔
COPY ./apache-data/000-default.conf /etc/apache2/sites-available/000-default.conf
COPY ./apache-data/default-ssl.conf /etc/apache2/sites-available/default-ssl.conf

# 複製 Supervisor 設定檔
COPY ./conf/supervisord.conf /etc/supervisor/supervisord.conf
COPY ./conf/web.conf /etc/supervisor/conf.d/web.conf

# 啟用 Apache 站台
RUN a2ensite 000-default.conf && a2ensite default-ssl.conf

# 啟動 Supervisor
CMD ["/usr/bin/supervisord", "-n", "-c", "/etc/supervisor/supervisord.conf"]

EXPOSE 80 443
