#!/bin/bash

# Soul创业实验 - GitHub Token 配置脚本

clear

echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "   🔑 GitHub Personal Access Token 配置"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
echo "📝 步骤1：创建Token"
echo "   1. 访问：https://github.com/settings/tokens/new"
echo "   2. Note: 填写 'Soul创业实验项目'"
echo "   3. Expiration: 选择 'No expiration'"
echo "   4. 勾选权限："
echo "      ☑️  repo (全部子选项)"
echo "      ☑️  workflow"
echo "      ☑️  write:packages"
echo "      ☑️  delete:packages"
echo "   5. 点击 'Generate token'"
echo "   6. 复制Token（以ghp_开头）"
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

# 询问是否已创建Token
read -p "❓ 是否已经创建好Token? (y/n): " ready

if [ "$ready" != "y" ] && [ "$ready" != "Y" ]; then
  echo ""
  echo "👉 请先创建Token，然后重新运行此脚本"
  echo "🔗 https://github.com/settings/tokens/new"
  exit 0
fi

echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "   📋 步骤2：粘贴Token"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
read -sp "🔐 请粘贴你的Token: " token
echo ""

# 验证Token格式
if [ -z "$token" ]; then
  echo ""
  echo "❌ Token不能为空"
  exit 1
fi

if [[ ! $token =~ ^ghp_ ]] && [[ ! $token =~ ^github_pat_ ]]; then
  echo ""
  echo "⚠️  Token格式可能不正确"
  echo "   正确格式应该是："
  echo "   - ghp_xxxxxxxxxxxx（classic token）"
  echo "   - github_pat_xxxxxxxxxxxx（fine-grained token）"
  echo ""
  read -p "是否继续? (y/n): " continue
  if [ "$continue" != "y" ] && [ "$continue" != "Y" ]; then
    exit 0
  fi
fi

echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "   ⚙️  步骤3：配置Git"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

cd "/Users/karuo/Documents/开发/3、自营项目/一场soul的创业实验"

echo "📝 配置远程仓库..."
git remote set-url origin "https://${token}@github.com/fnvtk/Mycontent.git"

if [ $? -eq 0 ]; then
  echo "✅ Git配置成功"
else
  echo "❌ Git配置失败"
  exit 1
fi

echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "   🧪 步骤4：测试推送"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

echo "🚀 测试推送到GitHub..."
git push origin soul-content 2>&1

if [ $? -eq 0 ]; then
  echo ""
  echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
  echo "   🎉 配置成功！"
  echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
  echo ""
  echo "✅ Token配置成功"
  echo "✅ 代码推送成功"
  echo ""
  echo "🔗 查看仓库："
  echo "   https://github.com/fnvtk/Mycontent/tree/soul-content"
  echo ""
  echo "📝 下次上传只需运行："
  echo "   ./quick-push.sh \"提交信息\""
  echo ""
else
  echo ""
  echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
  echo "   ❌ 推送失败"
  echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
  echo ""
  echo "可能的原因："
  echo "1. Token权限不足"
  echo "   - 请确保勾选了 repo, workflow 权限"
  echo ""
  echo "2. Token已过期"
  echo "   - 请重新创建Token"
  echo ""
  echo "3. 网络连接问题"
  echo "   - 请检查网络连接"
  echo ""
  echo "4. 仓库权限问题"
  echo "   - 请确保你是仓库的所有者或协作者"
  echo ""
  echo "📖 详细帮助："
  echo "   查看 🔑GitHub权限配置指南.md"
  echo ""
  exit 1
fi
