<commit_before># frozen_string_literal: true

require_dependency "renalware/ukrdc"

# rubocop:disable Metrics/ClassLength
module Renalware
  module UKRDC
    class PatientPresenter < SimpleDelegator
      UKRDC_MAX_PHONE_LEN = 80

      attr_reader :changes_since, :changes_up_until
      delegate :prd_description, to: :profile, allow_nil: true
      delegate :code, :term, to: :prd_description, allow_nil: true, prefix: true

      # rubocop:disable Metrics/MethodLength
      def initialize(patient, changes_since: nil)
        if changes_since.present? && changes_since.is_a?(String)
          changes_since = Time.zone.parse(changes_since)
        end
        # TODO: document what's happening here with dates
        @changes_since = changes_since ||
                         patient.sent_to_ukrdc_at ||
                         Renalware.config.ukrdc_default_changes_since_date
        if @changes_since.blank?
          raise(
            ArgumentError,
            "No date for comparison: patient#sent_to_ukrdc_at and changes_since are nil"
          )
        end
        @changes_up_until = Time.zone.now
        super(patient)
      end
      # rubocop:enable Metrics/MethodLength

      def language
        return if super.nil? || super.name == "Unknown"

        super
      end

      def modalities
        __getobj__.modalities.includes(:description).order(started_on: :asc, created_at: :asc)
      end

      def dead?
        current_modality_death?
      end

      def current_modality_hd?
        return false if current_modality.blank?

        current_modality.description.is_a?(Renalware::HD::ModalityDescription)
      end

      def letters
        return Letters::Letter.none if send_to_rpv == false

        CollectionPresenter.new(
          letters_patient
            .letters
            .approved_or_completed
            .includes(:updated_by, :author, :letterhead, :archive)
            .where("updated_at > ?", changes_since),
          Renalware::Letters::LetterPresenterFactory
        )
      end

      def finished_hd_sessions
        hd_patient
          .finished_hd_sessions
          .includes(:patient, :dialysate, :updated_by)
          .where("hd_sessions.updated_at > ?", changes_since)
      end

      def current_registration_status_rr_code
        @current_registration_status_rr_code ||= begin
          status = transplant_patient.current_registration_status
          status&.description&.rr_code
        end
      end

      def hospital_unit_code
        letter_head.site_code
      end<commit_msg>Fix a Letters const resultion bug in UKRDC export<commit_after># frozen_string_literal: true

require_dependency "renalware/ukrdc"

# rubocop:disable Metrics/ClassLength
module Renalware
  module UKRDC
    class PatientPresenter < SimpleDelegator
      UKRDC_MAX_PHONE_LEN = 80

      attr_reader :changes_since, :changes_up_until
      delegate :prd_description, to: :profile, allow_nil: true
      delegate :code, :term, to: :prd_description, allow_nil: true, prefix: true

      # rubocop:disable Metrics/MethodLength
      def initialize(patient, changes_since: nil)
        if changes_since.present? && changes_since.is_a?(String)
          changes_since = Time.zone.parse(changes_since)
        end
        # TODO: document what's happening here with dates
        @changes_since = changes_since ||
                         patient.sent_to_ukrdc_at ||
                         Renalware.config.ukrdc_default_changes_since_date
        if @changes_since.blank?
          raise(
            ArgumentError,
            "No date for comparison: patient#sent_to_ukrdc_at and changes_since are nil"
          )
        end
        @changes_up_until = Time.zone.now
        super(patient)
      end
      # rubocop:enable Metrics/MethodLength

      def language
        return if super.nil? || super.name == "Unknown"

        super
      end

      def modalities
        __getobj__.modalities.includes(:description).order(started_on: :asc, created_at: :asc)
      end

      def dead?
        current_modality_death?
      end

      def current_modality_hd?
        return false if current_modality.blank?

        current_modality.description.is_a?(Renalware::HD::ModalityDescription)
      end

      def letters
        return ::Renalware::Letters::Letter.none if send_to_rpv == false

        CollectionPresenter.new(
          letters_patient
            .letters
            .approved_or_completed
            .includes(:updated_by, :author, :letterhead, :archive)
            .where("updated_at > ?", changes_since),
          Renalware::Letters::LetterPresenterFactory
        )
      end

      def finished_hd_sessions
        hd_patient
          .finished_hd_sessions
          .includes(:patient, :dialysate, :updated_by)
          .where("hd_sessions.updated_at > ?", changes_since)
      end

      def current_registration_status_rr_code
        @current_registration_status_rr_code ||= begin
          status = transplant_patient.current_registration_status
          status&.description&.rr_code
        end
      end

      def hospital_unit_code
        letter_head.site_code
      end
{'fraction_non_alphanumeric': 0.07083413973292045, 'fraction_numerical': 0.000774143603638475, 'mean_word_length': 2.379986919555265, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '366440', 'n_tokens_mistral': 1519, 'n_tokens_neox': 1449, 'n_words': 301}