anchor.diff:           code_change/4]).
   
   % states
  --export([disable_org_access/2, 
  +-export([disable_org_access/2,
            migrate_org/2,
            verify_org/2,
            set_org_to_sql/2,
  
positive.code: % api
  -export([start_link/1]).
  
  -define(PHASE_2_MIGRATION_COMPONENTS, 
              [checksums, clients, cookbooks, environments, roles, data]).
  
  -define(ORG_META(OrgName), [{org_name, OrgName}]).
  
  -record(state, { account_info, org_name, start_time = 0, error }).
  
  start_link(Config) ->
      gen_fsm:start_link(?MODULE, Config, []).
  
  init(Config) -> 
      % TODO config is currently just org name, we may want to
      % factor up some of the account_info stuff out of moser.
      {ok, disable_org_access, #state{org_name = Config, start_time = os:timestamp()}, 0}.
  
  disable_org_access(timeout, #state{org_name = OrgName} = State) -> 
      case mover_org_darklaunch:disable_org(OrgName) of 
          ok -> 
              {next_state, migrate_org, State, 0};
          {error, Error} -> 
              %lager:error(?ORG_META(OrgName), "Failed to place org into maintenance mode, skipping it: ~p", [Error]),
              {stop, {error, Error}, State}
      end.
  
  migrate_org(timeout, #state{org_name = OrgName} = State) ->
      case moser_converter:convert_org(OrgName) of 
          [{ok, _}] -> 
              {next_state, verify_org, State, 0};
          {error, Error} ->
              %TODO lager:error(?ORG_META(OrgName), "Failed to migrate org: ~p", [Error]),
  
negative.code: verify_org(timeout, #state{org_name = _OrgName} = State) ->
      % TODO do we have any validation we can run?
      % case moser_validator:validate_org(OrgName) of
      case ok of
          ok ->
              {next_state, set_org_to_sql, State, 0};
          {error, Error} ->
              %lager:error(?ORG_META(OrgName), "Failed to verify org: ~p", [Error]),
              {next_state, abort_migration, #state{error = Error} = State, 0}
      end.
  