class EdFi::LogDecorator < ApplicationDecorator
  def title
    case endpoint.to_sym
    when :calendardates
      associated.date
    when :calendars, :sessions
      associated.id
    when :studentschoolassociations, :students, :studenteducationorganizationassociations,
      :studenteducationorganizationresponsibilityassociations,
      :studentcurricularmaterialprogramassociations,
      :studenteducationorganizationassessmentassociations

      student = associated_type.to_sym == :Student ? associated : associated.student
      student.full_name
    when :studentschoolattendanceevents
      "#{h.l(associated.date)} #{associated.student.full_name}"
    when :staffs, :staffeducationorganizationemploymentassociations,
      :staffeducationorganizationcontactassociations,
      :staffeducationorganizationassignmentassociations,
      :studentacademicrecords

      associated.full_name
    when :parents
      associated.last_name
    when :studentprogramassociations, :coursetranscripts
      associated.student.full_name
    when :courseofferings
      JSON.parse(json)['localCourseCode']
    when :sections
      JSON.parse(json)['courseOfferingReference']['localCourseCode']
    when :staffsectionassociations
      staff_id = JSON.parse(json)['staffReference']['staffUniqueId']
      school_year.school.employees
        .joins(:additional_ids)
        .find_by(additional_ids: { number: staff_id })
        &.full_name
    when :studentparentassociations
      student_id = JSON.parse(json)['studentReference']['studentUniqueId']
      school_year.school_year_students.find_by(edfi_id: student_id)&.student&.full_name
    when :studentsectionassociations, :studentschoolgraduationplans
      student_id = JSON.parse(json)['studentReference']['studentUniqueId']
      school_year.school.student_additional_ids.find_by(number: student_id)&.student&.full_name
    end
  end
end
