(ns auto-ap.ssr.core
  (:require [auto-ap.permissions :refer [wrap-must]]
            [auto-ap.routes.ezcater-xls :as ezcater-xls]
            [auto-ap.routes.utils
    :refer [wrap-admin wrap-client-redirect-unauthenticated wrap-secure]]
            [auto-ap.ssr.account :as account]
            [auto-ap.ssr.admin :as admin]
            [auto-ap.ssr.not-found :as not-found]
            [auto-ap.ssr.admin.accounts :as admin-accounts]
            [auto-ap.ssr.admin.background-jobs :as admin-jobs]
            [auto-ap.ssr.admin.clients :as admin-clients]
            [auto-ap.ssr.admin.excel-invoice :as admin-excel-invoices]
            [auto-ap.ssr.admin.history :as history]
            [auto-ap.ssr.admin.import-batch :as import-batch]
            [auto-ap.ssr.pos.sales-summaries :as pos-sales-summaries]
            [auto-ap.ssr.admin.transaction-rules :as admin-rules]
            [auto-ap.ssr.admin.vendors :as admin-vendors]
            [auto-ap.ssr.auth :as auth]
            [auto-ap.ssr.dashboard :as dashboard]
            [auto-ap.ssr.company :as company]
            [auto-ap.ssr.company-dropdown :as company-dropdown]
            [auto-ap.ssr.company.company-1099 :as company-1099]
            [auto-ap.ssr.company.plaid :as company-plaid]
            [auto-ap.ssr.company.reports :as company-reports]
            [auto-ap.ssr.company.yodlee :as company-yodlee]
            [auto-ap.ssr.indicators :as indicators]
            [auto-ap.ssr.invoice.glimpse :as invoice-glimpse]
            [auto-ap.ssr.invoices :as invoice]
            [auto-ap.ssr.ledger :as l]
            [auto-ap.ssr.outgoing-invoice.new :as oin]
            [auto-ap.ssr.payments :as payments]
            [auto-ap.ssr.pos.cash-drawer-shifts :as pos-cash-drawer-shifts]
            [auto-ap.ssr.pos.expected-deposits :as pos-expected-deposits]
            [auto-ap.ssr.pos.refunds :as pos-refunds]
            [auto-ap.ssr.pos.sales-orders :as pos-sales]
            [auto-ap.ssr.pos.tenders :as pos-tenders]
            [auto-ap.ssr.search :as search]
            [auto-ap.ssr.transaction.insights :as insights]
            [auto-ap.ssr.users :as users]
            [auto-ap.ssr.transaction :as transaction]
            [auto-ap.ssr.vendor :as vendors]
            [ring.middleware.json :refer [wrap-json-response]]))

;; from auto-ap.ssr-routes, because they're shared


(def key->handler
  (-> {:logout                                         auth/logout
       :impersonate                                    (wrap-client-redirect-unauthenticated (wrap-secure (wrap-admin auth/impersonate)))
       :admin-history                                  (wrap-client-redirect-unauthenticated (wrap-secure (wrap-admin history/page)))
       :admin-history-search                           (wrap-client-redirect-unauthenticated (wrap-secure (wrap-admin history/page)))
       :admin-history-inspect                          (wrap-client-redirect-unauthenticated (wrap-secure (wrap-admin history/inspect)))
       :active-client                                  (wrap-client-redirect-unauthenticated (wrap-secure (wrap-secure company-dropdown/active-client)))
       :company-dropdown-search-results
       (wrap-client-redirect-unauthenticated (wrap-secure (wrap-json-response company-dropdown/dropdown-search-results {})))
       :company-search
       (wrap-client-redirect-unauthenticated (wrap-secure company/search))
       :bank-account-search (wrap-client-redirect-unauthenticated (wrap-secure company/bank-account-search))
       :account-search (wrap-client-redirect-unauthenticated (wrap-secure account/account-search))
       :bank-account-typeahead (wrap-client-redirect-unauthenticated (wrap-secure company/bank-account-typeahead))
       :not-found not-found/page

       :company                                        (wrap-client-redirect-unauthenticated (wrap-secure company/page))
       :company-update-signature                       (wrap-client-redirect-unauthenticated (wrap-secure company/upload-signature-data))
       :company-upload-signature                       (wrap-client-redirect-unauthenticated (wrap-secure company/upload-signature-file))
       :company-yodlee-provider-account-refresh        (wrap-client-redirect-unauthenticated (wrap-admin company-yodlee/refresh-provider-account))
       :company-yodlee-provider-account-reauthenticate (wrap-client-redirect-unauthenticated (wrap-secure company-yodlee/reauthenticate))
       :invoice-glimpse                                (wrap-client-redirect-unauthenticated (wrap-must (wrap-secure invoice-glimpse/page) {:activity :import :subject :invoice}))
       :invoice-glimpse-upload                         (wrap-client-redirect-unauthenticated (wrap-must (wrap-secure invoice-glimpse/upload) {:activity :import :subject :invoice}))
       :invoice-glimpse-textract-invoice               (wrap-client-redirect-unauthenticated (wrap-must (wrap-secure invoice-glimpse/textract-invoice) {:activity :import :subject :invoice}))
       :invoice-glimpse-create-invoice                 (wrap-client-redirect-unauthenticated (wrap-must (wrap-secure invoice-glimpse/create-invoice) {:activity :import :subject :invoice}))
       :invoice-glimpse-update-textract-invoice        (wrap-client-redirect-unauthenticated (wrap-must (wrap-secure invoice-glimpse/update-textract-invoice) {:activity :import :subject :invoice}))
       :vendor-search (wrap-client-redirect-unauthenticated (wrap-secure vendors/search))
       :transaction-insights                           (wrap-client-redirect-unauthenticated (wrap-admin insights/page))
       :transaction-insight-table                      (wrap-client-redirect-unauthenticated (wrap-admin insights/insight-table))
       :transaction-insight-rows                       (wrap-client-redirect-unauthenticated (wrap-admin insights/transaction-rows))
       :transaction-insight-code                       (wrap-client-redirect-unauthenticated (wrap-admin insights/code))
       :transaction-insight-disapprove                 (wrap-client-redirect-unauthenticated (wrap-admin insights/disapprove))
       :transaction-insight-explain                    (wrap-client-redirect-unauthenticated (wrap-admin insights/explain))
       :admin-ezcater-xls                              (wrap-client-redirect-unauthenticated (wrap-admin ezcater-xls/page))
       :search                                         (wrap-client-redirect-unauthenticated (wrap-secure search/dialog-contents))}
      (into company-plaid/key->handler)
      (into company-reports/key->handler)
      (into company-yodlee/key->handler)
      (into company-1099/key->handler)
      (into invoice/key->handler)
      (into import-batch/key->handler)
       (into pos-sales/key->handler)
       (into pos-expected-deposits/key->handler)
       (into pos-tenders/key->handler)
       (into pos-cash-drawer-shifts/key->handler)
       (into pos-refunds/key->handler)
       (into pos-sales-summaries/key->handler)
       (into users/key->handler)
       (into admin-accounts/key->handler)
       (into admin-excel-invoices/key->handler)
       (into admin/key->handler)
       (into admin-jobs/key->handler)
      (into admin-vendors/key->handler)
      (into admin-clients/key->handler)
      (into admin-rules/key->handler)
      (into transaction/key->handler)
      (into dashboard/key->handler)
      (into indicators/key->handler)
      (into payments/key->handler)
      (into oin/route->handler)
      (into l/key->handler)))

