module Imports
  class OrganisationImportService < ImportService
    def create_organisations(folder)
      import_from(folder, :create_organisation)
    end

  private

    PROVIDER_TYPE = {
      "HOUSING-ASSOCIATION" => Organisation.provider_types[:PRP],
      "LOCAL-AUTHORITY" => Organisation.provider_types[:LA],
    }.freeze

    def create_organisation(xml_document)
      old_visible_id = organisation_field_value(xml_document, "visible-id")

      Organisation.create!(
        name: organisation_field_value(xml_document, "name"),
        provider_type: PROVIDER_TYPE[organisation_field_value(xml_document, "institution-type")],
        phone: organisation_field_value(xml_document, "telephone-number"),
        holds_own_stock: to_boolean(organisation_field_value(xml_document, "holds-stock")),
        active: to_boolean(organisation_field_value(xml_document, "active")),
        old_association_type: organisation_field_value(xml_document, "old-association-type"),
        software_supplier_id: organisation_field_value(xml_document, "software-supplier-id"),
        housing_management_system: organisation_field_value(xml_document, "housing-management-system"),
        choice_based_lettings: to_boolean(organisation_field_value(xml_document, "choice-based-lettings")),
        common_housing_register: to_boolean(organisation_field_value(xml_document, "common-housing-register")),
        choice_allocation_policy: to_boolean(organisation_field_value(xml_document, "choice-allocation-policy")),
        cbl_proportion_percentage: organisation_field_value(xml_document, "cbl-proportion-percentage"),
        enter_affordable_logs: to_boolean(organisation_field_value(xml_document, "enter-affordable-logs")),
        owns_affordable_logs: to_boolean(organisation_field_value(xml_document, "owns-affordable-rent")),
        housing_registration_no: organisation_field_value(xml_document, "housing-registration-no"),
        general_needs_units: organisation_field_value(xml_document, "general-needs-units"),
        supported_housing_units: organisation_field_value(xml_document, "supported-housing-units"),
        unspecified_units: organisation_field_value(xml_document, "unspecified-units"),
        old_org_id: organisation_field_value(xml_document, "id"),
        old_visible_id:,
      )
    rescue ActiveRecord::RecordNotUnique
      name = organisation_field_value(xml_document, "name")
      @logger.warn("Organisation #{name} is already present with old visible ID #{old_visible_id}, skipping.")
    rescue ActiveRecord::RecordInvalid
      @logger.error("Organisation #{old_visible_id}: Failed to import")
      raise
    end

    def organisation_field_value(xml_document, field)
      field_value(xml_document, "institution", field)
    end
  end
end