require 'rails_helper'

RSpec.describe Idv::HybridMobile::ChooseIdTypeController do
  let(:idv_vendor) { Idp::Constants::Vendors::MOCK }
  let(:user) { create(:user) }
  let(:passport_status) { nil }

  let!(:document_capture_session) do
    create(
      :document_capture_session,
      user:,
      requested_at: Time.zone.now,
      doc_auth_vendor: idv_vendor,
      passport_status:,
    )
  end

  let(:document_capture_session_uuid) { document_capture_session&.uuid }
  let(:session_uuid) { document_capture_session.uuid }

  before do
    stub_request(:get, IdentityConfig.store.dos_passport_composite_healthcheck_endpoint)
      .to_return({ status: 200, body: { status: 'UP' }.to_json })
    stub_analytics
    session[:doc_capture_user_id] = user&.id
    session[:document_capture_session_uuid] = document_capture_session_uuid

    allow(IdentityConfig.store).to receive(:doc_auth_vendor).and_return(idv_vendor)
    allow(IdentityConfig.store).to receive(:doc_auth_vendor_default).and_return(idv_vendor)
    allow(subject).to receive(:document_capture_session).and_return(document_capture_session)
  end

  describe 'before actions' do
    it 'includes correct before_actions' do
      expect(subject).to have_actions(
        :before,
        :check_valid_document_capture_session,
      )
    end
  end

  describe '#show' do
    context 'passport is available' do
      let(:analytics_name) { :idv_doc_auth_choose_id_type_visited }
      let(:analytics_args) do
        {
          step: 'hybrid_choose_id_type',
          analytics_id: 'Doc Auth',
          flow_path: 'hybrid',
        }
      end

      it 'renders the shared choose_id_type template' do
        get :show

        expect(response).to render_template 'idv/shared/choose_id_type'
      end

      it 'sends analytics_visited event' do
        get :show

        expect(@analytics).to have_logged_event(analytics_name, analytics_args)
      end
    end
  end

  describe '#update' do
    let(:chosen_id_type) { 'drivers_license' }
    let(:analytics_name) { :idv_doc_auth_choose_id_type_submitted }
    let(:idv_vendor) { Idp::Constants::Vendors::LEXIS_NEXIS }
    let(:analytics_args) do
      {
        success: true,
        step: 'hybrid_choose_id_type',
        analytics_id: 'Doc Auth',
        flow_path: 'hybrid',
        chosen_id_type: chosen_id_type,
      }
    end

    let(:params) do
      { doc_auth: { choose_id_type_preference: chosen_id_type } }
    end
    it 'sends analytics submitted event for id choice' do
      put :update, params: params

      expect(@analytics).to have_logged_event(analytics_name, analytics_args)
    end
    context 'user chooses drivers_license' do
      it 'maintains passport_status as allowed and redirects to correct vendor' do
        put :update, params: params
        expect(document_capture_session.passport_status).to eq('not_requested')
        expect(response).to redirect_to idv_hybrid_mobile_document_capture_url
      end
    end

    context 'user chooses passport' do
      let(:chosen_id_type) { 'passport' }
      let(:params) do
        { doc_auth: { choose_id_type_preference: chosen_id_type } }
      end

      it 'sets passport_status to requested and redirects to vendor that supports passport' do
        put :update, params: params
        expect(document_capture_session.passport_status).to eq('requested')
        expect(response).to redirect_to idv_hybrid_mobile_document_capture_url
      end
    end
  end
end
