_ = require 'underscore'

class TableInfo
  constructor: (@tableName, opts) ->
    @attributes = {}
    @references = {}
    @indexes = {}

  addColumn: (name, type, opts) ->
    @attributes[name] = if opts then _.extend({type}, opts) else {type}
    @primaryKeyName = name if opts?.primaryKey

  addReference: (name, tableName, opts={}) ->
    unless (opts = @attributes[name])?
      opts = @attributes[name] = {type: 'INTEGER'}
    opts.references = _.extend {name: tableName}, opts
    @references[name] = opts

  createIndex: (indexName, columns) ->
    columns = [columns] unless Array.isArray(columns)
    @indexes[indexName] = columns

  _checkPrimaryKey: ->
    @addColumn('id', 'INTEGER', primaryKey: true) unless @primaryKeyName

  _finishConfig: ->
    for name, {references} of @references
      references.fields ?= Migration.tables[references.name].primaryKeyName

module.exports =
class Migration
  @tables = {}

  @createTable: (tableName, opts={}, callback) ->
    if _.isFunction(opts)
      callback = opts
      opts = {}
    tableInfo = new TableInfo(tableName, opts)
    callback?(tableInfo)
    @tables[tableName] = tableInfo

  @_finishConfig: ->
    info._checkPrimaryKey() for name, info of @tables
    info._finishConfig() for name, info of @tables

  @clear: -> @tables = {}
