module Admin
  class SessionsController < Admin::BaseController
    def index
      @sessions = StreamSession.includes(:match, :user).order(created_at: :desc).limit(50)
    end

    def show
      @session = StreamSession.find(params[:id])
      @events = @session.stream_events.recent.limit(50)
    end

    def stop
      @session = StreamSession.find(params[:id])
      if @session.terminal?
        redirect_to admin_session_path(@session), alert: "Sessione già terminata (#{@session.status})."
        return
      end

      Sessions::Stop.new(@session).call
      redirect_to admin_session_path(@session), notice: "Sessione terminata."
    rescue StandardError => e
      Rails.logger.error("[admin] stop session #{@session.id}: #{e.class} #{e.message}")
      redirect_to admin_session_path(@session), alert: "Errore durante la chiusura: #{e.message}"
    end
  end
end
