/**
 * GET /api/cloud-providers/github/orgs
 *
 * Returns the list of "owners" the authenticated GitHub user can publish a
 * repository under: their personal account plus every organization they
 * belong to. The first entry is always the personal account so the UI can
 * default to it. Returns 401 when gh is not signed in so the caller can
 * route the user to the sign-in flow.
 */
import { NextResponse } from 'next/server';
import { resolve } from '../../../../../lib/server-container.js';
export const dynamic = 'force-dynamic';
export async function GET() {
    try {
        const github = resolve('IGitHubRepositoryService');
        let user;
        try {
            user = await github.getAuthenticatedUser();
        }
        catch {
            return NextResponse.json({ error: 'GitHub CLI is not authenticated', code: 'GH_NOT_AUTHENTICATED' }, { status: 401 });
        }
        const listOrgs = resolve('ListGitHubOrganizationsUseCase');
        const orgs = await listOrgs.execute();
        const owners = [
            { login: user, kind: 'user', description: 'Your personal account' },
            ...orgs.map((o) => ({ login: o.login, kind: 'org', description: o.description })),
        ];
        return NextResponse.json({ owners });
    }
    catch (error) {
        return NextResponse.json({ error: error instanceof Error ? error.message : 'Internal server error' }, { status: 500 });
    }
}
