# ════════════════════════════════════════════════════════════════
# 光湖 · Forgejo 品牌主题部署
# 部署光湖品牌 UI（深色主题 CSS）到 Forgejo 自定义目录
# ════════════════════════════════════════════════════════════════
name: 🎨 光湖品牌主题

on:
  workflow_dispatch:
    inputs:
      action:
        description: '操作: 部署 / 预览CSS'
        default: '部署'

jobs:
  deploy-theme:
    runs-on: ubuntu
    steps:
      - name: 同步最新代码
        run: |
          cd /data/guanghulab/repo
          git fetch origin
          git reset --hard origin/main
          echo "✅ 代码已同步: $(git log --oneline -1)"

      - name: 找到并复制品牌 CSS
        run: |
          CSS_SRC="/data/guanghulab/repo/style-system/forgejo/forgejo-custom.css"
          CSS_DST="/var/lib/forgejo/custom/public/css/custom.css"

          if [ ! -f "$CSS_SRC" ]; then
            # 尝试其他路径
            CSS_SRC="/data/guanghulab/repo/style-system/forgejo-custom.css"
          fi

          if [ ! -f "$CSS_SRC" ]; then
            echo "❌ 品牌 CSS 未找到"
            find /data/guanghulab/repo/style-system -name "*.css" 2>/dev/null
            exit 1
          fi

          echo "✅ 找到品牌 CSS: $CSS_SRC"

          # 确保目标目录存在
          mkdir -p "$(dirname "$CSS_DST")"

          # 复制 CSS
          cp "$CSS_SRC" "$CSS_DST"
          chmod 644 "$CSS_DST"
          echo "✅ 品牌 CSS 已部署到: $CSS_DST"

          # 显示 CSS 信息
          echo ""
          echo "═══ 部署信息 ═══"
          echo "文件: $(basename $CSS_SRC)"
          echo "大小: $(wc -c < $CSS_SRC) bytes"
          echo "行数: $(wc -l < $CSS_SRC)"
          echo "目标: $CSS_DST"

      - name: 验证部署
        run: |
          echo "═══ 验证 ═══"
          CSS_DST="/var/lib/forgejo/custom/public/css/custom.css"
          if [ -f "$CSS_DST" ]; then
            echo "✅ CSS 文件存在: $CSS_DST"
            echo "   $(wc -c < $CSS_DST) bytes"
          else
            echo "⚠️ CSS 文件未找到，尝试其他路径"
            echo ""
            echo "正在搜索 Forgejo 自定义目录..."
            for dir in /var/lib/forgejo /data/forgejo /home/forgejo /opt/forgejo /etc/forgejo; do
              if [ -d "$dir" ]; then
                echo "  找到: $dir"
                ls -la "$dir/custom/public/css/" 2>/dev/null || echo "  无 custom/public/css/"
              fi
            done
          fi

      - name: 输出结果
        run: |
          echo ""
          echo "═══════════════════════════════════════"
          echo "🎨 光湖品牌主题已部署！"
          echo ""
          echo "刷新 Forgejo 页面即可看到效果："
          echo "  https://guanghulab.com/"
          echo ""
          echo "如需撤销：删除以下文件后重启 Forgejo"
          echo "  rm /var/lib/forgejo/custom/public/css/custom.css"
          echo "  systemctl restart forgejo"
          echo "═══════════════════════════════════════"
