#!/bin/bash

# 获取 Apple Developer Team ID 的脚本

set -e

# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 日志函数
log_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

log_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

log_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

log_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# 检查 Apple ID 和密码
check_credentials() {
    if [ -z "$APPLE_ID" ] || [ -z "$APPLE_PASSWORD" ]; then
        log_error "请先设置 APPLE_ID 和 APPLE_PASSWORD 环境变量"
        log_info "运行: export APPLE_ID='your-apple-id@example.com'"
        log_info "运行: export APPLE_PASSWORD='your-app-password'"
        exit 1
    fi
    
    log_info "使用 Apple ID: $APPLE_ID"
}

# 方法1: 通过 xcrun altool 获取
get_team_id_altool() {
    log_info "尝试通过 xcrun altool 获取 Team ID..."
    
    local output
    if output=$(xcrun altool --list-providers -u "$APPLE_ID" -p "$APPLE_PASSWORD" 2>&1); then
        local team_id=$(echo "$output" | grep -o 'Team ID: [A-Z0-9]*' | head -1 | cut -d' ' -f3)
        if [ -n "$team_id" ]; then
            echo "$team_id"
            return 0
        fi
    fi
    
    return 1
}

# 方法2: 通过 xcodebuild 获取
get_team_id_xcodebuild() {
    log_info "尝试通过 xcodebuild 获取 Team ID..."
    
    # 检查是否有 Xcode 项目
    if [ -f "ios/Runner.xcodeproj/project.pbxproj" ]; then
        local team_id=$(grep -o 'DEVELOPMENT_TEAM = [A-Z0-9]*' ios/Runner.xcodeproj/project.pbxproj | head -1 | cut -d' ' -f3)
        if [ -n "$team_id" ]; then
            echo "$team_id"
            return 0
        fi
    fi
    
    return 1
}

# 方法3: 通过开发者证书获取
get_team_id_certificates() {
    log_info "尝试通过开发者证书获取 Team ID..."
    
    local identities=$(security find-identity -v -p codesigning 2>/dev/null)
    if [ $? -eq 0 ] && [ -n "$identities" ]; then
        local team_id=$(echo "$identities" | grep -o '([A-Z0-9]*)' | head -1 | tr -d '()')
        if [ -n "$team_id" ]; then
            echo "$team_id"
            return 0
        fi
    fi
    
    return 1
}

# 方法4: 手动输入
get_team_id_manual() {
    log_warning "无法自动获取 Team ID"
    log_info "请手动输入您的 Team ID："
    log_info "1. 登录 https://developer.apple.com"
    log_info "2. 进入 'Account' -> 'Membership'"
    log_info "3. 查看 'Team ID' 字段"
    echo ""
    read -p "请输入您的 Team ID: " team_id
    
    if [ -n "$team_id" ]; then
        echo "$team_id"
        return 0
    else
        return 1
    fi
}

# 更新配置文件
update_config() {
    local team_id=$1
    
    if [ -z "$team_id" ]; then
        log_error "Team ID 为空"
        return 1
    fi
    
    log_info "更新 ios_signing_config.sh 文件..."
    
    # 备份原文件
    cp ios_signing_config.sh ios_signing_config.sh.backup
    
    # 更新 Team ID
    sed -i '' "s/export TEAM_ID=\"YOUR_TEAM_ID\"/export TEAM_ID=\"$team_id\"/" ios_signing_config.sh
    
    # 更新签名身份
    sed -i '' "s/export SIGNING_IDENTITY=\"iPhone Developer: Your Name (YOUR_TEAM_ID)\"/export SIGNING_IDENTITY=\"iPhone Developer: Your Name ($team_id)\"/" ios_signing_config.sh
    sed -i '' "s/export DISTRIBUTION_IDENTITY=\"iPhone Distribution: Your Name (YOUR_TEAM_ID)\"/export DISTRIBUTION_IDENTITY=\"iPhone Distribution: Your Name ($team_id)\"/" ios_signing_config.sh
    
    log_success "配置文件已更新"
    log_info "Team ID: $team_id"
}

# 主函数
main() {
    log_info "开始获取 Apple Developer Team ID..."
    log_info "=========================================="
    
    check_credentials
    
    local team_id=""
    
    # 尝试各种方法获取 Team ID
    if team_id=$(get_team_id_altool); then
        log_success "通过 xcrun altool 获取到 Team ID: $team_id"
    elif team_id=$(get_team_id_xcodebuild); then
        log_success "通过 xcodebuild 获取到 Team ID: $team_id"
    elif team_id=$(get_team_id_certificates); then
        log_success "通过开发者证书获取到 Team ID: $team_id"
    else
        team_id=$(get_team_id_manual)
        if [ $? -eq 0 ]; then
            log_success "手动输入 Team ID: $team_id"
        else
            log_error "无法获取 Team ID"
            exit 1
        fi
    fi
    
    # 更新配置文件
    update_config "$team_id"
    
    log_success "=========================================="
    log_success "Team ID 获取完成！"
    log_success "=========================================="
    log_info "现在可以运行: source ios_signing_config.sh"
    log_info "然后运行: ./build_ios_dmg.sh"
    log_success "=========================================="
}

# 运行主函数
main "$@"
