module JSONAPI::ResourceIdentifier

def determine_model_class(record, association:, definition:, use_instance_class:)

def determine_model_class(record, association:, definition:, use_instance_class:)
  return record.class if association.nil?
  return record.class if polymorphic_association_for_association?(association, definition)
  return record.class if use_instance_class && sti_subclass?(record.class, association.klass)
  association.klass
end

def extract_id(identifier)

def extract_id(identifier)
  identifier[:id].to_s.presence
end

def extract_lid(identifier)

def extract_lid(identifier)
  identifier[:lid].to_s.presence
end

def extract_type(identifier)

def extract_type(identifier)
  identifier[:type]
end

def find_related_record(type, id, association, is_polymorphic)

def find_related_record(type, id, association, is_polymorphic)
  related_model_class = resolve_related_model_class(type, association, is_polymorphic)
  resource_class = JSONAPI::ResourceLoader.find_for_model(related_model_class)
  resource_class.records.find(id)
rescue ActiveRecord::RecordNotFound
  raise ArgumentError, "Related resource not found: #{type} with id #{id}"
rescue NameError
  raise ArgumentError, "Invalid relationship type: #{type} does not correspond to a valid model class"
end

def polymorphic_association?(definition, relationship_name)

def polymorphic_association?(definition, relationship_name)
  relationship_def = definition.relationship_definitions.find do |r|
    r[:name].to_s == relationship_name.to_s
  end
  return false unless relationship_def
  relationship_def[:options][:polymorphic] == true
end

def polymorphic_association_for_association?(association, definition)

def polymorphic_association_for_association?(association, definition)
  return false unless definition
  relationship_name = association.name
  polymorphic_association?(definition, relationship_name)
end

def resolve_and_find_related_record(identifier, association:, definition:, relationship_name:)

def resolve_and_find_related_record(identifier, association:, definition:, relationship_name:)
  type = extract_type(identifier)
  id = extract_id(identifier)
  raise ArgumentError, "Missing type or id in relationship data" unless type && id
  is_polymorphic = polymorphic_association?(definition, relationship_name)
  validate_relationship_type!(type, association, definition) unless is_polymorphic
  find_related_record(type, id, association, is_polymorphic)
end

def resolve_related_model_class(type, association, is_polymorphic)

def resolve_related_model_class(type, association, is_polymorphic)
  return TypeConversion.type_to_class_name(type).constantize if is_polymorphic
  association.klass
end

def resolve_type_format_from_incoming(type, definition)

def resolve_type_format_from_incoming(type, definition)
  # Detect format from incoming type string
  if type.include?("/")
    :prefixed
  elsif definition.respond_to?(:type_format) && definition.type_format
    definition.type_format
  else
    JSONAPI.configuration.namespace_type_format
  end
end

def serialize_identifier(record, association:, definition:, use_instance_class: false)

def serialize_identifier(record, association:, definition:, use_instance_class: false)
  model_class = determine_model_class(record, association:, definition:,
                                              use_instance_class:,)
  related_definition = JSONAPI::ResourceLoader.find_for_model(model_class)
  related_type = TypeConversion.resource_type_name(related_definition)
  { type: related_type, id: record.id.to_s }
end

def sti_subclass?(instance_class, association_class)

def sti_subclass?(instance_class, association_class)
  return false unless instance_class.respond_to?(:base_class)
  instance_class.base_class == association_class && instance_class != association_class
end

def validate_relationship_type!(type, association, definition = nil)

def validate_relationship_type!(type, association, definition = nil)
  # Get expected type using the same format as the incoming type
  format = resolve_type_format_from_incoming(type, definition)
  expected_type = TypeConversion.model_type_name(association.klass, format:)
  # Also check flat type for backwards compatibility
  expected_flat = TypeConversion.model_type_name(association.klass, format: :flat)
  return if type == expected_type || type == expected_flat
  raise ArgumentError, "Invalid relationship type: expected #{expected_type}, got #{type}"
end