class Support::Sites::SchoolsController < Support::Sites::Controller
  def index
    data = School
      .by_active(params[:active]&.to_bool)
      .is_district(params[:district]&.to_bool)
      .by_site_types(params[:types])
      .ordered
      .pluck(:id, :name, :active, :site_type, :school_district_office)
      .map do |id, name, active, site_type, school_district_office|
      {
        id: id,
        name: name,
        academic_year: current_years[id],
        active: !active.zero?,
        district: !school_district_office.zero?,
        site_type: site_type
      }
    end
    render_success :ok, json: data
  end

  def show
    render_success :ok, json: school_props(school)
  end

  def create
    school = schools.build(school_params)
    school.build_school_config(school_config_params)
    school.employees.build(employee_params)

    if school.save
      render_success :ok, json: school_props(school)
    else
      render_error :unprocessable_entity, errors: school
    end
  end

  def update
    school.school_config.assign_attributes(school_config_params)
    school.find_or_build_customer_wizard.assign_attributes(school_customer_wizard_params)

    if school.update(school_params)
      render_success :ok, json: school_props(school)
    else
      render_error :unprocessable_entity
    end
  end

  def destroy
    Maintenance::SchoolDeleteJob.perform_async(params[:id])
    render_success :ok, message: 'Site queued for deletion.'
  end

  def district_type
    type = params[:type]
    school.update(school_district_office: true) if ['school', 'catholic'].include?(type)
    school.update(catholic_diocese_office: true) if type == 'catholic'

    if type.nil?
      school.update(school_district_office: false)
      school.update(catholic_diocese_office: false)
      SchoolDistrict.where(school_id: school.id)&.destroy_all
    end
    render_success :ok
  end

  def revoke_tokens
    Maintenance::RevokeFamilyStudentTokenJob.perform_async(params[:id])
    render_success :ok, message: 'Tokens revoked.'
  end

  private
    def schools
      School.order(active: :desc)
    end

    def school
      @school ||= schools.find_by(id: params[:id])
    end

    def district_school
      @district_school ||= schools.find_by(id: school&.school_district&.school_id)
    end

    def current_years
      @current_years ||= SchoolYear.where(current: true).pluck(:school_id, :academic_year).to_h
    end

    def school_params
      params.permit(
        :active,
        :name,
        :address,
        :address_ext,
        :city,
        :state,
        :zip,
        :country_id,
        :time_zone,
        :website_url,
        :site_type,
        :color,
        :site_environment,
        :support_id,
        :catholic_school
      )
    end

    def school_config_params
      params.permit(:color, :school_year_type, :non_binary)
    end

    def school_customer_wizard_params
      params.permit(:getting_started)
    end

    def employee_params
      params.permit(
        :title_id,
        :first_name,
        :last_name,
        :email,
        work_phone_attributes: [:number, :country_code],
        mobile_phone_attributes: [:number, :country_code]
      ).merge!(employee_defaults)
    end

    def employee_defaults
      {
        superuser: true,
        level: 2,
        active: true
      }
    end

    def school_props(school)
      district_school_count = SchoolDistrict.find_by(school_id: school.id)&.schools&.count

      {}.tap do |prop|
        prop[:id] = school.id
        prop[:name] = school.name
        prop[:site_type] = school.site_type
        prop[:site_environment] = school.site_environment
        prop[:country] = school.decorate.country_name
        prop[:city] = school.city
        prop[:state] = school.state
        prop[:active] = school.active?
        prop[:academic_year] = school.current_year&.academic_year
        prop[:district] = school.school_district_office?
        prop[:catholic_diocese] = school.catholic_diocese_office?
        prop[:state_id] = school.state_id&.number
        prop[:non_binary] = school.school_config.non_binary?
        prop[:edfi_system] = school.ed_fi_system
        prop[:employees] = school&.employees&.is_current(true)&.count || 0
        prop[:students] = school&.students&.current_status(school, :current)&.count || 0
        prop[:families] = school&.families&.current_status(:current)&.count || 0
        prop[:getting_started] = school.find_or_build_customer_wizard.getting_started?
        prop[:support_id] = school.support_id
        prop[:time_zone] = school.time_zone
        prop[:app_color] = school.find_or_build_school_branding.color.to_s.camelize(:lower)
        prop[:has_sites] = district_school_count ? district_school_count > 1 : false
        prop[:catholic_school] = school.catholic_school?

        prop[:phone] = school.phone
        prop[:phone_2] = school.phone_2
        prop[:fax_number] = school.fax_number
        prop[:contact_email] = school.contact_email
        prop[:admin_email] = school.admin_email
        prop[:admin_name] = school.admin_user&.full_name(:reverse)

        return prop unless school.school_district

        prop[:school_district_id] = school.school_district&.school_id
        prop[:school_district_name] = school.school_district&.name
        prop[:belongs_to_diocese] = district_school&.catholic_diocese_office?
      end
    end
end
