# typed: false
require "rails_helper"

RSpec.describe LogEntriesController, type: :controller do
  render_views
  let(:user) { create(:user, :admin) }

  context "when user is not signed in" do
    describe "GET #index" do
      it "redirects to sign in" do
        get :index, params: { filter: "example.com/test" }
        expect(response).to redirect_to(new_user_session_path)
      end
    end

    describe "GET #stats" do
      it "redirects to sign in" do
        get :stats, params: { seconds: 3600 }
        expect(response).to redirect_to(new_user_session_path)
      end
    end

    describe "GET #show" do
      it "redirects to sign in" do
        get :show, params: { id: 1 }
        expect(response).to redirect_to(new_user_session_path)
      end
    end
  end

  context "when user is signed in" do
    before { sign_in user }

    describe "GET #index" do
      it "renders the index template with filtered log entries" do
        get :index, params: { filter: "example.com/test" }
        expect(response).to be_successful
        expect(response).to render_template(:index)
      end

      it "shows existing log entries matching the filter" do
        log_entry =
          create(
            :http_log_entry,
            uri_host: "example.com",
            uri_path: "/test",
            uri_scheme: "https",
          )

        get :index, params: { filter: "example.com/test" }

        expect(response).to be_successful
        expect(assigns(:log_entries)).to include(log_entry)
      end
    end

    describe "GET #stats" do
      it "renders the stats template with time window data" do
        get :stats, params: { seconds: 3600 }
        expect(response).to be_successful
        expect(response).to render_template(:stats)
        expect(assigns(:time_window)).to eq 3600.seconds
      end
    end

    describe "GET #show" do
      let(:log_entry) { create(:http_log_entry) }

      it "renders the show template with the requested log entry" do
        get :show, params: { id: log_entry.id }
        expect(response).to be_successful
        expect(response).to render_template(:show)
        expect(assigns(:log_entry)).to eq(log_entry)
      end
    end
  end
end
