# Use the official .NET 9.0 runtime as base image
FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base
WORKDIR /app

# Use the SDK image for building
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
WORKDIR /src

# Copy project files and dependencies
COPY ["TeleBot/TeleBot/TeleBot.csproj", "TeleBot/TeleBot/"]
COPY ["LittleShop.Client/LittleShop.Client.csproj", "LittleShop.Client/"]

# Restore dependencies
RUN dotnet restore "TeleBot/TeleBot/TeleBot.csproj"

# Copy all source code
COPY . .

# Build the application
WORKDIR "/src/TeleBot/TeleBot"
RUN dotnet build "TeleBot.csproj" -c Release -o /app/build

# Publish the application
FROM build AS publish
RUN dotnet publish "TeleBot.csproj" -c Release -o /app/publish /p:UseAppHost=false

# Final runtime image
FROM base AS final
WORKDIR /app

# Create necessary directories
RUN mkdir -p logs
RUN mkdir -p data

# Copy published application
COPY --from=publish /app/publish .

# Set environment variables
ENV DOTNET_ENVIRONMENT=Production
ENV ASPNETCORE_URLS=
ENV TZ=UTC

# Create non-root user for security
RUN adduser --disabled-password --gecos '' --shell /bin/bash --home /app telebot
RUN chown -R telebot:telebot /app
USER telebot

# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
    CMD pgrep -f "dotnet.*TeleBot" > /dev/null || exit 1

# Run the application
ENTRYPOINT ["dotnet", "TeleBot.dll"]