# frozen_string_literal: true

# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
# Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE
require 'rails_helper'

RSpec.describe Api::TransactionsController do
	let(:nonprofit) { transaction_for_donation.nonprofit }
	let(:user) { create(:user) }
	let(:supporter) { transaction_for_donation.supporter }

	let(:transaction_for_donation) do
		create(:transaction_for_donation)
	end

	before do
		transaction_for_donation
	end

	def index_base_path(nonprofit_id)
		"/api/nonprofits/#{nonprofit_id}/transactions"
	end

	def index_base_url(nonprofit_id, _event_id)
		"http://www.example.com#{index_base_path(nonprofit_id)}"
	end

	describe 'GET /:id' do
		let(:transaction) { transaction_for_donation }

		def base_path(nonprofit_id, transaction_id)
			index_base_path(nonprofit_id) + "/#{transaction_id}"
		end

		def base_url(nonprofit_id, transaction_id)
			"http://www.example.com#{base_path(nonprofit_id, transaction_id)}"
		end

		context 'with nonprofit user' do
			subject do
				response.parsed_body
			end

			before do
				user.roles.create(name: 'nonprofit_associate', host: nonprofit)
				sign_in user
				get base_path(
					nonprofit.id,
					transaction.id
				)
			end

			it {
				expect(response).to have_http_status(:success)
			}

			include_context 'with json results for transaction_for_donation'
		end

		context 'with no user' do
			it 'returns unauthorized' do
				get base_path(
					nonprofit.id,
					transaction.id
				)
				expect(response).to have_http_status(:unauthorized)
			end
		end
	end

	# describe 'GET /:id/subtransaction' do
	# 	def subtransaction_path(nonprofit_id, transaction_id)
	# 		"/api/nonprofits/#{nonprofit_id}/transactions/#{transaction_id}/subtransaction"
	# 	end

	# 	def subtransaction_url(nonprofit_id, transaction_id)
	# 		"http://www.example.com#{subtransaction_path(nonprofit_id, transaction_id)}"
	# 	end

	# 	def payment_path(nonprofit_id, transaction_id, payment_id)
	# 		"#{subtransaction_path(nonprofit_id, transaction_id)}/payments/#{payment_id}"
	# 	end

	# 	def payment_url(nonprofit_id, transaction_id, payment_id)
	# 		"http://www.example.com#{payment_path(nonprofit_id, transaction_id, payment_id)}"
	# 	end

	# 	let(:transaction) { transaction_for_donation }
	# 	let(:subtransaction) { transaction.subtransaction }
	# 	let(:supporter) { subtransaction.supporter }

	# 	let(:nonprofit) { subtransaction.nonprofit }

	# 	context 'with nonprofit user' do
	# 		subject(:json) do
	# 			response.parsed_body
	# 		end

	# 		before do
	# 			user.roles.create(name: 'nonprofit_associate', host: nonprofit)
	# 			sign_in user
	# 			get subtransaction_path(
	# 				nonprofit.id,
	# 				transaction.id
	# 			)
	# 		end

	# 		it {
	# 			expect(response).to have_http_status(:success)
	# 		}

	# 		include_context 'with json results for subtransaction on transaction_for_donation'
	# 	end

	# 	context 'with no user' do
	# 		it 'returns unauthorized' do
	# 			get subtransaction_path(
	# 				nonprofit.id,
	# 				transaction.id
	# 			)
	# 			expect(response).to have_http_status(:unauthorized)
	# 		end
	# 	end
	# end

	describe 'GET /' do
		context 'with nonprofit user' do
			subject(:json) do
				response.parsed_body
			end

			before do
				user.roles.create(name: 'nonprofit_associate', host: nonprofit)
				sign_in user
				get index_base_path(nonprofit.id)
			end

			it {
				expect(response).to have_http_status(:success)
			}

			it { expect(json['data'].count).to eq 1 }

			describe 'for transaction_for_donation' do
				subject(:first) do
					json['data'].first
				end

				def base_path(nonprofit_id, transaction_id)
					index_base_path(nonprofit_id) + "/#{transaction_id}"
				end

				let(:transaction) { transaction_for_donation }

				def base_url(nonprofit_id, transaction_id)
					"http://www.example.com#{base_path(nonprofit_id, transaction_id)}"
				end
				include_context 'with json results for transaction_for_donation'
			end

			it { is_expected.to include('first_page' => true) }
			it { is_expected.to include('last_page' =>  true) }
			it { is_expected.to include('current_page' => 1) }
			it { is_expected.to include('requested_size' => 25) }
			it { is_expected.to include('total_count' => 1) }
		end

		context 'with no user' do
			it 'returns unauthorized' do
				get index_base_path(nonprofit.id)
				expect(response).to have_http_status(:unauthorized)
			end
		end
	end
end
