class Employee::Legacy::Classrooms::AssignmentsController < Employee::Controller
  include Employee::ClassroomScoped

  def index
    render_success :ok, json: assignments.map { |a| assignment_props(a) }
  end

  def show
    data = assignment_props(assignment)
    data[:documents] =  documents.map { |d| document_props(d) }
    render_success :ok, json: data
  end

  def create
    assignment = assignments.new
    if assignment.update(assignment_params)
      render_success :ok, json: assignment_props(assignment)
    else
      render_error :unprocessable_entity, errors: assignment
    end
  end

  def update
    if assignment.update(assignment_params)
      render_success :ok, json: assignment_props(assignment)
    else
      render_error :unprocessable_entity, errors: assignment
    end
  end

  def post
    service = JSON.parse(post_service(assignment).call)
    if service['success'] == true
      render_success :ok
    else
      render_error :unprocessable_entity
    end
  end

  private
    def assignments
      classroom.class_assignments
    end

    def assignment
      @assignment ||= assignments.find_by(id: params[:id])
    end

    def class_subject
      classroom.class_subjects.find_by(id: params[:subject_id])
    end

    def class_group
      classroom.class_assignment_groups.find_by(id: params[:group_id])
    end

    def grade_category
      classroom.class_grade_categories.find_by(id: params[:category_id])
    end

    def documents
      assignment.documents
    end

    def document_props(document)
      {
        id: document.id,
        name: document.name,
        file: document.extension
      }
    end

    def post_service(assignment)
      Internal::HumanResources::GradePostService.new(assignment, current_user)
    end

    def assignment_params
      params.permit(:name, :quarter, :due_date, :possible, :status, :dropbox, :semester)
        .merge(assignment_associations)
    end

    def assignment_associations
      {}.tap do |options|
        options[:class_subject] = class_subject if params[:subject_id].present?
        options[:class_assignment_group] = class_group if params[:group_id].present?
        options[:class_grade_category] = grade_category if params[:category_id].present?
        options[:weight] = 1 if assignment.nil?
      end
    end

    def assignment_props(assignment)
      {}.tap do |props|
        props[:id] = assignment.id
        props[:class_id] = assignment.class_id
        props[:name] = assignment.name
        props[:quarter] = assignment.quarter
        props[:semester] = assignment.semester
        props[:due_date] = assignment.due_date
        props[:subject_id] = assignment.class_subject_id
        props[:subject] = assignment.class_subject&.name
        props[:possible] = assignment.possible
        props[:category_id] = assignment.class_grade_category_id
        props[:category] = assignment.class_grade_category&.name
        props[:group_id] = assignment.class_assignment_group_id
        props[:group] = assignment.class_assignment_group&.name
        props[:status] = assignment.status
        props[:dropbox] = assignment.dropbox?
      end
    end
end
