class CourseVideosController < ApplicationController
  before_action :require_login
  before_action :validate_params, except: [:watch_histories]
  before_action :find_course, only: [:create]
  before_action :find_video, only: [:update, :watch_histories]
  before_action :teacher_allowed, except: [:watch_histories]

  def create
    title = params[:name].strip
    link = params[:link].strip
    course_second_category_id = params[:category_id] || 0
    @course.course_videos.create!(title: title, link: link, is_link: 1, user_id: current_user.id, course_second_category_id: course_second_category_id)
    render_ok
  end

  def update
    title = params[:name].strip
    link = params[:link].strip
    @video.update!(title: title, link: link)
    render_ok
  end

  def watch_histories
    return normal_status(403, "你没有权限操作") if !current_user.teacher_of_course?(@course)
    course_video = CourseVideo.find(@video.id)

    watch_course_videos = course_video.watch_course_videos.joins("
       JOIN watch_video_histories ON watch_video_histories.watch_course_video_id = watch_course_videos.id AND watch_video_histories.end_at IS NOT NULL
       left JOIN course_members ON course_members.user_id = watch_course_videos.user_id AND course_members.course_id = #{@course.id} AND role = 4
     ").group("watch_video_histories.watch_course_video_id").where("watch_course_videos.end_at IS NOT NULL")

    watch_course_videos = watch_course_videos.select("count(watch_course_videos.course_video_id) AS freq, watch_course_videos.id, watch_course_videos.user_id v_user_id, watch_course_videos.start_at, watch_course_videos.end_at, watch_course_videos.is_finished, watch_course_videos.total_duration")

    @watch_course_videos =
        @course.course_members.where(role: %i[STUDENT]).joins("LEFT JOIN (
          #{watch_course_videos.to_sql}) AS watch_course_videos ON course_members.user_id = watch_course_videos.v_user_id
    ").select("watch_course_videos.*, course_members.user_id, course_members.course_group_id")

    # 分班查询
    if params[:group_id].present?
      @watch_course_videos = @watch_course_videos.where("course_group_id = ?", params[:group_id])
    end

    # 观看状态
    case params[:status]
    when "finished"
      logger.info("finished: #{params[:status]}")
      @watch_course_videos = @watch_course_videos.where("watch_course_videos.is_finished = ?", true)
    when "unfinished"
      logger.info("finished: #{params[:status]}")
      @watch_course_videos = @watch_course_videos.where("watch_course_videos.is_finished = ?", false)
    when "unopened"
      logger.info("finished: #{params[:status]}")
      @watch_course_videos = @watch_course_videos.where("watch_course_videos.start_at is null")
    end

    @count = @watch_course_videos.count("user_id")

    if params[:order].present?
      key = params[:order].split("-")
      if ["freq", 'total_duration'].include?(key.first) && ["desc", "asc"].include?(key.last)
        @watch_course_videos = @watch_course_videos.order("#{key.first} #{key.last}")
      end
    end
    @watch_course_videos = paginate @watch_course_videos
  end

  private

  def validate_params
    tip_exception("视频名称不能为空") if params[:name].blank?
    tip_exception("链接地址不能为空") if params[:link].blank?
  end

  def find_video
    @video = CourseVideo.find params[:id]
    @course = @video.course
  end

end