import chalk from "chalk";
import { execSync } from "child_process";
export async function cloudCheckPR(options) {
    const { prNumber, repo, debug = false } = options;
    if (debug) {
        console.log(chalk.gray("[DEBUG] Starting cloud check-pr"));
        console.log(chalk.gray(`[DEBUG] PR number: ${prNumber || "auto-detect"}`));
        console.log(chalk.gray(`[DEBUG] Repo: ${repo || "auto-detect"}`));
    }
    // Step 1: Get repo info
    console.log(chalk.blue("📦 Getting repository information..."));
    const repoInfo = await getRepoInfo(repo, debug);
    if (!repoInfo) {
        console.error(chalk.red("❌ Failed to get repository information"));
        console.error(chalk.yellow("Make sure you're in a git repository with a GitHub remote"));
        process.exit(1);
    }
    console.log(chalk.green(`✓ Repository: ${repoInfo.owner}/${repoInfo.name}`));
    // Step 2: Get PR number (from arg or detect from current branch)
    const prNum = prNumber || (await detectPRNumber(repoInfo, debug));
    if (!prNum) {
        console.error(chalk.red("❌ No PR number provided and couldn't detect from current branch"));
        console.error(chalk.yellow("Usage: dev3000 cloud check-pr <pr-number>"));
        process.exit(1);
    }
    console.log(chalk.blue(`🔍 Fetching PR #${prNum} details...`));
    // Step 3: Fetch PR details from GitHub
    const prDetails = await fetchPRDetails(repoInfo.owner, repoInfo.name, prNum, debug);
    if (!prDetails) {
        console.error(chalk.red(`❌ Failed to fetch PR #${prNum}`));
        process.exit(1);
    }
    console.log(chalk.green(`✓ PR #${prNum}: ${prDetails.title}`));
    console.log(chalk.gray(`  Branch: ${prDetails.branch}`));
    console.log(chalk.gray(`  Author: ${prDetails.author}`));
    // Step 4: Find Vercel preview URL
    console.log(chalk.blue("🔗 Finding Vercel preview URL..."));
    const previewUrl = await findVercelPreview(repoInfo, prDetails.branch, debug);
    if (!previewUrl) {
        console.error(chalk.red("❌ No Vercel preview deployment found for this PR"));
        console.error(chalk.yellow("Make sure the PR has a Vercel deployment"));
        process.exit(1);
    }
    console.log(chalk.green(`✓ Preview URL: ${previewUrl}`));
    // Step 5: Analyze PR changes to determine what pages to check
    console.log(chalk.blue("📝 Analyzing PR changes..."));
    const changedFiles = await getChangedFiles(repoInfo.owner, repoInfo.name, prNum, debug);
    console.log(chalk.gray(`  Changed files: ${changedFiles.length}`));
    // Step 6: Run workflow to check the PR
    console.log(chalk.blue("🤖 Running AI-powered PR verification..."));
    console.log(chalk.gray("This may take a few minutes..."));
    const result = await runPRCheckWorkflow({
        previewUrl,
        prTitle: prDetails.title,
        prBody: prDetails.body,
        changedFiles,
        repoOwner: repoInfo.owner,
        repoName: repoInfo.name,
        prNumber: prNum,
        debug
    });
    // Step 7: Display results
    console.log(`\n${chalk.bold("📊 PR Check Results")}`);
    console.log(chalk.gray("─".repeat(60)));
    if (result.success) {
        console.log(chalk.green("✓ All checks passed"));
    }
    else {
        console.log(chalk.red("✗ Some checks failed"));
    }
    console.log(`\n${chalk.bold("Full Report:")}`);
    console.log(result.reportUrl);
    if (result.prComment) {
        console.log(`\n${chalk.gray("A comment will be posted to the PR with these results")}`);
    }
}
async function getRepoInfo(repoArg, debug) {
    try {
        if (repoArg) {
            // Parse from provided repo URL or owner/name
            const match = repoArg.match(/(?:github\.com\/)?([^/]+)\/([^/]+)/);
            if (match) {
                return {
                    owner: match[1],
                    name: match[2].replace(/\.git$/, ""),
                    branch: execSync("git branch --show-current", { encoding: "utf-8" }).trim()
                };
            }
        }
        // Auto-detect from current directory using our script
        const scriptPath = new URL("../../scripts/get-repo-info.sh", import.meta.url).pathname;
        const result = execSync(`bash "${scriptPath}"`, { encoding: "utf-8" });
        const info = JSON.parse(result);
        if (debug) {
            console.log(chalk.gray(`[DEBUG] Repo info: ${JSON.stringify(info, null, 2)}`));
        }
        return {
            owner: info.repoOwner,
            name: info.repoName,
            branch: info.currentBranch
        };
    }
    catch (error) {
        if (debug) {
            console.error(chalk.gray(`[DEBUG] Error getting repo info: ${error}`));
        }
        return null;
    }
}
async function detectPRNumber(repoInfo, debug) {
    try {
        // Use gh CLI to find PR for current branch
        const result = execSync(`gh pr list --head "${repoInfo.branch}" --json number --jq '.[0].number'`, {
            encoding: "utf-8"
        }).trim();
        if (debug) {
            console.log(chalk.gray(`[DEBUG] Detected PR number: ${result}`));
        }
        return result || null;
    }
    catch (error) {
        if (debug) {
            console.error(chalk.gray(`[DEBUG] Error detecting PR number: ${error}`));
        }
        return null;
    }
}
async function fetchPRDetails(owner, repo, prNumber, debug) {
    try {
        const result = execSync(`gh api repos/${owner}/${repo}/pulls/${prNumber} --jq '{title,body,branch:.head.ref,author:.user.login}'`, { encoding: "utf-8" });
        const details = JSON.parse(result);
        if (debug) {
            console.log(chalk.gray(`[DEBUG] PR details: ${JSON.stringify(details, null, 2)}`));
        }
        return details;
    }
    catch (error) {
        if (debug) {
            console.error(chalk.gray(`[DEBUG] Error fetching PR details: ${error}`));
        }
        return null;
    }
}
async function findVercelPreview(_repoInfo, branch, debug) {
    try {
        // Use vc CLI to find deployments for this branch
        const result = execSync(`vc ls --yes | grep "${branch}" | head -1 | awk '{print $2}'`, { encoding: "utf-8" }).trim();
        if (debug) {
            console.log(chalk.gray(`[DEBUG] Found preview URL: ${result}`));
        }
        return result || null;
    }
    catch (error) {
        if (debug) {
            console.error(chalk.gray(`[DEBUG] Error finding Vercel preview: ${error}`));
        }
        return null;
    }
}
async function getChangedFiles(owner, repo, prNumber, debug) {
    try {
        const result = execSync(`gh api repos/${owner}/${repo}/pulls/${prNumber}/files --jq '.[].filename'`, {
            encoding: "utf-8"
        });
        const files = result.trim().split("\n").filter(Boolean);
        if (debug) {
            console.log(chalk.gray(`[DEBUG] Changed files: ${files.join(", ")}`));
        }
        return files;
    }
    catch (error) {
        if (debug) {
            console.error(chalk.gray(`[DEBUG] Error getting changed files: ${error}`));
        }
        return [];
    }
}
// biome-ignore lint/suspicious/noExplicitAny: Workflow response has dynamic structure
async function runPRCheckWorkflow(params) {
    const { previewUrl, prTitle, prBody, changedFiles, repoOwner, repoName, prNumber, debug } = params;
    // Call the workflow API endpoint (we'll create this next)
    const workflowUrl = "https://dev3000-mcp.vercel.sh/api/cloud/check-pr";
    try {
        const response = await fetch(workflowUrl, {
            method: "POST",
            headers: {
                "Content-Type": "application/json"
            },
            body: JSON.stringify({
                previewUrl,
                prTitle,
                prBody,
                changedFiles,
                repoOwner,
                repoName,
                prNumber
            })
        });
        if (!response.ok) {
            throw new Error(`HTTP ${response.status}: ${response.statusText}`);
        }
        const result = await response.json();
        if (debug) {
            console.log(chalk.gray(`[DEBUG] Workflow result: ${JSON.stringify(result, null, 2)}`));
        }
        return result;
    }
    catch (error) {
        if (debug) {
            console.error(chalk.gray(`[DEBUG] Error running workflow: ${error}`));
        }
        // For now, return a mock result since we haven't built the workflow yet
        return {
            success: true,
            reportUrl: `${previewUrl}#pr-check-report`,
            prComment: true,
            message: "PR check workflow not yet implemented - coming soon!"
        };
    }
}
//# sourceMappingURL=cloud-check-pr.js.map