<commit_before>module SmartAnswer
  class CalculateStatutorySickPayFlow < Flow
    MINIMUM_NUMBER_OF_DAYS_IN_PERIOD_OF_INCAPACITY_TO_WORK = 4

    def define
      content_id "1c676a9e-0424-4ebb-bab8-d8cb8d2fc6f8"
      name 'calculate-statutory-sick-pay'

      status :published
      satisfies_need "100262"

      # Question 1
      checkbox_question :is_your_employee_getting? do
        option :statutory_maternity_pay
        option :maternity_allowance
        option :statutory_paternity_pay
        option :statutory_adoption_pay
        option :additional_statutory_paternity_pay

        calculate :employee_not_entitled_pdf do
          # this avoids lots of content duplication in the YML
          PhraseList.new(:ssp_link)
        end

        calculate :paternity_maternity_warning do |response|
          (response.split(",") & %w{statutory_paternity_pay additional_statutory_paternity_pay statutory_adoption_pay}).any?
        end

        permitted_next_nodes = [:employee_tell_within_limit?, :already_getting_maternity]
        next_node(permitted: permitted_next_nodes) do |response|
          if (response.split(",") & %w{statutory_paternity_pay additional_statutory_paternity_pay statutory_adoption_pay none}).any?
            :employee_tell_within_limit?
          else
            :already_getting_maternity
          end
        end
      end

      # Question 2
      multiple_choice :employee_tell_within_limit? do
        option :yes
        option :no

        calculate :enough_notice_of_absence do |response|
          response == 'yes'
        end

        next_node(permitted: [:employee_work_different_days?]) do
          :employee_work_different_days?
        end
      end

      # Question 3
      multiple_choice :employee_work_different_days? do
        option :yes
        option :no

        permitted_next_nodes = [
          :not_regular_schedule,
          :first_sick_day?
        ]
        next_node(permitted: permitted_next_nodes) do |response|
          case response
          when 'yes'
            :not_regular_schedule # Answer 4
          when 'no'
            :first_sick_day? # Question 4
          end
        end
      end

      # Question 4
      date_question :first_sick_day? do
        from { Date.new(2011, 1, 1) }
        to { Date.today.end_of_year }
        validate_in_range

        calculate :sick_start_date do |response|
          response
        end

        calculate :sick_start_date_for_awe do |response|
          response
        end

        next_node(permitted: [:last_sick_day?]) do
          :last_sick_day?
        end
      end

      # Question 5
      date_question :last_sick_day? do
        from { Date.new(2011, 1, 1) }
        to { Date.today.end_of_year }
        validate_in_range

        calculate :sick_end_date do |response|
          response
        end

        calculate :sick_end_date_for_awe do |response|
          response
        end

        next_node_calculation(:days_sick) do |response|
          period = DateRange.new(begins_on: sick_start_date, ends_on: response)
          period.number_of_days
        end

        validate { days_sick >= 1 }

        permitted_next_nodes = [:has_linked_sickness?, :must_be_sick_for_4_days]
        next_node(permitted: permitted_next_nodes) do |response|
          if days_sick >= MINIMUM_NUMBER_OF_DAYS_IN_PERIOD_OF_INCAPACITY_TO_WORK
            :has_linked_sickness?
          else
            :must_be_sick_for_4_days
          end
        end
      end

      # Question 6
      multiple_choice :has_linked_sickness? do
        option :yes
        option :no

        save_input_as :has_linked_sickness

        permitted_next_nodes = [
          :linked_sickness_start_date?,
          :paid_at_least_8_weeks?
        ]
        next_node(permitted: permitted_next_nodes) do |response|
          case response
          when 'yes'
            :linked_sickness_start_date?
          when 'no'
            :paid_at_least_8_weeks?
          end
        end
      end

      # Question 6.1
      date_question :linked_sickness_start_date? do
        from { Date.new(2010, 1, 1) }
        to { Date.today.end_of_year }
        validate_in_range

        next_node_calculation :sick_start_date_for_awe do |response|
          response
        end

        validate :linked_sickness_must_be_before do
          sick_start_date > sick_start_date_for_awe
        end

        next_node(permitted: [:linked_sickness_end_date?]) do
          :linked_sickness_end_date?
        end
      end

      # Question 6.2
      date_question :linked_sickness_end_date? do
        from { Date.new(2010, 1, 1) }
        to { Date.today.end_of_year }
        validate_in_range

        next_node_calculation :sick_end_date_for_awe do |response|
          response
        end

        validate :must_be_within_eight_weeks do
          furthest_allowed_date = sick_start_date - 8.weeks
          sick_end_date_for_awe > furthest_allowed_date
        end

        validate :must_be_at_least_1_day_before_first_sick_day do
          sick_end_date_for_awe < sick_start_date - 1
        end

        validate :must_be_valid_period_of_incapacity_for_work do |response|
          period = DateRange.new(begins_on: sick_start_date_for_awe, ends_on: response)
          period.number_of_days >= MINIMUM_NUMBER_OF_DAYS_IN_PERIOD_OF_INCAPACITY_TO_WORK
        end

        next_node(:paid_at_least_8_weeks?)
      end

      # Question 7.1
      multiple_choice :paid_at_least_8_weeks? do
        option :eight_weeks_more
        option :eight_weeks_less
        option :before_payday

        save_input_as :eight_weeks_earnings

        permitted_next_nodes = [
          :how_often_pay_employee_pay_patterns?,
          :total_earnings_before_sick_period?
        ]
        next_node(permitted: permitted_next_nodes) do |response|
          case response
          when 'eight_weeks_more', 'before_payday'
            :how_often_pay_employee_pay_patterns? # Question 7.2
          when 'eight_weeks_less'
            :total_earnings_before_sick_period? # Question 10
          end
        end
      end

      # Question 7.2<commit_msg>Use next_node with a block in SSP Q6.2<commit_after>module SmartAnswer
  class CalculateStatutorySickPayFlow < Flow
    MINIMUM_NUMBER_OF_DAYS_IN_PERIOD_OF_INCAPACITY_TO_WORK = 4

    def define
      content_id "1c676a9e-0424-4ebb-bab8-d8cb8d2fc6f8"
      name 'calculate-statutory-sick-pay'

      status :published
      satisfies_need "100262"

      # Question 1
      checkbox_question :is_your_employee_getting? do
        option :statutory_maternity_pay
        option :maternity_allowance
        option :statutory_paternity_pay
        option :statutory_adoption_pay
        option :additional_statutory_paternity_pay

        calculate :employee_not_entitled_pdf do
          # this avoids lots of content duplication in the YML
          PhraseList.new(:ssp_link)
        end

        calculate :paternity_maternity_warning do |response|
          (response.split(",") & %w{statutory_paternity_pay additional_statutory_paternity_pay statutory_adoption_pay}).any?
        end

        permitted_next_nodes = [:employee_tell_within_limit?, :already_getting_maternity]
        next_node(permitted: permitted_next_nodes) do |response|
          if (response.split(",") & %w{statutory_paternity_pay additional_statutory_paternity_pay statutory_adoption_pay none}).any?
            :employee_tell_within_limit?
          else
            :already_getting_maternity
          end
        end
      end

      # Question 2
      multiple_choice :employee_tell_within_limit? do
        option :yes
        option :no

        calculate :enough_notice_of_absence do |response|
          response == 'yes'
        end

        next_node(permitted: [:employee_work_different_days?]) do
          :employee_work_different_days?
        end
      end

      # Question 3
      multiple_choice :employee_work_different_days? do
        option :yes
        option :no

        permitted_next_nodes = [
          :not_regular_schedule,
          :first_sick_day?
        ]
        next_node(permitted: permitted_next_nodes) do |response|
          case response
          when 'yes'
            :not_regular_schedule # Answer 4
          when 'no'
            :first_sick_day? # Question 4
          end
        end
      end

      # Question 4
      date_question :first_sick_day? do
        from { Date.new(2011, 1, 1) }
        to { Date.today.end_of_year }
        validate_in_range

        calculate :sick_start_date do |response|
          response
        end

        calculate :sick_start_date_for_awe do |response|
          response
        end

        next_node(permitted: [:last_sick_day?]) do
          :last_sick_day?
        end
      end

      # Question 5
      date_question :last_sick_day? do
        from { Date.new(2011, 1, 1) }
        to { Date.today.end_of_year }
        validate_in_range

        calculate :sick_end_date do |response|
          response
        end

        calculate :sick_end_date_for_awe do |response|
          response
        end

        next_node_calculation(:days_sick) do |response|
          period = DateRange.new(begins_on: sick_start_date, ends_on: response)
          period.number_of_days
        end

        validate { days_sick >= 1 }

        permitted_next_nodes = [:has_linked_sickness?, :must_be_sick_for_4_days]
        next_node(permitted: permitted_next_nodes) do |response|
          if days_sick >= MINIMUM_NUMBER_OF_DAYS_IN_PERIOD_OF_INCAPACITY_TO_WORK
            :has_linked_sickness?
          else
            :must_be_sick_for_4_days
          end
        end
      end

      # Question 6
      multiple_choice :has_linked_sickness? do
        option :yes
        option :no

        save_input_as :has_linked_sickness

        permitted_next_nodes = [
          :linked_sickness_start_date?,
          :paid_at_least_8_weeks?
        ]
        next_node(permitted: permitted_next_nodes) do |response|
          case response
          when 'yes'
            :linked_sickness_start_date?
          when 'no'
            :paid_at_least_8_weeks?
          end
        end
      end

      # Question 6.1
      date_question :linked_sickness_start_date? do
        from { Date.new(2010, 1, 1) }
        to { Date.today.end_of_year }
        validate_in_range

        next_node_calculation :sick_start_date_for_awe do |response|
          response
        end

        validate :linked_sickness_must_be_before do
          sick_start_date > sick_start_date_for_awe
        end

        next_node(permitted: [:linked_sickness_end_date?]) do
          :linked_sickness_end_date?
        end
      end

      # Question 6.2
      date_question :linked_sickness_end_date? do
        from { Date.new(2010, 1, 1) }
        to { Date.today.end_of_year }
        validate_in_range

        next_node_calculation :sick_end_date_for_awe do |response|
          response
        end

        validate :must_be_within_eight_weeks do
          furthest_allowed_date = sick_start_date - 8.weeks
          sick_end_date_for_awe > furthest_allowed_date
        end

        validate :must_be_at_least_1_day_before_first_sick_day do
          sick_end_date_for_awe < sick_start_date - 1
        end

        validate :must_be_valid_period_of_incapacity_for_work do |response|
          period = DateRange.new(begins_on: sick_start_date_for_awe, ends_on: response)
          period.number_of_days >= MINIMUM_NUMBER_OF_DAYS_IN_PERIOD_OF_INCAPACITY_TO_WORK
        end

        next_node(permitted: [:paid_at_least_8_weeks?]) do
          :paid_at_least_8_weeks?
        end
      end

      # Question 7.1
      multiple_choice :paid_at_least_8_weeks? do
        option :eight_weeks_more
        option :eight_weeks_less
        option :before_payday

        save_input_as :eight_weeks_earnings

        permitted_next_nodes = [
          :how_often_pay_employee_pay_patterns?,
          :total_earnings_before_sick_period?
        ]
        next_node(permitted: permitted_next_nodes) do |response|
          case response
          when 'eight_weeks_more', 'before_payday'
            :how_often_pay_employee_pay_patterns? # Question 7.2
          when 'eight_weeks_less'
            :total_earnings_before_sick_period? # Question 10
          end
        end
      end

      # Question 7.2
{'fraction_non_alphanumeric': 0.05607024893257069, 'fraction_numerical': 0.012648030290824137, 'mean_word_length': 2.213564587108465, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, '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': '1688410', 'n_tokens_mistral': 4183, 'n_tokens_neox': 3854, 'n_words': 720}