FROM php:8.3-fpm

# 1. CONTOURNEMENT FAI : Forcer HTTPS pour les dépôts Debian (évite l'interception HTTP)
RUN sed -i 's|http://deb.debian.org|https://deb.debian.org|g' /etc/apt/sources.list.d/debian.sources 2>/dev/null || true \
    && sed -i 's|http://security.debian.org|https://security.debian.org|g' /etc/apt/sources.list.d/debian.sources 2>/dev/null || true \
    && sed -i 's|http://|https://|g' /etc/apt/sources.list 2>/dev/null || true

# 2. Installer les dépendances système + extensions PHP
RUN apt-get update && apt-get install -y \
    libpq-dev \
    libzip-dev \
    libpng-dev \
    libjpeg-dev \
    libfreetype6-dev \
    libonig-dev \
    libicu-dev \
    libxml2-dev \
    zip \
    unzip \
    git \
    curl \
    ca-certificates \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# 3. Configurer et installer les extensions PHP (y compris GD pour Excel)
RUN docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j$(nproc) \
        pdo \
        pdo_pgsql \
        zip \
        gd \
        intl \
        mbstring \
        xml

# 4. Installer Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

WORKDIR /var/www/html

# 5. Copier composer.json en premier (optimise le cache Docker)
COPY composer.json composer.lock* ./

# 6. Installer les dépendances PHP
RUN composer install --no-dev --optimize-autoloader --no-interaction --no-scripts || true

# 7. Copier le reste du code
COPY . .

# 8. Finaliser l'installation
RUN composer install --no-dev --optimize-autoloader --no-interaction || true

# 9. Créer .env si absent
RUN if [ ! -f .env ]; then cp .env.example .env 2>/dev/null || touch .env; fi

# 10. Permissions
RUN chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache 2>/dev/null || true \
    && chmod -R 775 /var/www/html/storage /var/www/html/bootstrap/cache 2>/dev/null || true

EXPOSE 8000

CMD ["php", "artisan", "serve", "--host=0.0.0.0", "--port=8000"]

# ... votre Dockerfile existant ...

# Configuration du Cron pour Laravel Scheduler
RUN apt-get update && apt-get install -y cron
RUN echo "* * * * * cd /var/www/html && php artisan schedule:run >> /dev/null 2>&1" | crontab -

# Commande de démarrage : cron + php-fpm
CMD ["sh", "-c", "cron && php-fpm"]