class Scheme < ApplicationRecord
  belongs_to :organisation
  has_many :locations
  has_many :case_logs

  scope :search_by_code, ->(code) { where("code ILIKE ?", "%#{code}%") }
  scope :search_by_service_name, ->(name) { where("service_name ILIKE ?", "%#{name}%") }
  scope :search_by_postcode, ->(postcode) { joins(:locations).where("locations.postcode ILIKE ?", "%#{postcode.delete(' ')}%") }
  scope :search_by, ->(param) { search_by_postcode(param).or(search_by_service_name(param)).or(search_by_code(param)).distinct }

  SCHEME_TYPE = {
    0 => "Missings",
    4 => "Foyer",
    5 => "Direct Access Hostel",
    6 => "Other Supported Housing",
    7 => "Housing for older people",
  }.freeze

  PRIMARY_CLIENT_GROUP = {
    "O" => "Homeless families with support needs",
    "H" => "Offenders & people at risk of offending",
    "M" => "Older people with support needs",
    "L" => "People at risk of domestic violence",
    "A" => "People with a physical or sensory disability",
    "G" => "People with alcohol problems",
    "F" => "People with drug problems",
    "B" => "People with HIV or AIDS",
    "D" => "People with learning disabilities",
    "E" => "People with mental health problems",
    "I" => "Refugees (permanent)",
    "S" => "Rough sleepers",
    "N" => "Single homeless people with support needs",
    "R" => "Teenage parents",
    "Q" => "Young people at risk",
    "P" => "Young people leaving care",
    "X" => "Missing",
  }.freeze

  SUPPORT_TYPE = {
    0 => "Missing",
    1 => "Resettlement Support",
    2 => "Low levels of support",
    3 => "Medium levels of support",
    4 => "High levels of care and support",
    5 => "Nursing care services to a care home",
    6 => "Floating Support",
  }.freeze

  INTENDED_STAY = {
    "M" => "Medium stay",
    "P" => "Permanent",
    "S" => "Short Stay",
    "V" => "Very short stay",
    "X" => "Missing",
  }.freeze

  REGISTERED_UNDER_CARE_ACT = {
    0 => "No",
    1 => "Yes – part registered as a care home",
  }.freeze

  SENSITIVE = {
    0 => "No",
    1 => "Yes",
  }.freeze

  def display_attributes
    [
      { name: "Service code", value: code },
      { name: "Name", value: service_name },
      { name: "Confidential information", value: sensitive_display },
      { name: "Managed by", value: organisation.name },
      { name: "Type of scheme", value: scheme_type_display },
      { name: "Registered under Care Standards Act 2000", value: registered_under_care_act_display },
      { name: "Total number of units", value: total_units },
      { name: "Primary client group", value: primary_client_group_display },
      { name: "Secondary client group", value: secondary_client_group_display },
      { name: "Level of support given", value: support_type_display },
      { name: "Intended length of stay", value: intended_stay_display },
    ]
  end

  def scheme_type_display
    SCHEME_TYPE[scheme_type]
  end

  def sensitive_display
    SENSITIVE[sensitive]
  end

  def registered_under_care_act_display
    REGISTERED_UNDER_CARE_ACT[registered_under_care_act]
  end

  def primary_client_group_display
    PRIMARY_CLIENT_GROUP[primary_client_group]
  end

  def secondary_client_group_display
    PRIMARY_CLIENT_GROUP[secondary_client_group]
  end

  def support_type_display
    SUPPORT_TYPE[support_type]
  end

  def intended_stay_display
    INTENDED_STAY[intended_stay]
  end
end