FROM ubuntu:22.04

ENV DEBIAN_FRONTEND=noninteractive
ENV TZ=UTC

# Install system packages + PHP 8.3 from ondrej PPA (same as VPS setup)
RUN apt-get update && apt-get install -y software-properties-common curl gnupg \
    && add-apt-repository ppa:ondrej/php -y \
    && apt-get update && apt-get install -y \
        nginx \
        supervisor \
        git \
        zip \
        unzip \
        php8.3 \
        php8.3-fpm \
        php8.3-mysql \
        php8.3-pgsql \
        php8.3-mbstring \
        php8.3-xml \
        php8.3-bcmath \
        php8.3-curl \
        php8.3-zip \
        php8.3-gd \
        php8.3-intl \
        php8.3-cli \
        php8.3-tokenizer \
        php8.3-fileinfo \
        php8.3-opcache \
    && curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \
    && apt-get install -y nodejs \
    && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /var/www/html

# Copy project files
COPY . .

# Create required Laravel directories that may be missing from git
RUN mkdir -p storage/framework/sessions \
    && mkdir -p storage/framework/views \
    && mkdir -p storage/framework/cache/data \
    && mkdir -p storage/logs \
    && mkdir -p bootstrap/cache \
    && chmod -R 775 storage bootstrap/cache

# Install PHP dependencies
RUN composer install --no-dev --optimize-autoloader --no-interaction

# Install and build frontend
RUN npm install && npm run build

# Nginx config
COPY docker/nginx.conf /etc/nginx/nginx.conf

# Supervisor config
COPY docker/supervisord.conf /etc/supervisor/conf.d/supervisord.conf

# Startup script
COPY docker/start.sh /start.sh
RUN chmod +x /start.sh

# Permissions
RUN chown -R www-data:www-data /var/www/html/storage \
    && chown -R www-data:www-data /var/www/html/bootstrap/cache \
    && chmod -R 775 /var/www/html/storage \
    && chmod -R 775 /var/www/html/bootstrap/cache

EXPOSE 80

CMD ["/start.sh"]
