// engine/deploy/providers/fly.ts — Fly.io Machines API deploy provider
const FLY_API_BASE = 'https://api.machines.dev';
/** Trigger a Fly.io machine creation to deploy an app. */
export async function deployToFly(config, options) {
    const appName = config.projectId ?? 'planu-app';
    const url = `${FLY_API_BASE}/v1/apps/${appName}/machines`;
    const body = {
        config: {
            image: options.image ?? 'flyio/hellofly:latest',
            ...(options.region !== undefined && options.region.length > 0
                ? { region: options.region }
                : {}),
        },
    };
    try {
        const response = await fetch(url, {
            method: 'POST',
            headers: {
                Authorization: `Bearer ${config.token}`,
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(body),
        });
        if (!response.ok) {
            const text = await response.text();
            return {
                id: `fly-error-${String(Date.now())}`,
                provider: 'fly',
                status: 'error',
                createdAt: new Date().toISOString(),
                errorMessage: `Fly API ${String(response.status)}: ${text.slice(0, 200)}`,
            };
        }
        const data = (await response.json());
        return {
            id: data.id,
            provider: 'fly',
            status: mapFlyState(data.state ?? 'pending'),
            createdAt: new Date().toISOString(),
        };
    }
    catch (err) {
        return {
            id: `fly-error-${String(Date.now())}`,
            provider: 'fly',
            status: 'error',
            createdAt: new Date().toISOString(),
            errorMessage: err instanceof Error ? err.message : String(err),
        };
    }
}
/** Check the status of a Fly.io machine. */
export async function getFlyMachineStatus(config, machineId) {
    const appName = config.projectId ?? 'planu-app';
    const url = `${FLY_API_BASE}/v1/apps/${appName}/machines/${machineId}`;
    try {
        const response = await fetch(url, {
            headers: {
                Authorization: `Bearer ${config.token}`,
            },
        });
        if (!response.ok) {
            return {
                id: machineId,
                provider: 'fly',
                status: 'error',
                createdAt: new Date().toISOString(),
                errorMessage: `Fly API ${String(response.status)}`,
            };
        }
        const data = (await response.json());
        return {
            id: data.id,
            provider: 'fly',
            status: mapFlyState(data.state ?? 'pending'),
            createdAt: new Date().toISOString(),
        };
    }
    catch (err) {
        return {
            id: machineId,
            provider: 'fly',
            status: 'error',
            createdAt: new Date().toISOString(),
            errorMessage: err instanceof Error ? err.message : String(err),
        };
    }
}
function mapFlyState(state) {
    switch (state) {
        case 'started':
            return 'ready';
        case 'replacing':
            return 'building';
        case 'stopped':
        case 'destroying':
        case 'destroyed':
            return 'cancelled';
        default:
            return 'pending';
    }
}
//# sourceMappingURL=fly.js.map