<commit_before>module Api
  module V1
    module Indc
      class CountriesDocumentsSerializer < ActiveModel::Serializer
        attributes :documents, :framework, :sectoral, :data

        def data
          location_codes = object.locations.map(&:iso_code3)

          location_with_intent_to_submit = query_location_with_intent_to_submit(location_codes)
          documents_by_location = query_documents_by_location(location_codes)

          object.locations.map do |location|
            docs = documents_by_location[location.iso_code3] || []

            ordering = docs.empty? ? 0 : docs.last['ordering']

            # if country hasn't submitted a second_ndc, but they have expressed
            # the intention of doing so, by responding 'enhance_2020', or 'intend_2020'
            # on indicator with slug: ndce_status_2020 / ndce_status_2020_label
            if docs.select{|t| t['slug'] == 'second_ndc'}.empty? &&
                location_with_intent_to_submit.include?(location.iso_code3)
              docs += ::Indc::Document.select('indc_documents.*, NULL AS submission_date').
                where(slug: 'second_ndc')
            end

            if object.laws_info[location.iso_code3]
              docs += object.laws_info[location.iso_code3].map do |key, val|
                next unless val
                ordering += 1
                title = key == 'in_framework' ? 'Climate Framework Laws or Policies' : 'Sectoral Laws or Policies'
                {
                  id: nil,
                  ordering: ordering,
                  slug: key.gsub('in_', ''),
                  long_name: title,
                  description: title,
                  is_ndc: false,
                  submission_date: nil
                }
              end.compact
            end

            [location.iso_code3, docs]
          end.to_h
        end

        def documents
          ::Indc::Document.order(:ordering).map do |d|
            total_countries = Location.where(id: ::Indc::Submission.select(:location_id).where(document_id: d.id).
                                             distinct.pluck(:location_id)).count
            {
              id: d.id,
              ordering: d.ordering,
              slug: d.slug,
              long_name: d.long_name,
              description: d.description,
              is_ndc: d.is_ndc,
              total_countries: total_countries
            }
          end
        end

        def framework
          return [] unless object.laws_and_policies && object.laws_and_policies['targets']

          object.laws_and_policies['targets'].map do |target|
            next if target['sources'].empty? || !target['sources'].first['framework']
            source = target['sources'].first

            {
              id: source['id'],
              slug: "framework_#{source['id']}",
              long_name: source['title'],
              url: source['link'],
              iso: target['iso_code3']
            }
          end.compact.uniq
        end

        def sectoral
          return [] unless object.laws_and_policies && object.laws_and_policies['targets']

          object.laws_and_policies['targets'].map do |target|
            next if target['sources'].empty? || !target['sources'].first['sectoral']

            source = target['sources'].first

            {
              id: source['id'],
              slug: "sectoral_#{source['id']}",
              long_name: source['title'],
              url: source['link'],
              iso: target['iso_code3']
            }
          end.compact.uniq
        end

        private<commit_msg>target could be connected with many laws, both framework and structural. Fix selecting framework and structural laws from targets data<commit_after>module Api
  module V1
    module Indc
      class CountriesDocumentsSerializer < ActiveModel::Serializer
        attributes :documents, :framework, :sectoral, :data

        def data
          location_codes = object.locations.map(&:iso_code3)

          location_with_intent_to_submit = query_location_with_intent_to_submit(location_codes)
          documents_by_location = query_documents_by_location(location_codes)

          object.locations.map do |location|
            docs = documents_by_location[location.iso_code3] || []

            ordering = docs.empty? ? 0 : docs.last['ordering']

            # if country hasn't submitted a second_ndc, but they have expressed
            # the intention of doing so, by responding 'enhance_2020', or 'intend_2020'
            # on indicator with slug: ndce_status_2020 / ndce_status_2020_label
            if docs.select{|t| t['slug'] == 'second_ndc'}.empty? &&
                location_with_intent_to_submit.include?(location.iso_code3)
              docs += ::Indc::Document.select('indc_documents.*, NULL AS submission_date').
                where(slug: 'second_ndc')
            end

            # laws_info structure
            # [
            #   { 'USA': { in_framework: true, in_sectoral: false } },
            #   ...
            # ]
            if object.laws_info[location.iso_code3]
              docs += object.laws_info[location.iso_code3].map do |key, val|
                next unless val

                ordering += 1
                title = key == 'in_framework' ? 'Climate Framework Laws or Policies' : 'Sectoral Laws or Policies'
                {
                  id: nil,
                  ordering: ordering,
                  slug: key.gsub('in_', ''),
                  long_name: title,
                  description: title,
                  is_ndc: false,
                  submission_date: nil
                }
              end.compact
            end

            [location.iso_code3, docs]
          end.to_h
        end

        def documents
          ::Indc::Document.order(:ordering).map do |d|
            total_countries = Location.where(id: ::Indc::Submission.select(:location_id).where(document_id: d.id).
                                             distinct.pluck(:location_id)).count
            {
              id: d.id,
              ordering: d.ordering,
              slug: d.slug,
              long_name: d.long_name,
              description: d.description,
              is_ndc: d.is_ndc,
              total_countries: total_countries
            }
          end
        end

        def framework
          laws_and_policies.select { |law| law[:slug].starts_with?('framework') }
        end

        def sectoral
          laws_and_policies.select { |law| law[:slug].starts_with?('sectoral') }
        end

        private

        def laws_and_policies
          return [] unless object.laws_and_policies && object.laws_and_policies['targets']

          object.laws_and_policies['targets'].flat_map do |target|
            target['sources'].map do |law|
              next unless law['sectoral'] || law['framework']

              {
                id: law['id'],
                slug: "#{law['sectoral'] ? 'sectoral' : 'framework'}_#{law['id']}",
                long_name: law['title'],
                url: law['link'],
                iso: target['iso_code3']
              }
            end
          end.compact.uniq
        end
{'fraction_non_alphanumeric': 0.1008461645165765, 'fraction_numerical': 0.007351921209599112, 'mean_word_length': 1.6773115484589678, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1086293', 'n_tokens_mistral': 2042, 'n_tokens_neox': 1896, 'n_words': 404}