"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.toolRunSqlOutputSchema = exports.toolRunSqlArgsSchema = exports.createToolRunSqlArgsSchema = exports.buildRunSqlDescription = exports.DEFAULT_RUN_SQL_MAX_LIMIT = exports.DEFAULT_RUN_SQL_LIMIT = void 0;
const zod_1 = require("zod");
const toolSchemaBuilder_1 = require("../toolSchemaBuilder");
const toolMcpQueryResultDescription_1 = require("./toolMcpQueryResultDescription");
exports.DEFAULT_RUN_SQL_LIMIT = 500;
exports.DEFAULT_RUN_SQL_MAX_LIMIT = 5000;
const buildRunSqlDescription = (defaultLimit, maxLimit) => `Execute an arbitrary SQL query against the project's data warehouse and return the results. Successful results can be linked from the final answer with [Open in SQL Runner](#sql-runner-link).

Use this tool when the user wants to run a custom SQL query that doesn't fit the explore-based metric query model.
This is useful for ad-hoc analysis, data exploration, or queries that join across tables not modeled in explores.
${(0, toolMcpQueryResultDescription_1.buildMcpVisualizationFollowUpInstruction)('run_sql')}

The query is executed directly against the warehouse, so use the SQL dialect appropriate for the connected warehouse (e.g., PostgreSQL, BigQuery, Snowflake, etc.).

Parameters:
- sql: The SQL query to execute. Must be a valid SELECT statement.
- limit: Maximum number of rows to return (default ${defaultLimit}, max ${maxLimit}).

${(0, toolMcpQueryResultDescription_1.buildMcpQueryRunResponseDescription)({
    contentDescription: 'CSV with a header row plus data rows. Empty results return prose text like "Query returned 0 rows."',
    completedResultShape: `    result: {
      status: "done",
      rows:     Array<Record<string, unknown>>,  // each row keyed by column name
      columns:  string[],                        // column names in order
      rowCount: number,                          // total rows returned
      sqlRunnerUrl: string | null                // shareable URL to inspect/edit the SQL in SQL Runner
    }`,
})}

Notes:
${toolMcpQueryResultDescription_1.MCP_QUERY_COMMON_NOTES}
- Values in rows are JSON-serializable primitives: numbers, strings, booleans, ISO date strings, or null. They are NOT pre-stringified — there's no need for parseFloat / parseInt on numeric columns.
- Empty results still return structuredContent.result with { status: "done", rows: [], columns, rowCount: 0, sqlRunnerUrl } — distinct from a parse failure.
- Lightdash applies the requested row limit to the SQL query. Ensure the SELECT statement is complete; malformed trailing SQL can surface errors near the generated LIMIT.
- On startup/validation/application/warehouse error, the response has isError: true and content[0].text contains the error message; structuredContent is omitted.
`;
exports.buildRunSqlDescription = buildRunSqlDescription;
const createToolRunSqlArgsSchema = ({ maxLimit = exports.DEFAULT_RUN_SQL_MAX_LIMIT, defaultLimit = exports.DEFAULT_RUN_SQL_LIMIT, } = {}) => (0, toolSchemaBuilder_1.createToolSchema)()
    .extend({
    sql: zod_1.z
        .string()
        .describe('The SQL query to execute against the data warehouse.'),
    limit: zod_1.z
        .number()
        .int()
        .positive()
        .max(maxLimit)
        .default(defaultLimit)
        .describe(`Maximum number of rows to return. Defaults to ${defaultLimit}, max ${maxLimit}.`),
})
    .build();
exports.createToolRunSqlArgsSchema = createToolRunSqlArgsSchema;
exports.toolRunSqlArgsSchema = (0, exports.createToolRunSqlArgsSchema)();
exports.toolRunSqlOutputSchema = zod_1.z.object({
    result: zod_1.z.string(),
    metadata: zod_1.z.object({
        status: zod_1.z.enum(['success', 'error', 'rejected', 'timeout']),
    }),
});
//# sourceMappingURL=toolRunSqlArgs.js.map