// -- Migration generation -----------------------------------------------------
export function generateMigrations(tables, indexes, rlsPolicies, engine) {
    const migrations = [];
    let order = 1;
    if (engine === 'mongodb') {
        for (const table of tables) {
            migrations.push({
                order: order++,
                sql: `db.createCollection("${table.name}", { validator: { $jsonSchema: { bsonType: "object", required: [${table.columns
                    .filter((c) => !c.nullable)
                    .map((c) => `"${c.name}"`)
                    .join(', ')}] } } })`,
                reversible: true,
                reverseSql: `db.${table.name}.drop()`,
                dataLoss: false,
            });
        }
        return migrations;
    }
    for (const table of tables) {
        const columnDefs = table.columns.map((col) => {
            let def = `  ${col.name} ${col.type}`;
            if (!col.nullable) {
                def += ' NOT NULL';
            }
            if (col.default) {
                def += ` DEFAULT ${col.default}`;
            }
            return def;
        });
        migrations.push({
            order: order++,
            sql: [
                `CREATE TABLE ${table.name} (`,
                columnDefs.join(',\n'),
                `  PRIMARY KEY (${table.primaryKey})`,
                ');',
            ].join('\n'),
            reversible: true,
            reverseSql: `DROP TABLE IF EXISTS ${table.name};`,
            dataLoss: false,
        });
    }
    for (const index of indexes) {
        const uniqueStr = index.unique ? 'UNIQUE ' : '';
        migrations.push({
            order: order++,
            sql: `CREATE ${uniqueStr}INDEX ${index.name} ON ${index.table} (${index.columns.join(', ')});`,
            reversible: true,
            reverseSql: `DROP INDEX IF EXISTS ${index.name};`,
            dataLoss: false,
        });
    }
    if (engine === 'supabase' || engine === 'postgresql') {
        for (const table of tables) {
            migrations.push({
                order: order++,
                sql: `ALTER TABLE ${table.name} ENABLE ROW LEVEL SECURITY;`,
                reversible: true,
                reverseSql: `ALTER TABLE ${table.name} DISABLE ROW LEVEL SECURITY;`,
                dataLoss: false,
            });
        }
        for (const policy of rlsPolicies) {
            const usingClause = `USING (${policy.using})`;
            const withCheckClause = policy.withCheck ? ` WITH CHECK (${policy.withCheck})` : '';
            migrations.push({
                order: order++,
                sql: `CREATE POLICY ${policy.name} ON ${policy.table} FOR ${policy.operation} ${usingClause}${withCheckClause};`,
                reversible: true,
                reverseSql: `DROP POLICY IF EXISTS ${policy.name} ON ${policy.table};`,
                dataLoss: false,
            });
        }
    }
    return migrations;
}
// -- Helpers ------------------------------------------------------------------
export function buildRelationships(tables) {
    const relationships = [];
    for (const table of tables) {
        for (const fk of table.foreignKeys) {
            const refTable = fk.references.split('(')[0];
            relationships.push(`${table.name} -> ${refTable} (via ${fk.column}, ON DELETE ${fk.onDelete})`);
        }
    }
    return relationships;
}
export function buildDataIntegrityRules(tables, engine) {
    const rules = [];
    rules.push('All primary keys use UUID for distributed safety');
    rules.push('Timestamps (created_at, updated_at) auto-populated');
    for (const table of tables) {
        if (table.foreignKeys.length > 0) {
            rules.push(`${table.name}: foreign key constraints enforce referential integrity`);
        }
        if (table.checkConstraints.length > 0) {
            rules.push(`${table.name}: check constraints enforce valid data ranges`);
        }
    }
    if (engine === 'supabase' || engine === 'postgresql') {
        rules.push('Row-Level Security (RLS) enabled on all tables');
    }
    return rules;
}
//# sourceMappingURL=migrations.js.map