package module_server

import (
	capital_handlers "cf-capital/module_server/handlers"
	//capital_cron "cf-capital/module_server/handlers/commands"
	JWT "cf-capital/module_server/handlers/jwt"
	capital_handlers_lendbox "cf-capital/module_server/handlers/lendbox"
	"net/http"

	"github.com/gorilla/mux"
)

func AddModuleRoutes(serverRouter *mux.Router) {

	capitalRouterWithoutAuth := serverRouter.PathPrefix("/v0").Subrouter()

	//Credit Fair Capital Golang - IFA & NCD
	capitalRouterWithoutAuth.Methods(http.MethodPost).Path("/{service}/generate-otp").HandlerFunc(capital_handlers.GenerateOTP())
	capitalRouterWithoutAuth.Methods(http.MethodPost).Path("/{service}/verify-password").HandlerFunc(capital_handlers.VerifyPassword())
	capitalRouterWithoutAuth.Methods(http.MethodPost).Path("/{service}/verify-otp").HandlerFunc(capital_handlers.VerifyOTP())
	capitalRouterWithoutAuth.Methods(http.MethodGet).Path("/{api}/get-frequently-asked-questions").HandlerFunc(capital_handlers.GetFrequentlyAskedQuestions())



	//Microsoft
	capitalRouterWithoutAuth.Methods(http.MethodGet).Path("/{api}/m-login").HandlerFunc(capital_handlers.MicroSoftLogin())
	capitalRouterWithoutAuth.Methods(http.MethodGet).Path("/{api}/callback").HandlerFunc(capital_handlers.MicroSoftCallback())

	//Cron
	//capitalRouterWithoutAuth.Methods(http.MethodGet).Path("/cron-service/daily").HandlerFunc(capital_cron.DailyCron())

	//Credit Fair Capital Investment
	capitalRouterWithoutAuth.Methods(http.MethodPost).Path("/calculation-flexi").HandlerFunc(capital_handlers.Investment())
	capitalRouterWithoutAuth.Methods(http.MethodPost).Path("/calculation-flexi-v1").HandlerFunc(capital_handlers.InvestmentV1())

	//capitalRouterWithoutAuth.Methods(http.MethodGet).Path("/{service}/invest-webhook").HandlerFunc(capital_handlers.InvestWebhook())

	capitalRouterWithoutAuth.Methods(http.MethodPost).Path("/{service}/payment-confirmation").HandlerFunc(capital_handlers.InvestmentWebhook())
	capitalRouterWithoutAuth.Methods(http.MethodPost).Path("/{service}/payment-update").HandlerFunc(capital_handlers.InvestmentWebhookUpdate())
	// capitalRouterWithoutAuth.Methods(http.MethodPost).Path("/{service}/daily").HandlerFunc(capital_handlers.Daily())
	capitalRouterWithoutAuth.Methods(http.MethodPost).Path("/{service}/forgot-password").HandlerFunc(capital_handlers.ForgotPassword())
	capitalRouterWithoutAuth.Methods(http.MethodPost).Path("/{service}/submit-password").HandlerFunc(capital_handlers.SubmitPassword())
	capitalRouterWithoutAuth.Methods(http.MethodGet).Path("/{service}/abort-transactions").HandlerFunc(capital_handlers.AbortTransactions())


	capitalRouter := serverRouter.PathPrefix("/v0").Subrouter()
	capitalRouter.Use(Middleware)
	capitalRouter.Methods(http.MethodPost).Path("/{api}/validate-pan").HandlerFunc(capital_handlers.ValidatePAN())
	capitalRouter.Methods(http.MethodPost).Path("/{api}/create-user").HandlerFunc(capital_handlers.CreateUser())
	capitalRouter.Methods(http.MethodPost).Path("/{api}/esign-agreement").HandlerFunc(capital_handlers.EsignAgreement())
	capitalRouter.Methods(http.MethodPost).Path("/{api}/upload-document").HandlerFunc(capital_handlers.UploadDocument())
	capitalRouter.Methods(http.MethodGet).Path("/{api}/get-pincode-details").HandlerFunc(capital_handlers.GetPincodeDetails())
	capitalRouter.Methods(http.MethodPost).Path("/{api}/bank").HandlerFunc(capital_handlers.AddBank())
	capitalRouter.Methods(http.MethodPost).Path("/{api}/update-address").HandlerFunc(capital_handlers.UpdateAddress())
	capitalRouter.Methods(http.MethodGet).Path("/{api}/get-user").HandlerFunc(capital_handlers.GetUser())
	capitalRouter.Methods(http.MethodGet).Path("/{api}/get-investor-details").HandlerFunc(capital_handlers.GetInvestorDetails())
	capitalRouter.Methods(http.MethodGet).Path("/{api}/get-agreement-link").HandlerFunc(capital_handlers.GetAgreementLink())
	capitalRouter.Methods(http.MethodPost).Path("/{api}/add-nominee").HandlerFunc(capital_handlers.AddNominee())
	capitalRouter.Methods(http.MethodGet).Path("/{api}/get-nominee").HandlerFunc(capital_handlers.GetNominee())
	capitalRouter.Methods(http.MethodGet).Path("/{api}/get-bank").HandlerFunc(capital_handlers.GetBank())

	capitalRouter.Methods(http.MethodGet).Path("/{api}/dashboard").HandlerFunc(capital_handlers.GetDashboard())
	capitalRouter.Methods(http.MethodPost).Path("/{api}/passbook").HandlerFunc(capital_handlers.GetPassbook())
	capitalRouter.Methods(http.MethodPost).Path("/{api}/investment").HandlerFunc(capital_handlers.Invest())
	capitalRouter.Methods(http.MethodPost).Path("/{api}/withdrawal").HandlerFunc(capital_handlers.Withdrawal())
	capitalRouter.Methods(http.MethodPost).Path("/{api}/getpdf").HandlerFunc(capital_handlers.GetPassbookPDF())

	capitalRouter.Methods(http.MethodGet).Path("/{api}/get-accounts").HandlerFunc(capital_handlers.GetAccounts())
	capitalRouter.Methods(http.MethodGet).Path("/{api}/get-schemes").HandlerFunc(capital_handlers.GetSchemes())
	//capitalRouter.Methods(http.MethodGet).Path("/{api}/sendmails").HandlerFunc(capital_handlers.SendMaturityMails())

	capitalRouter.Methods(http.MethodGet).Path("/{api}/investment-list").HandlerFunc(capital_handlers.GetInvestmentList())
	capitalRouter.Methods(http.MethodGet).Path("/{api}/withdrawal-list").HandlerFunc(capital_handlers.GetWithdrawalList())

	capitalRouter.Methods(http.MethodGet).Path("/{api}/con-status").HandlerFunc(capital_handlers.ConnectionCheck())

	//capitalRouter.Methods(http.MethodGet).Path("/{api}/agreement/{id}").HandlerFunc(capital_handlers.GetAgreement())

	//Credit Fair Capital Golang - NCD
	capitalRouter.Methods(http.MethodPost).Path("/{api}/make-transaction").HandlerFunc(capital_handlers.MakeTransaction())
	capitalRouter.Methods(http.MethodPost).Path("/{api}/invest").HandlerFunc(capital_handlers.Invest())

	//Credit Fair Capital Golang - Lendbox
	capitalRouter.Methods(http.MethodPost).Path("/{api}/investor-signup").HandlerFunc(capital_handlers_lendbox.InvestorSignUp())
	capitalRouter.Methods(http.MethodPost).Path("/{api}/upload-docs").HandlerFunc(capital_handlers_lendbox.UploadDocs())
	capitalRouter.Methods(http.MethodPost).Path("/{api}/save-inv-bank-details").HandlerFunc(capital_handlers_lendbox.InvestorBankDetails())

	//Test
	capitalRouter.Methods(http.MethodPost).Path("/{api}/delete-user").HandlerFunc(capital_handlers.DeleteUser())

	//Payment confirmation
	capitalRouter.Methods(http.MethodPost).Path("/{api}/investment-confirmation").HandlerFunc(capital_handlers.PaymentConfirmation())
	capitalRouter.Methods(http.MethodPost).Path("/{api}/withdrawal-confirmation").HandlerFunc(capital_handlers.WithdrawalConfirmation())

	//Test APIs
	capitalRouter.Methods(http.MethodPost).Path("/{api}/lendbox-create-user").HandlerFunc(capital_handlers.CreateLendboxUser())
	capitalRouter.Methods(http.MethodPost).Path("/{api}/lendbox-create-user-bank").HandlerFunc(capital_handlers.CreateLendboxUserBankDetails())
	capitalRouter.Methods(http.MethodPost).Path("/{api}/lendbox-create-wallet-order").HandlerFunc(capital_handlers.CreateLendboxWalletOrderDetails())
	//IFA APIs
	capitalRouter.Methods(http.MethodGet).Path("/{api}/get-recent-transaction").HandlerFunc(capital_handlers.GetRecentTransaction())
	capitalRouter.Methods(http.MethodGet).Path("/{api}/get-investors").HandlerFunc(capital_handlers.GetInvestors())
	capitalRouter.Methods(http.MethodGet).Path("/{api}/get-investor-portfolio").HandlerFunc(capital_handlers.GetPortfolio())

	//Investor Borrower Mapping
	capitalRouter.Methods(http.MethodGet).Path("/{api}/get-investor-borrower-mapping").HandlerFunc(capital_handlers.GetInvestorBorrowerMapping())
}

func Middleware(h http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

		token := r.Header.Get("Authorization")
		res, err := JWT.Verify(token)

		if err != nil {
			w.WriteHeader(http.StatusUnauthorized)
			w.Header().Set("Content-Type", "application/json")
			w.Write(res)
			return
		}
		q := r.URL.Query()
		//log.Print(res)
		q.Add("authUser", string(res))
		r.URL.RawQuery = q.Encode()

		h.ServeHTTP(w, r)

	})
}
