class EdFi::Indiana::V2026::StudentsService < EdFi::Indiana::V2026::ApplicationService
  def call
    data = school_year_students
      .includes(student: [:family, :language, { detail: :birth_country }])
      .where('ExitDate = ? OR ExitDate >= ?', '0000-00-00', Date.current)

    data.each { |s| create(s) }
  end

  def endpoint
    'ed-fi/students'
  end

  private
    def language_descriptors
      @language_descriptors ||= EdFi::Descriptor
        .by_language_descriptor(school)
        .map { |l| [l.key, l.code] }
        .to_h
    end

    def props(year)
      student = year.student
      family = student.family
      detail = student.detail
      student_language = student.language
      language_id = student_language&.language_id? ? student_language.language_id : 1
      language = language_descriptors[language_id]
      language_use = student_language&.use || 'Home language'
      country = countries[detail&.birth_country&.alpha2 || 'US']
      country_descriptor = "uri://doe.in.gov/CountryDescriptor##{country}"

      {
        studentUniqueId: student.state_id,
        firstName: student.first_name,
        middleName: student.middle_name,
        lastSurname: student.last_name,
        generationCodeSuffix: student.suffix,
        birthSexDescriptor: "uri://ed-fi.org/SexDescriptor##{student.decorate.gender_label}",
        birthDate: student.date_of_birth.strftime('%Y-%m-%d'),
        birthCountryDescriptor: country_descriptor.to_s
      }
    end

    def create(data)
      response = rest_client_request(:post, url, data.student, body: props(data).to_json)
      return unless response

      edfi_id = response.headers[:location].split('/').last
      data.student.update(edfi_id: edfi_id)
    end

    def countries
      @countries ||= [
        ['AF', '001'],
        ['AL', '002'],
        ['DZ', '003'],
        ['AD', '004'],
        ['AO', '005'],
        ['AG', '006'],
        ['AR', '007'],
        ['AM', '008'],
        ['AU', '009'],
        ['AT', '010'],
        ['AZ', '011'],
        ['BS', '012'],
        ['BH', '013'],
        ['BD', '014'],
        ['BB', '015'],
        ['BY', '016'],
        ['BE', '017'],
        ['BZ', '018'],
        ['BJ', '019'],
        ['BT', '020'],
        ['BO', '021'],
        ['BA', '022'],
        ['B2', '023'],
        ['BR', '024'],
        ['BN', '025'],
        ['BG', '026'],
        ['BF', '027'],
        ['BI', '028'],
        ['KH', '029'],
        ['CM', '030'],
        ['CA', '031'],
        ['CV', '032'],
        ['CF', '033'],
        ['TD', '034'],
        ['CL', '035'],
        ['CN', '036'],
        ['CO', '037'],
        ['KM', '038'],
        ['CG', '039'],
        ['CD', '040'],
        ['CR', '041'],
        ['HR', '042'],
        ['CU', '043'],
        ['CY', '044'],
        ['CA', '045'],
        ['CI', '046'],
        ['DK', '047'],
        ['DJ', '048'],
        ['DM', '049'],
        ['DO', '050'],
        ['TL', '051'],
        ['EC', '052'],
        ['EG', '053'],
        ['SV', '054'],
        ['GQ', '055'],
        ['ER', '056'],
        ['EE', '057'],
        ['ET', '058'],
        ['FJ', '059'],
        ['FI', '060'],
        ['FR', '061'],
        ['GA', '062'],
        ['GM', '063'],
        ['GE', '064'],
        ['DE', '065'],
        ['GH', '066'],
        ['GR', '067'],
        ['GD', '068'],
        ['GT', '069'],
        ['GN', '070'],
        ['GW', '071'],
        ['GY', '072'],
        ['HT', '073'],
        ['HN', '074'],
        ['HU', '075'],
        ['IS', '076'],
        ['IN', '077'],
        ['ID', '078'],
        ['IR', '079'],
        ['IQ', '080'],
        ['IE', '081'],
        ['IL', '082'],
        ['IT', '083'],
        ['JM', '084'],
        ['JP', '085'],
        ['JO', '086'],
        ['KZ', '087'],
        ['KE', '088'],
        ['KI', '089'],
        ['KP', '090'],
        ['KR', '091'],
        ['KW', '092'],
        ['KG', '093'],
        ['LA', '094'],
        ['LV', '095'],
        ['LB', '096'],
        ['LS', '097'],
        ['LR', '098'],
        ['LY', '099'],
        ['LI', '100'],
        ['LT', '101'],
        ['LU', '102'],
        ['MK', '103'],
        ['MG', '104'],
        ['MW', '105'],
        ['MY', '106'],
        ['MV', '107'],
        ['ML', '108'],
        ['MT', '109'],
        ['MH', '110'],
        ['MR', '111'],
        ['MU', '112'],
        ['MX', '113'],
        ['MF', '114'],
        ['MD', '115'],
        ['MC', '116'],
        ['MN', '117'],
        ['MA', '118'],
        ['MZ', '119'],
        ['MM', '120'],
        ['NA', '121'],
        ['NR', '122'],
        ['NP', '123'],
        ['NL', '124'],
        ['NZ', '125'],
        ['NI', '126'],
        ['NE', '127'],
        ['NG', '128'],
        ['NO', '129'],
        ['OM', '130'],
        ['PK', '131'],
        ['PW', '132'],
        ['PA', '133'],
        ['PG', '134'],
        ['PY', '135'],
        ['PE', '136'],
        ['PH', '137'],
        ['PL', '138'],
        ['PT', '139'],
        ['QA', '140'],
        ['RO', '141'],
        ['RU', '142'],
        ['RW', '143'],
        ['KN', '144'],
        ['LC', '145'],
        ['VC', '146'],
        ['WS', '147'],
        ['SM', '148'],
        ['ST', '149'],
        ['SA', '150'],
        ['SN', '151'],
        ['RS', '152'],
        ['SC', '153'],
        ['SL', '154'],
        ['SG', '155'],
        ['SK', '156'],
        ['SI', '157'],
        ['SB', '158'],
        ['SO', '159'],
        ['ZA', '160'],
        ['ES', '161'],
        ['LS', '162'],
        ['SD', '163'],
        ['SR', '164'],
        ['SZ', '165'],
        ['SE', '166'],
        ['CH', '167'],
        ['SY', '168'],
        ['TW', '169'],
        ['TJ', '170'],
        ['TZ', '171'],
        ['TH', '172'],
        ['TG', '173'],
        ['TO', '174'],
        ['TT', '175'],
        ['TN', '176'],
        ['TR', '177'],
        ['TM', '178'],
        ['TV', '179'],
        ['UG', '180'],
        ['UA', '181'],
        ['AE', '182'],
        ['GB', '183'],
        ['UY', '184'],
        ['UZ', '185'],
        ['VU', '186'],
        ['VT', '187'],
        ['VE', '188'],
        ['VN', '189'],
        ['EH', '190'],
        ['YE', '191'],
        ['ZM', '192'],
        ['ZW', '193'],
        ['AW', '195'],
        ['PS', '197'],
        ['XK', '198'],
        ['US', 'USA']
      ].to_h
    end
end
