#!/bin/bash
# Skrypt commitowania projektu na Gitea
# Użycie: ./git-init.sh [gitea_user] [gitea_password] [gitea_repo]

set -e
# set -x  # Odkomentuj dla debugowania

# Parametry
GITEA_USER="${1:-ms}"
GITEA_PASS="${2:-}"
GITEA_REPO="${3:-ksef-pdf-generator}"
GITEA_HOST="10.1.1.1:30008"
GITEA_URL="https://${GITEA_USER}:${GITEA_PASS}@${GITEA_HOST}/${GITEA_USER}/${GITEA_REPO}.git"
GITEA_URL_DISPLAY="https://${GITEA_HOST}/${GITEA_USER}/${GITEA_REPO}.git"

# Kolory
YELLOW='\033[1;33m'
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m'

# Sprawdzenie hasła
if [ -z "$GITEA_PASS" ]; then
  echo -e "${RED}❌ Error: Gitea password required${NC}"
  echo "Użycie: ./git-init.sh [user] [password] [repo]"
  echo ""
  echo "Przykład: ./git-init.sh ms YOUR_PASSWORD ksef-pdf-generator"
  exit 1
fi

echo -e "${YELLOW}🚀 Git initialization for Gitea${NC}"
echo "Host: https://${GITEA_HOST}"
echo "User: ${GITEA_USER}"
echo "Repo: ${GITEA_REPO}"
echo ""

# Krok 1: Konfiguracja git (ignorowanie cert dla self-signed)
echo -e "${YELLOW}1️⃣  Configuring git...${NC}"
git config --global http.sslVerify false
git config user.name "Mirek SIC"
git config user.email "mirek@sic.pl"
git config --global credential.helper store  # Cache credentials
echo -e "${GREEN}✓ Git configured${NC}"
echo ""

# Krok 2: Inicjalizacja repozytorium
echo -e "${YELLOW}2️⃣  Initializing repository...${NC}"
if [ ! -d ".git" ]; then
  git init
  git checkout -b main
else
  echo "Repository already initialized"
fi
echo -e "${GREEN}✓ Repository ready${NC}"
echo ""

# Krok 3: Dodanie remote
echo -e "${YELLOW}3️⃣  Adding remote origin...${NC}"
if git remote | grep -q origin; then
  echo "Removing existing origin..."
  git remote remove origin
fi
# Używaj URL z hasłem dla push, ale wyświetlaj bez hasła
git remote add origin "$GITEA_URL"
echo -e "${GREEN}✓ Remote added: $GITEA_URL_DISPLAY${NC}"
echo ""

# Krok 4: Stage i commit
echo -e "${YELLOW}4️⃣  Staging and committing...${NC}"
git add .
git commit -m "Initial commit: KSEF PDF Generator from GitHub" --allow-empty || true
echo -e "${GREEN}✓ Changes committed${NC}"
echo ""

# Krok 5: Push do Gitea (z timeout)
echo -e "${YELLOW}5️⃣  Pushing to Gitea...${NC}"
echo "Uwaga: pierwsze push może potrwać kilka sekund..."

if timeout 60 git push -u origin main 2>&1; then
  echo -e "${GREEN}✓ Push successful${NC}"
  echo ""
  echo -e "${GREEN}✅ Git setup complete!${NC}"
  echo ""
  echo "Repository: $GITEA_URL_DISPLAY"
  echo ""
  echo "Dalsze komendy:"
  echo "  git status"
  echo "  git log --oneline"
  echo "  git push origin main"
  echo "  git pull origin main"
else
  EXIT_CODE=$?
  echo ""
  if [ $EXIT_CODE -eq 124 ]; then
    echo -e "${RED}❌ Error: Push timeout (60 sekund)${NC}"
  else
    echo -e "${RED}❌ Error: Push failed (exit code: $EXIT_CODE)${NC}"
  fi
  echo ""
  echo "Debug info:"
  echo "  URL: $GITEA_URL_DISPLAY"
  echo "  User: $GITEA_USER"
  echo ""
  echo "Opcje debugowania:"
  echo "  GIT_TRACE=1 ./git-init.sh $GITEA_USER $GITEA_PASS $GITEA_REPO"
  echo "  git remote -v"
  echo "  git log --oneline"
  exit 1
fi
echo "  git push origin main"
echo "  git pull origin main"
