class BookmarksController < ApplicationController
  before_action :set_video, except: [:destroy, :edit, :update]
  before_action :set_bookmark, only: [:destroy, :edit, :update]

  def new
    @bookmark = @video.bookmarks.build(bookmark_params)
  end

  def create
    @bookmark = @video.bookmarks.build(bookmark_params)

    @bookmark.save
    @bookmarks = @video.bookmarks
  end

  def edit
  end

  def update
    @video = @bookmark.video

    @bookmark.update(bookmark_params)
    @bookmarks = @video.bookmarks
  end

  def destroy
    @video = @bookmark.video

    @bookmark.destroy
    @bookmarks = @video.bookmarks
  end

  private

  def bookmark_params
    params.require(:bookmark).permit(:notes, :time_elapsed, :category)
  end

  def bookmarks
    if @video
      policy_scope(@video.bookmarks)
    else
      policy_scope(Bookmark)
    end
  end
  
  def videos
    policy_scope(Video)
  end

  def set_bookmark
    @bookmark = authorize bookmarks.find(params[:id])
  end

  def set_video
    @video = authorize videos.find(params[:video_id])
  end
end
