#!/usr/bin/env bash
# init-gitea-courses.sh — Create the courses repo in Gitea and push initial content
# Run this when Gitea is available.
# Usage: GITEA_TOKEN=xxx bash scripts/init-gitea-courses.sh

set -euo pipefail

GITEA_URL="${GITEA_URL:-https://git.falahos.my}"
GITEA_TOKEN="${GITEA_TOKEN:?GITEA_TOKEN is required}"
REPO_OWNER="maifors"
REPO_NAME="courses"
WORKDIR="/tmp/courses-repo"

echo "=== Initializing Courses Repo ==="

# Create repo via Gitea API
echo "Creating repo $REPO_OWNER/$REPO_NAME..."
curl -s -X POST "$GITEA_URL/api/v1/admin/users/$REPO_OWNER/repos" \
  -H "Authorization: token $GITEA_TOKEN" \
  -H "Content-Type: application/json" \
  -d "{\"name\": \"$REPO_NAME\", \"description\": \"Falah Academy micro learning course content\", \"private\": false, \"auto_init\": false}" \
  || echo "Repo may already exist, continuing..."

# Clone or create local working directory
rm -rf "$WORKDIR"
mkdir -p "$WORKDIR"
cd "$WORKDIR"

git init
git config user.name "Falah Academy Bot"
git config user.email "academy@falahos.my"

# ─── Course Index ───
mkdir -p courses
cat > courses/course-index.yaml << 'INDEXEOF'
courses:
  - slug: "atomic-habits"
    title: "Atomic Habits"
    author: "James Clear"
  - slug: "deep-work"
    title: "Deep Work"
    author: "Cal Newport"
  - slug: "7-habits"
    title: "The 7 Habits of Highly Effective People"
    author: "Stephen R. Covey"
INDEXEOF

# ─── Atomic Habits Course ───
COURSE="courses/atomic-habits"
mkdir -p "$COURSE"

cat > "$COURSE/course.yaml" << 'COURSEEOF'
slug: "atomic-habits"
title: "Atomic Habits"
author: "James Clear"
description: "Master the science of habit formation in just 25 minutes."
imageUrl: ""
moduleCount: 5
totalMinutes: 25
difficulty: "beginner"
priceFlh: 499
priceUsd: 4.99
subscriptionOnly: false
published: true
modules:
  - slug: "the-1-percent-rule"
    order: 1
  - slug: "identity-based-habits"
    order: 2
  - slug: "the-4-laws"
    order: 3
  - slug: "habit-stacking"
    order: 4
  - slug: "design-your-environment"
    order: 5
COURSEEOF

# Module 1: The 1% Rule
MODULE="$COURSE/the-1-percent-rule"
mkdir -p "$MODULE"
cat > "$MODULE/module.yaml" << 'YAMLEOF'
title: "The 1% Rule"
order: 1
keyTakeaway: "Small habits don't just add up — they compound. Improving just 1% every day leads to being 37x better after one year."
duration: 5
YAMLEOF

cat > "$MODULE/lesson.md" << 'MDEOF'
# The 1% Rule

Habits are the compound interest of self-improvement. The same way that money multiplies through compound interest, the effects of your habits multiply as you repeat them.

## The Math of Small Changes

If you get 1% better each day for one year, you'll end up 37 times better. Conversely, getting 1% worse each day for a year will decline you nearly to zero.

Success is the product of daily habits — not once-in-a-lifetime transformations.
MDEOF

cat > "$MODULE/quiz.yaml" << 'QUIZEOF'
questions:
  - question: "If you get 1% better every day, how much better after one year?"
    options:
      - "About 3x better"
      - "About 37x better"
      - "About 10x better"
      - "About 100x better"
    correctIndex: 1
  - question: "What is the 'Valley of Disappointment'?"
    options:
      - "A period where habits feel boring"
      - "The phase where you work but see no results"
      - "The time between starting a new habit"
      - "When you lose motivation completely"
    correctIndex: 1
QUIZEOF

# Module 5: Design Your Environment
MODULE="$COURSE/design-your-environment"
mkdir -p "$MODULE"
cat > "$MODULE/module.yaml" << 'YAMLEOF'
title: "Design Your Environment"
order: 5
keyTakeaway: "Your surroundings shape your behavior more than willpower. Design your environment for success by reducing friction for good habits."
duration: 5
YAMLEOF

cat > "$MODULE/lesson.md" << 'MDEOF'
# Design Your Environment

Willpower is overrated. The most reliable way to stick to good habits is to design your environment so that the right choice is the easy choice.
MDEOF

cat > "$MODULE/quiz.yaml" << 'QUIZEOF'
questions:
  - question: "What is a commitment device?"
    options:
      - "A promise to yourself"
      - "A present choice that locks in better future behavior"
      - "A device that tracks habits"
      - "An accountability partner"
    correctIndex: 1
QUIZEOF

# ─── Commit & Push ───
git add -A
git commit -m "feat: initial course content - Atomic Habits"

git remote add origin "$GITEA_URL/$REPO_OWNER/$REPO_NAME.git"
git branch -M main
git push -u origin main

echo ""
echo "=== Done! Repo: $GITEA_URL/$REPO_OWNER/$REPO_NAME ==="
echo "Configure Gitea webhook to POST to https://falahos.my/api/learn/sync"
echo "  with secret matching SYNC_SECRET env var."
