'use strict';

function emitDropTable(ir, dialect) {
  if (!ir || typeof ir !== 'object' || typeof ir.tableName !== 'string') {
    throw new Error('emitDropTable: ir must be an object with tableName');
  }

  const identifier = dialect.formatTableIdentifier(ir);

  switch (dialect.name) {
    case 'postgres':
      return `DROP TABLE IF EXISTS ${identifier} CASCADE`;
    case 'mysql':
      return `DROP TABLE IF EXISTS ${identifier}`;
    case 'sqlite':
      return `DROP TABLE IF EXISTS ${identifier}`;
    case 'oracle':
      return [
        'BEGIN',
        // ORA-00942 = table does not exist; suppress so DROP IF EXISTS semantics works
        `    EXECUTE IMMEDIATE 'DROP TABLE ${identifier} CASCADE CONSTRAINTS';`,
        'EXCEPTION',
        '    WHEN OTHERS THEN',
        '        IF SQLCODE != -942 THEN RAISE; END IF;',
        'END;',
        '/'
      ].join('\n');
    default:
      throw new Error(`Unsupported dialect: ${dialect.name}`);
  }
}

module.exports = { emitDropTable };
