class Admin::Legacy::HumanResources::EmployeesController < Admin::Legacy::HumanResources::Controller
  def index
    data = employees
      .preload(:position, :manager, :state_user, :state_id, :departments, :categories, :substitute)
      .is_current(params[:current]&.to_bool)
      .by_level(params[:level])
      .by_type(params[:type_id])
      .by_categories(params[:category_ids])
      .by_departments(params[:department_ids])
      .by_substitute(params[:substitute]&.to_bool)
      .by_main_district_site
      .distinct

    render_success :ok, json: data.map { |d| employee_props(d) }
  end

  def show
    render_success :ok, json: employee_props(employee)
  end

  def update
    if employee.update(employee_params)
      render_success :ok, json: employee_props(employee.reload)
    else
      render_error :unprocessable_entity, errors: employee
    end
  end

  def destroy
    if employee.delete_from_legacy(current_user)
      render_success :ok
    else
      render_error :unprocessable_entity, errors: employee
    end
  end

  def password_update
    unless params[:password] == params[:password_confirmation]
      employee.add_custom_error(:password_confirmation, 'must match password')
    end

    if employee.update(encrypted_password: params[:password], password_alert: true)
      render_success :ok, json: employee_props(employee.reload)
    else
      render_error :unprocessable_entity, errors: employee
    end
  end

  private
    def employees
      current_school.employees
    end

    def employee
      @employee ||= employees.find_by(id: params[:id])
    end

    def detail
      @detail ||= EmployeeDetail.find_or_initialize_by(employee: employee)
    end

    def employee_params
      params.permit(
        :title_id,
        :first_name,
        :middle_name,
        :last_name,
        :suffix,
        :pay_status,
        :active,
        :username,
        :level,
        :date_of_birth,
        :gender,
        :service_years,
        :race_id,
        :pin,
        :pin_confirmation,
        :email,
        :email_3,
        detail_attributes: [:id, :ncea_status, :ethnicity],
        substitute_attributes: [:id, :employee_id, :_destroy],
        department_employees_attributes: [:id, :department_id, :_destroy]
      ).to_h.deep_merge(set_associations)
    end

    def set_associations
      {}.tap do |prop|
        prop[:position] = position if params.key?(:position_id)
        prop[:manager] = manager if params.key?(:manager_id)
        prop[:location] = location if params.key?(:location_id)
        prop[:building] = building if params.key?(:building_id)
        prop[:detail_attributes] = { author_id: current_user.id } if params.key?(:detail_attributes)
      end
    end

    def position
      @position ||= current_school.positions.find_by(id: params[:position_id])
    end

    def manager
      @manager ||= current_school.employees.find_by(id: params[:manager_id])
    end

    def location
      @location ||= current_school.facility_locations.find_by(id: params[:location_id])
    end

    def building
      @building ||= current_school.facility_buildings.find_by(id: params[:building_id])
    end

    def employee_props(employee)
      {}.tap do |prop|
        prop[:id] = employee.id
        prop[:employee_id] = employee.employee_id
        prop[:first_name] = employee.first_name
        prop[:last_name] = employee.last_name
        prop[:middle_name] = employee.middle_name
        prop[:suffix] = employee.suffix
        prop[:nickname] = employee.nickname
        prop[:full_name] = employee.full_name(:reverse)
        prop[:username] = employee.username
        prop[:active] = employee.active?
        prop[:level] = employee.level
        prop[:date_of_birth] = employee.date_of_birth
        prop[:birth_date] = employee.decorate.birth_date
        prop[:position_name] = employee.decorate.position_name
        prop[:position_id] = employee.position_id? ? employee.position_id : nil
        prop[:manager_name] = employee.decorate.manager_full_name(:reverse)
        prop[:manager_id] = employee.manager_id? ? employee.manager_id : nil
        prop[:directory] = employee.directory?
        prop[:state_user] = employee.state_user.present?
        prop[:state_id] = employee.state_id&.number
        prop[:title_id] = employee.title_id? ? employee.title_id : nil
        prop[:pay_status] = employee.pay_status
        prop[:gender] = employee.gender
        prop[:gender_label] = employee.decorate.gender_label
        prop[:current] = employee.current?
        prop[:service_years] = employee.service_years
        prop[:password_changed] = employee.password_changed
        prop[:email] = employee.email
        prop[:email_3] = employee.email_3
        prop[:location_id] = employee.location_id? ? employee.location_id : nil
        prop[:building_id] = employee.building_id? ? employee.building_id : nil
        prop[:photo_url] = employee.path_to_photo
        prop[:is_substitute] = employee.substitute.present?
        prop[:type_id] = employee.type_id? ? employee.type_id : nil
        prop[:category_ids] = employee.category_ids
        prop[:department_ids] = employee.department_ids
        prop[:department_employees_attributes] = employee.department_employees.map do |department|
          { id: department.id, deparment_id: department.department_id }
        end
        return prop unless [:show, :update].include?(action_name.to_sym)

        prop[:primary_email] = employee.primary_email_address
        prop[:primary_suppression] = employee.primary_email_address&.suppression
        prop[:title_name] = employee.decorate.title_name
        prop[:building_name] = employee.decorate.building_name
        prop[:location_name] = employee.decorate.location_name
        prop[:type_name] = employee.decorate.type_name
        prop[:manager_name] = employee.decorate.manager_full_name
        prop[:race_name] = employee.decorate.race_name
        prop[:race_id] = employee.race_id? ? employee.race_id : nil
        prop[:departments] = employee.departments.pluck(:name)
        prop[:detail_attributes] = {
          id: detail.id,
          ncea_status: detail.ncea_status,
          ethnicity: detail.ethnicity
        }
        prop[:ethnicity] = detail.ethnicity
        prop[:ncea_status] = detail.ncea_status
        prop[:is_substitute] = employee.substitute.present?
        prop[:substitute_attributes] = {
          id: employee.substitute&.id,
          employee_id: employee.id
        }
        prop[:social_security_number] = employee.decorate.social_security_number_number
        prop[:additional_ids_attributes] = employee.additional_ids.map do |id|
          {
            id: id.id,
            code: id.code,
            number: id.number
          }
        end
      end
    end
end
