module WolfCore::FkmOperations

def create_foreign_key(tenant:, source:, source_id:, destination:, destination_id:)

def create_foreign_key(tenant:, source:, source_id:, destination:, destination_id:)
  raise_service_error('tenant is required to create a foreign key') if tenant.blank?
  raise_service_error('source is required to create a foreign key') if source.blank?
  raise_service_error('source_id is required to create a foreign key') if source_id.blank?
  raise_service_error('destination is required to create a foreign key') if destination.blank?
  raise_service_error('destination_id is required to create a foreign key') if destination_id.blank?
  response = http_post(
    url: "#{ENV['FKM_URL']}/Prod/create",
    body: {
      tenant: tenant,
      source: source,
      source_id: source_id,
      destination: destination,
      destination_id: destination_id
    }
  )
  response = parse_http_response(response)
  log_object response, title: 'create_foreign_key response is'
  validate_http_response(response: response, message: 'Error creating foreign key')
  parse_http_response(response).body
end

def find_or_create_foreign_key(tenant:, source:, source_id:, destination:, destination_id:)

def find_or_create_foreign_key(tenant:, source:, source_id:, destination:, destination_id:)
  found_destination_id = get_foreign_key_destination_id(
    tenant: tenant, source: source, source_id: source_id,
    destination: destination
  )
  log_object "find_or_create_foreign_key found_destination_id is #{found_destination_id}"
  return Result.success(data: { operation: :find }) if found_destination_id.present?
  create_foreign_key(
    tenant: tenant, source: source, source_id: source_id,
    destination: destination, destination_id: destination_id
  )
  Result.success(data: { operation: :create })
end

def get_foreign_key_destination_id(tenant:, source:, source_id:, destination:)

def get_foreign_key_destination_id(tenant:, source:, source_id:, destination:)
  foreign_keys = get_foreign_keys(tenant: tenant, source: source, source_id: source_id)
  foreign_key = foreign_keys.find { |fk| fk['destination'] == destination }
  foreign_key&.dig('destination_id')
end

def get_foreign_keys(tenant:, source:, source_id:)

def get_foreign_keys(tenant:, source:, source_id:)
  response = http_get(
    url: "#{ENV['FKM_URL']}/Prod/lookup?tenant=#{tenant}&source=#{source}&source_id=#{source_id}"
  )
  response = parse_http_response(response)
  log_object response, title: 'get_foreign_keys response is'
  validate_http_response(response: response, message: 'Error getting foreign keys')
  response.body
end

def get_foreign_keys_by_destination(tenant:, destination:, destination_id:)

def get_foreign_keys_by_destination(tenant:, destination:, destination_id:)
  response = http_get(
    url: "#{ENV['FKM_URL']}/Prod/lookup?tenant=#{tenant}&destination=#{destination}&destination_id=#{destination_id}"
  )
  response = parse_http_response(response)
  log_object response, title: 'get_foreign_keys_by_destination response is'
  validate_http_response(response: response, message: 'Error getting foreign keys by destination id')
  response.body
end