import { existsSync, mkdirSync, readdirSync, writeFileSync } from 'node:fs';
import { basename, join, resolve } from 'node:path';
import { findProjectRoot } from '../local-runtime.js';
export async function runNew(subject, rest, flags) {
    const { kind, rawName } = resolveNewTarget(subject, rest);
    const projectRoot = findProjectRoot(process.cwd());
    const title = toTitle(rawName);
    const slug = toSlug(rawName);
    const usingStarterData = existsSync(join(projectRoot, 'data', 'revenue.csv'));
    // Detect available data files for smarter notebook templates
    const availableDataFiles = detectAvailableDataFiles(projectRoot);
    // Notebooks use .dqlnb extension and their own creation path
    if (kind === 'notebook') {
        return runNewNotebook({ projectRoot, title, slug, flags, usingStarterData, availableDataFiles });
    }
    const outputDir = resolve(projectRoot, defaultDirForKind(kind, flags.outDir));
    const filePath = join(outputDir, `${slug}.dql`);
    if (existsSync(filePath)) {
        throw new Error(`${capitalize(kind)} already exists: ${filePath}`);
    }
    mkdirSync(outputDir, { recursive: true });
    const owner = flags.owner || process.env.USER || 'team';
    const domain = flags.domain || 'general';
    const chart = normalizeChart(flags.chart || 'bar');
    const metricName = `${slug}_metric`;
    const content = buildTemplate({
        kind,
        title,
        domain,
        owner,
        chart,
        queryOnly: flags.queryOnly,
        usingStarterData,
        metricName,
    });
    writeFileSync(filePath, content, 'utf-8');
    const relatedFiles = kind === 'semantic-block'
        ? writeSemanticCompanionFiles(projectRoot, slug, domain, owner, metricName)
        : [];
    const relativePath = relativeToProject(projectRoot, filePath);
    const nextSteps = nextStepsFor(kind, relativePath, usingStarterData, flags.queryOnly);
    if (flags.format === 'json') {
        console.log(JSON.stringify({
            created: true,
            type: kind,
            name: title,
            path: filePath,
            previewReady: usingStarterData && kind !== 'semantic-block',
            relatedFiles,
            nextSteps,
        }, null, 2));
        return;
    }
    console.log(`\n  ✓ Created DQL ${kind}: ${title}`);
    console.log(`    Path: ${filePath}`);
    if (relatedFiles.length > 0) {
        console.log('    Related files:');
        relatedFiles.forEach((relatedFile) => console.log(`      - ${relatedFile}`));
    }
    console.log('');
    console.log('  Next steps:');
    nextSteps.forEach((step, index) => console.log(`    ${index + 1}. ${step}`));
    console.log('');
}
function resolveNewTarget(subject, rest) {
    if (!subject) {
        throw new Error('Usage: dql new <block|dashboard|workbook|semantic-block|notebook|business-view|term> <name>');
    }
    if (subject === 'business_view') {
        subject = 'business-view';
    }
    if (subject === 'block' || subject === 'dashboard' || subject === 'workbook' || subject === 'semantic-block' || subject === 'notebook' || subject === 'business-view' || subject === 'term') {
        const rawName = rest.join(' ').trim();
        if (!rawName) {
            throw new Error(`Missing ${subject} name. Usage: dql new ${subject} <name>`);
        }
        return { kind: subject, rawName };
    }
    return { kind: 'block', rawName: subject };
}
function buildTemplate(opts) {
    switch (opts.kind) {
        case 'dashboard':
            return buildDashboardTemplate(opts);
        case 'workbook':
            return buildWorkbookTemplate(opts);
        case 'semantic-block':
            return buildSemanticBlockTemplate(opts);
        case 'business-view':
            return buildBusinessViewTemplate(opts);
        case 'term':
            return buildTermTemplate(opts);
        case 'block':
        default:
            return buildBlockTemplate(opts);
    }
}
function buildTermTemplate(opts) {
    return `term "${opts.title}" {
    domain = "${opts.domain}"
    type = "entity"
    status = "draft"
    description = "Business definition for ${opts.title.toLowerCase()}."
    owner = "${opts.owner}"
    tags = ["term", "${opts.domain}"]
    identifiers = ["${toSlug(opts.title)}_id"]
    synonyms = []
    businessOwner = "${opts.owner}"
    businessRules = []
    caveats = []
}
`;
}
function buildBusinessViewTemplate(opts) {
    return `business_view "${opts.title}" {
    domain = "${opts.domain}"
    status = "draft"
    description = "Business composition for ${opts.title.toLowerCase()}"
    owner = "${opts.owner}"
    tags = ["business-view", "${opts.domain}"]
    businessOutcome = "Describe the business decision this view supports."
    decisionUse = "Describe when teams should use this view."
    reviewCadence = "weekly"

    includes {
        block "Customer Identity"
        block "Customer Orders Rollup"
        business_view "Customer Service Summary"
    }
}
`;
}
function buildBlockTemplate(opts) {
    const description = opts.usingStarterData
        ? `Starter block for ${opts.title.toLowerCase()} using local sample data`
        : `Starter block for ${opts.title.toLowerCase()}`;
    const query = opts.usingStarterData ? starterBlockQueryForChart(opts.chart) : placeholderBlockQueryForChart(opts.chart);
    const params = opts.usingStarterData && (opts.chart === 'bar' || opts.chart === 'kpi')
        ? `
    params {
        period = "current_quarter"
    }
`
        : '';
    const visualization = opts.queryOnly ? '' : blockVisualizationForChart(opts.chart);
    return `block "${opts.title}" {
    domain = "${opts.domain}"
    type = "custom"
    description = "${description}"
    owner = "${opts.owner}"
    tags = ["starter", "${opts.domain}"]${params}

    query = """
${indentBlock(query, 8)}
    """${visualization}

    tests {
        assert row_count > 0
    }
}
`;
}
function buildSemanticBlockTemplate(opts) {
    return `block "${opts.title}" {
    domain = "${opts.domain}"
    type = "semantic"
    description = "Starter semantic block for ${opts.title.toLowerCase()}"
    owner = "${opts.owner}"
    tags = ["starter", "semantic", "${opts.domain}"]
    metric = "${opts.metricName}"
}
`;
}
function buildDashboardTemplate(opts) {
    const starterSource = `read_csv_auto('./data/revenue.csv')`;
    const source = opts.usingStarterData ? starterSource : 'your_table';
    const primaryChart = dashboardChartFor(opts.chart, source, opts.usingStarterData);
    const tableChart = `  chart.table(
    SELECT recognized_at, segment_tier, amount, fiscal_period
    FROM ${source}
    ORDER BY recognized_at DESC,
    title = "Recent Rows"
  )`;
    return `dashboard "${opts.title}" {
${primaryChart}

${tableChart}
}
`;
}
function buildWorkbookTemplate(opts) {
    const source = opts.usingStarterData ? `read_csv_auto('./data/revenue.csv')` : 'your_table';
    return `workbook "${opts.title}" {
  page "Summary" {
    chart.kpi(
      SELECT SUM(amount) AS total_revenue FROM ${source},
      metrics = ["total_revenue"],
      title = "Total Revenue"
    )
  }

  page "Trend" {
    chart.line(
      SELECT recognized_at AS revenue_date, SUM(amount) AS total_revenue
      FROM ${source}
      GROUP BY recognized_at
      ORDER BY recognized_at,
      x = revenue_date,
      y = total_revenue,
      title = "Revenue Trend"
    )
  }
}
`;
}
function starterBlockQueryForChart(chart) {
    switch (chart) {
        case 'kpi':
            return `SELECT SUM(amount) AS total_revenue
FROM read_csv_auto('./data/revenue.csv')
WHERE fiscal_period = \${period}`;
        case 'line':
            return `SELECT recognized_at AS revenue_date, SUM(amount) AS total_revenue
FROM read_csv_auto('./data/revenue.csv')
GROUP BY recognized_at
ORDER BY recognized_at`;
        case 'table':
            return `SELECT recognized_at, segment_tier, amount, fiscal_period
FROM read_csv_auto('./data/revenue.csv')
ORDER BY recognized_at DESC`;
        case 'bar':
        default:
            return `SELECT segment_tier AS segment, SUM(amount) AS revenue
FROM read_csv_auto('./data/revenue.csv')
WHERE fiscal_period = \${period}
GROUP BY segment_tier
ORDER BY revenue DESC`;
    }
}
function placeholderBlockQueryForChart(chart) {
    switch (chart) {
        case 'kpi':
            return `SELECT SUM(measure) AS total_revenue
FROM your_table`;
        case 'line':
            return `SELECT dimension_date AS revenue_date, SUM(measure) AS total_revenue
FROM your_table
GROUP BY dimension_date
ORDER BY dimension_date`;
        case 'table':
            return `SELECT *
FROM your_table
LIMIT 50`;
        case 'bar':
        default:
            return `SELECT dimension AS segment, SUM(measure) AS revenue
FROM your_table
GROUP BY dimension
ORDER BY revenue DESC`;
    }
}
function blockVisualizationForChart(chart) {
    switch (chart) {
        case 'kpi':
            return `

    visualization {
        chart = "kpi"
        y = total_revenue
    }`;
        case 'line':
            return `

    visualization {
        chart = "line"
        x = revenue_date
        y = total_revenue
    }`;
        case 'table':
            return `

    visualization {
        chart = "table"
    }`;
        case 'bar':
        default:
            return `

    visualization {
        chart = "bar"
        x = segment
        y = revenue
    }`;
    }
}
function dashboardChartFor(chart, source, usingStarterData) {
    switch (chart) {
        case 'kpi':
            return `  chart.kpi(
    SELECT SUM(amount) AS total_revenue FROM ${source},
    metrics = ["total_revenue"],
    title = "Total Revenue"
  )`;
        case 'line':
            return `  chart.line(
    SELECT recognized_at AS revenue_date, SUM(amount) AS total_revenue
    FROM ${source}
    GROUP BY recognized_at
    ORDER BY recognized_at,
    x = revenue_date,
    y = total_revenue,
    title = "Revenue Trend"
  )`;
        case 'table':
            return `  chart.table(
    SELECT recognized_at, segment_tier, amount, fiscal_period
    FROM ${source}
    ORDER BY recognized_at DESC,
    title = "Revenue Table"
  )`;
        case 'bar':
        default:
            return `  chart.bar(
    SELECT segment_tier AS segment, SUM(amount) AS revenue
    FROM ${source}${usingStarterData ? '\n    WHERE fiscal_period = "current_quarter"' : ''}
    GROUP BY segment_tier
    ORDER BY revenue DESC,
    x = segment,
    y = revenue,
    title = "Revenue by Segment"
  )`;
    }
}
function nextStepsFor(kind, relativePath, usingStarterData, queryOnly) {
    if (kind === 'semantic-block') {
        return [
            `dql parse ${relativePath}`,
            `dql info ${relativePath}`,
        ];
    }
    if (kind === 'business-view') {
        return [
            `Edit ${relativePath} to point includes at existing blocks or business views`,
            `dql compile`,
        ];
    }
    if (kind === 'term') {
        return [
            `Edit ${relativePath} to capture the business definition, identifiers, and rules`,
            `Reference it from blocks or business views with terms = ["${toTitle(basename(relativePath, '.dql'))}"]`,
            `dql compile`,
        ];
    }
    if (!usingStarterData) {
        return [
            `Edit ${relativePath} to replace the placeholder SQL`,
            `dql parse ${relativePath}`,
        ];
    }
    const steps = [`dql parse ${relativePath}`];
    if (kind === 'block') {
        steps.push(queryOnly ? `dql info ${relativePath}` : `dql preview ${relativePath}`);
    }
    else {
        steps.push(`dql preview ${relativePath}`);
    }
    return steps;
}
function defaultDirForKind(kind, outDir) {
    if (outDir)
        return outDir;
    switch (kind) {
        case 'dashboard':
            return 'dashboards';
        case 'semantic-block':
            return 'blocks';
        case 'workbook':
            return 'workbooks';
        case 'notebook':
            return 'notebooks';
        case 'business-view':
            return 'business-views';
        case 'term':
            return 'terms';
        case 'block':
        default:
            return 'blocks';
    }
}
async function runNewNotebook(opts) {
    const { projectRoot, title, slug, flags, usingStarterData, availableDataFiles } = opts;
    const outputDir = resolve(projectRoot, flags.outDir || 'notebooks');
    const filePath = join(outputDir, `${slug}.dqlnb`);
    if (existsSync(filePath)) {
        throw new Error(`Notebook already exists: ${filePath}`);
    }
    mkdirSync(outputDir, { recursive: true });
    const template = flags.chart || 'blank'; // reuse --chart flag as template selector
    const cells = buildNotebookCells(title, template, usingStarterData, availableDataFiles);
    const content = JSON.stringify({ version: 1, title, cells }, null, 2);
    writeFileSync(filePath, content, 'utf-8');
    const relativePath = relativeToProject(projectRoot, filePath);
    if (flags.format === 'json') {
        console.log(JSON.stringify({ created: true, type: 'notebook', name: title, path: filePath }, null, 2));
        return;
    }
    console.log(`\n  ✓ Created DQL notebook: ${title}`);
    console.log(`    Path: ${filePath}`);
    console.log('');
    console.log('  Next steps:');
    console.log(`    1. dql notebook  (opens the interactive notebook UI)`);
    console.log(`    2. Open "${relativePath}" from the Files panel`);
    console.log('');
}
function buildNotebookCells(title, template, usingStarterData, availableDataFiles = []) {
    const id = () => Math.random().toString(36).slice(2, 10);
    // Pick the best available data source — prefer template-specific CSVs, fall back to any CSV
    function dataSrc(preferred) {
        for (const name of preferred) {
            if (availableDataFiles.includes(name))
                return `read_csv_auto('./data/${name}')`;
        }
        if (availableDataFiles.length > 0)
            return `read_csv_auto('./data/${availableDataFiles[0]}')`;
        return 'your_table';
    }
    if (template === 'revenue') {
        const src = dataSrc(['revenue.csv', 'orders.csv']);
        return [
            { id: id(), type: 'markdown', content: `# ${title}\n\nRevenue analysis using DQL and DuckDB.` },
            { id: id(), type: 'sql', name: 'revenue_summary', content: `SELECT *\nFROM ${src}\nLIMIT 100` },
        ];
    }
    if (template === 'pipeline') {
        const src = dataSrc(['pipeline.csv', 'orders.csv', 'funnel.csv']);
        return [
            { id: id(), type: 'markdown', content: `# ${title}\n\nPipeline health and conversion analysis.` },
            { id: id(), type: 'sql', name: 'pipeline_overview', content: `SELECT *\nFROM ${src}\nLIMIT 100` },
        ];
    }
    if (usingStarterData && availableDataFiles.length > 0) {
        const src = dataSrc(availableDataFiles);
        return [
            { id: id(), type: 'markdown', content: `# ${title}\n\nAdd your analysis here.` },
            { id: id(), type: 'sql', name: 'query_1', content: `SELECT *\nFROM ${src}\nLIMIT 10` },
        ];
    }
    return [
        { id: id(), type: 'markdown', content: `# ${title}\n\nAdd your analysis here.` },
        { id: id(), type: 'sql', name: 'query_1', content: `SELECT 1 AS hello` },
    ];
}
/** List CSV/Parquet/JSON files in the project's data/ directory */
function detectAvailableDataFiles(projectRoot) {
    try {
        const dataDir = join(projectRoot, 'data');
        if (!existsSync(dataDir))
            return [];
        return readdirSync(dataDir)
            .filter((f) => /\.(csv|parquet|json|jsonl|ndjson|xlsx)$/i.test(f))
            .sort();
    }
    catch {
        return [];
    }
}
function normalizeChart(value) {
    const normalized = value.trim().toLowerCase();
    return normalized || 'bar';
}
function indentBlock(value, spaces) {
    const prefix = ' '.repeat(spaces);
    return value.split('\n').map((line) => `${prefix}${line}`).join('\n');
}
function toSlug(value) {
    return value
        .replace(/\.dql$/i, '')
        .trim()
        .replace(/[^a-zA-Z0-9]+/g, '_')
        .replace(/^_+|_+$/g, '')
        .toLowerCase() || 'new_asset';
}
function toTitle(value) {
    const cleaned = value.replace(/\.dql$/i, '').trim();
    if (!cleaned)
        return 'New Asset';
    if (/[A-Z]/.test(cleaned) || cleaned.includes(' ')) {
        return cleaned.replace(/[_-]+/g, ' ');
    }
    return cleaned
        .split(/[_-]+/g)
        .map((part) => part ? `${part[0].toUpperCase()}${part.slice(1)}` : part)
        .join(' ');
}
function relativeToProject(projectRoot, filePath) {
    const normalizedRoot = `${resolve(projectRoot)}/`;
    const normalizedPath = resolve(filePath);
    return normalizedPath.startsWith(normalizedRoot)
        ? normalizedPath.slice(normalizedRoot.length)
        : basename(filePath);
}
function capitalize(value) {
    return `${value[0]?.toUpperCase() || ''}${value.slice(1)}`;
}
function writeSemanticCompanionFiles(projectRoot, slug, domain, owner, metricName) {
    const metricDir = join(projectRoot, 'semantic-layer', 'metrics');
    const blockDir = join(projectRoot, 'semantic-layer', 'blocks');
    mkdirSync(metricDir, { recursive: true });
    mkdirSync(blockDir, { recursive: true });
    const metricPath = join(metricDir, `${metricName}.yaml`);
    const blockPath = join(blockDir, `${slug}.yaml`);
    if (!existsSync(metricPath)) {
        writeFileSync(metricPath, `name: ${metricName}
label: ${toLabel(metricName)}
description: Starter semantic metric for ${toLabel(slug)}
domain: ${domain}
sql: SUM(measure)
type: sum
table: your_table
tags:
  - ${domain}
  - semantic
owner: ${owner}
`, 'utf-8');
    }
    if (!existsSync(blockPath)) {
        writeFileSync(blockPath, `name: ${slug}
block: ${slug}
domain: ${domain}
description: Companion business metadata for ${toLabel(slug)}
owner: ${owner}
tags:
  - ${domain}
  - semantic
semanticMappings:
  measure: ${metricName}
reviewStatus: review
`, 'utf-8');
    }
    return [relativeToProject(projectRoot, metricPath), relativeToProject(projectRoot, blockPath)];
}
function toLabel(value) {
    return value
        .split(/[_-]+/g)
        .filter(Boolean)
        .map((part) => `${part[0]?.toUpperCase() || ''}${part.slice(1)}`)
        .join(' ');
}
//# sourceMappingURL=new.js.map