import { APIResponseSchema } from "../api.js";
import { CreateJobOptionsSchema, JobSchema } from "./types.js";
import { throwSandboxError } from "./util.js";
import { z } from 'zod';
export const CreateJobRequestSchema = CreateJobOptionsSchema;
export const CreateJobDataSchema = JobSchema;
export const CreateJobResponseSchema = APIResponseSchema(CreateJobDataSchema);
export const JobCreateParamsSchema = z.object({
    sandboxId: z.string().describe('Sandbox ID where the job should run'),
    options: CreateJobOptionsSchema.describe('Job creation options'),
    orgId: z.string().optional().describe('Optional org id for CLI auth context'),
    signal: z.custom().optional().describe('Optional abort signal for cancellation'),
});
export async function jobCreate(client, params) {
    const { sandboxId, options, orgId, signal } = params;
    const body = {
        command: options.command,
    };
    if (options.streams) {
        body.streams = options.streams;
    }
    const queryParams = new URLSearchParams();
    if (orgId) {
        queryParams.set('orgId', orgId);
    }
    const queryString = queryParams.toString();
    const url = `/sandbox/sandboxes/${sandboxId}/jobs${queryString ? `?${queryString}` : ''}`;
    const resp = await client.post(url, body, CreateJobResponseSchema, CreateJobRequestSchema, signal);
    if (resp.success) {
        return resp.data;
    }
    throwSandboxError(resp, { sandboxId });
}
export const JobGetDataSchema = JobSchema;
export const JobGetResponseSchema = APIResponseSchema(JobGetDataSchema);
export const JobGetParamsSchema = z.object({
    sandboxId: z.string().describe('Sandbox ID'),
    jobId: z.string().describe('Job ID'),
    orgId: z.string().optional().describe('Organization ID'),
    signal: z.custom().optional().describe('Abort signal for cancellation'),
});
export async function jobGet(client, params) {
    const { sandboxId, jobId, orgId, signal } = params;
    const queryParams = new URLSearchParams();
    if (orgId) {
        queryParams.set('orgId', orgId);
    }
    const queryString = queryParams.toString();
    const url = `/sandbox/sandboxes/${sandboxId}/jobs/${jobId}${queryString ? `?${queryString}` : ''}`;
    const resp = await client.get(url, JobGetResponseSchema, signal);
    if (resp.success) {
        return resp.data;
    }
    throwSandboxError(resp, { sandboxId, jobId: params.jobId });
}
export const JobListDataSchema = z.object({
    jobs: z.array(JobSchema).describe('List of jobs'),
});
export const JobListResponseSchema = APIResponseSchema(JobListDataSchema);
export const JobListParamsSchema = z.object({
    sandboxId: z.string().describe('Sandbox ID'),
    orgId: z.string().optional().describe('Organization ID'),
    limit: z.number().optional().describe('Maximum number of results'),
    signal: z.custom().optional().describe('Abort signal for cancellation'),
});
export async function jobList(client, params) {
    const { sandboxId, orgId, limit, signal } = params;
    const queryParams = new URLSearchParams();
    if (orgId) {
        queryParams.set('orgId', orgId);
    }
    if (limit !== undefined) {
        queryParams.set('limit', String(limit));
    }
    const queryString = queryParams.toString();
    const url = `/sandbox/sandboxes/${sandboxId}/jobs${queryString ? `?${queryString}` : ''}`;
    const resp = await client.get(url, JobListResponseSchema, signal);
    if (resp.success) {
        return resp.data;
    }
    throwSandboxError(resp, { sandboxId });
}
export const JobStopDataSchema = JobSchema;
export const JobStopResponseSchema = APIResponseSchema(JobStopDataSchema);
export const JobStopParamsSchema = z.object({
    sandboxId: z.string().describe('Sandbox ID'),
    jobId: z.string().describe('Job ID'),
    force: z.boolean().optional().describe('Force termination (SIGKILL)'),
    orgId: z.string().optional().describe('Organization ID'),
    signal: z.custom().optional().describe('Abort signal for cancellation'),
});
export async function jobStop(client, params) {
    const { sandboxId, jobId, force, orgId, signal } = params;
    const queryParams = new URLSearchParams();
    if (orgId) {
        queryParams.set('orgId', orgId);
    }
    if (force) {
        queryParams.set('force', 'true');
    }
    const queryString = queryParams.toString();
    const url = `/sandbox/sandboxes/${sandboxId}/jobs/${jobId}${queryString ? `?${queryString}` : ''}`;
    const resp = await client.delete(url, JobStopResponseSchema, signal);
    if (resp.success) {
        return resp.data;
    }
    throwSandboxError(resp, { sandboxId, jobId: params.jobId });
}
//# sourceMappingURL=job.js.map