class Employee::Legacy::Classrooms::Cafeteria::Orders::StudentsController < Employee::Controller
  include Employee::ClassroomScoped

  def index
    data = {
      date: date,
      students: students.map { |s| student_order_props(s) }
    }
    render_success :ok, json: data
  end

  def show
    render_success :ok, json: { date: date }.merge(student_order_props(student))
  end

  def update
    order = lunch_order(student)
    order.order_date = Date.current
    order.milk_order_date = date
    order.assign_attributes(order_params(params[:item]))

    if order.save
      render_success :ok, object: 'order', json: student_order_props(student)
    else
      render_error :unprocessable_entity, message: 'Something went wrong'
    end
  end

  def batch_update
    orders = []
    students.where(id: params[:params].pluck(:id)).each do |student|
      param = params[:params].find { |i| i[:id] == student.id }
      order = lunch_order(student)
      order.order_date = Date.current
      order.milk_order_date = date
      order.assign_attributes(order_params(param))
      orders << order
    end

    if transactional_save(orders)
      render_success :ok, message: 'Orders created.'
    else
      render_error :unprocessable_entity, errors: multi_errors(orders)
    end
  end

  private
    def lunch_order(student)
      student.student_lunch_orders.find_or_initialize_by(
        school_year: current_school_year,
        lunch_day: lunch_day
      )
    end

    def student_lunch_orders
      @student_lunch_orders ||= current_school.student_lunch_orders
        .joins(:lunch_day)
        .where(student_id: classroom.students.pluck(:id))
        .by_lunch_date(date)
        .index_by(&:student_id)
    end

    def student_milk_orders
      @student_milk_orders ||= current_school.student_milk_orders
        .where(student_id: classroom.students.pluck(:id))
        .by_order_date(date)
        .index_by(&:student_id)
    end

    def student
      @student ||= classroom.students.find_by(id: params[:id])
    end

    def lunch_day
      @lunch_day ||= current_school_year.lunch_days.find_by(date: date)
    end

    def date
      params[:date] || Date.current
    end

    def order_params(param)
      param.permit(:quantity, :milk_quantity, :meal_id).merge(classroom: classroom)
    end

    def student_order_props(student)
      lunch_order = student_lunch_orders[student.id]
      milk_order = student_milk_orders[student.id]

      {}.tap do |props|
        props[:id] = student.id
        props[:first_name] = student.first_name
        props[:last_name] = student.last_name
        props[:full_name] = student.full_name(:reverse)
        props[:meal_id] = lunch_order&.meal_id
        props[:meal_name] = lunch_order&.meal&.name
        props[:quantity] = lunch_order&.quantity
        props[:milk_quantity] = milk_order&.quantity
        props[:lunch_charged] = lunch_order&.charge_date?
        props[:milk_charged] = milk_order&.charge_date?
      end
    end
end
