#!/bin/bash
# Script to setup and push code to the automatically created Gitea repository
# This script should be run after Gitea is installed and the repository is created

set -e

echo "=== Gitea Repository Setup Script ==="
echo "This script will configure the bakery-ia repository in Gitea"
echo

# Configuration - update these values as needed
GITEA_URL="https://gitea.bakery-ia.local"
GITEA_ADMIN_USER="bakery-admin"
REPO_NAME="bakery-ia"
LOCAL_DIR="/Users/urtzialfaro/Documents/bakery-ia"

# Check if Gitea admin password is set
if [ -z "$GITEA_ADMIN_PASSWORD" ]; then
    echo "Error: GITEA_ADMIN_PASSWORD environment variable is not set"
    echo "Please set it to the admin password you used during Gitea installation"
    exit 1
fi

echo "Checking if Gitea is accessible..."
if ! curl -s -o /dev/null -w "%{http_code}" "$GITEA_URL" | grep -q "200"; then
    echo "Error: Cannot access Gitea at $GITEA_URL"
    echo "Please ensure Gitea is running and accessible"
    exit 1
fi

echo "✓ Gitea is accessible"

echo "Checking if repository $REPO_NAME exists..."
REPO_CHECK=$(curl -s -w "%{http_code}" -u "$GITEA_ADMIN_USER:$GITEA_ADMIN_PASSWORD" \
    "$GITEA_URL/api/v1/repos/$GITEA_ADMIN_USER/$REPO_NAME" | tail -1)

if [ "$REPO_CHECK" != "200" ]; then
    echo "Repository $REPO_NAME does not exist or is not accessible"
    echo "Attempting to create it..."
    
    CREATE_RESPONSE=$(curl -s -w "%{http_code}" -u "$GITEA_ADMIN_USER:$GITEA_ADMIN_PASSWORD" \
        -X POST "$GITEA_URL/api/v1/user/repos" \
        -H "Content-Type: application/json" \
        -d '{
            "name": "'"$REPO_NAME"'",
            "description": "Main repository for Bakery IA project",
            "private": false,
            "auto_init": true,
            "default_branch": "main"
        }')
    
    HTTP_CODE=$(echo "$CREATE_RESPONSE" | tail -1)
    RESPONSE_BODY=$(echo "$CREATE_RESPONSE" | sed '$d')
    
    if [ "$HTTP_CODE" != "201" ]; then
        echo "Error creating repository: HTTP $HTTP_CODE"
        echo "Response: $RESPONSE_BODY"
        exit 1
    fi
    
    echo "✓ Repository $REPO_NAME created successfully"
else
    echo "✓ Repository $REPO_NAME already exists"
fi

echo "Configuring Git repository..."
cd "$LOCAL_DIR"

# Check if this is already a git repository
if [ ! -d ".git" ]; then
    echo "Initializing Git repository..."
    git init
    git branch -M main
else
    echo "Git repository already initialized"
fi

# Configure Git user if not already set
if [ -z "$(git config user.name)" ]; then
    git config user.name "$GITEA_ADMIN_USER"
    git config user.email "admin@bakery-ia.local"
    echo "✓ Configured Git user: $GITEA_ADMIN_USER"
fi

# Set the remote URL
GIT_REMOTE_URL="$GITEA_URL/$GITEA_ADMIN_USER/$REPO_NAME.git"

if git remote | grep -q "origin"; then
    CURRENT_REMOTE=$(git remote get-url origin)
    if [ "$CURRENT_REMOTE" != "$GIT_REMOTE_URL" ]; then
        echo "Updating remote origin to: $GIT_REMOTE_URL"
        git remote set-url origin "$GIT_REMOTE_URL"
    else
        echo "Remote origin is already set correctly"
    fi
else
    echo "Setting remote origin to: $GIT_REMOTE_URL"
    git remote add origin "$GIT_REMOTE_URL"
fi

echo "Checking if there are changes to commit..."
if [ -n "$(git status --porcelain)" ]; then
    echo "Committing changes..."
    git add .
    git commit -m "Initial commit - Bakery IA project setup"
    echo "✓ Changes committed"
else
    echo "No changes to commit"
fi

echo "Pushing to Gitea repository..."
git push --set-upstream origin main

echo "✓ Code pushed successfully to Gitea!"

echo "Repository URL: $GIT_REMOTE_URL"
echo "You can now configure your CI/CD pipelines to use this repository."

echo "=== Setup Complete ==="