FROM php:8.2-fpm-alpine

# Install system dependencies
RUN apk add --no-cache \
    git curl unzip icu-dev libzip-dev libpng-dev libjpeg-turbo-dev \
    freetype-dev libxml2-dev oniguruma-dev linux-headers \
    imagemagick-dev $PHPIZE_DEPS caddy \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j$(nproc) \
        bcmath ctype fileinfo gd intl mbstring \
        pdo pdo_mysql opcache pcntl sockets zip xml \
    && pecl install redis imagick \
    && docker-php-ext-enable redis imagick \
    && apk del $PHPIZE_DEPS linux-headers \
    && rm -rf /tmp/*

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

# Set working directory
WORKDIR /var/www/html

# Copy application code
COPY . .

# Install Laravel dependencies
RUN php /usr/bin/composer install \
    --no-dev --optimize-autoloader --no-interaction --no-progress \
    --no-scripts --ignore-platform-req=ext-ssh2

# Create required directories and set permissions
RUN mkdir -p \
    storage/framework/sessions \
    storage/framework/views \
    storage/framework/cache/data \
    storage/logs \
    bootstrap/cache \
    && chown -R www-data:www-data storage bootstrap/cache \
    && chmod -R 775 storage bootstrap/cache

# Caddy config for Laravel
COPY docker/Caddyfile /etc/caddy/Caddyfile

# Entrypoint
COPY docker/entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

EXPOSE 8088

ENTRYPOINT ["/entrypoint.sh"]
