import { ServerRoute } from '@hapi/hapi'
import { PaymentHandler } from '../handlers/payment.handler'
import { options } from "../validators/global.validator.schema"
import { Common } from "../../utils/common"
import {
    subscriptionRequest,
    subscriptionListRequest,
    transactionListRequest,
    userTransactionListRequest,
    subscriptionIdentifier,
    subscriptionListResponse,
    transactionListResponse,
    subscriptionResponse,
    paymentIntentResponse,
    userSubscriptionListRequest,
    subscriptionUpdateRequest,
    statsRequest,
    merchantOrderStatusRequest,
    verifyAppPurchaseRequest,
    verifyPaymentRequest
} from "../validators/payment.validator"
const paymentHandler = new PaymentHandler();
import { I18N } from '../../utils/i18n';
import { error400, error401, error403, error404, error500, confirmationOnly } from "../validators/global.validator.schema"
const routes: ServerRoute[] = [
    {
        method: 'POST',
        path: '/payment/buy-subscription',
        handler: paymentHandler.buySubscription,
        options: {
            tags: ['api', 'Payment - Subscription'],
            description: I18N.t('BUY_SUBSCRIPTION_DESCRIPTION'),
            notes: I18N.t('BUY_SUBSCRIPTION_NOTE'),
            auth: { strategies: ['jwt'], scope: ['admin', 'managePlans', 'user'] },
            plugins: { 'hapi-swagger': { order: 1 } },
            validate: {
                headers: Common.routeHeaders("authorized"),
                options: options,
                failAction: Common.failover,
                payload: subscriptionRequest,
            },
            response: {
                status: {
                    201: paymentIntentResponse,
                    400: error400,
                    401: error401,
                    403: error403,
                    404: error404,
                    500: error500
                }
            }
        },
    },
    {
        method: 'PATCH',
        path: '/payment/update-subscription',
        handler: paymentHandler.updateSubscription,
        options: {
            tags: ['api', 'Payment - Subscription'],
            description: I18N.t('BUY_SUBSCRIPTION_DESCRIPTION'),
            notes: I18N.t('BUY_SUBSCRIPTION_NOTE'),
            auth: { strategies: ['jwt'], scope: ['admin', 'managePlans', 'user'] },
            plugins: { 'hapi-swagger': { order: 1 } },
            validate: {
                headers: Common.routeHeaders("authorized"),
                options: options,
                failAction: Common.failover,
                payload: subscriptionUpdateRequest
            },
            response: {
                status: {
                    201: subscriptionResponse,
                    400: error400,
                    401: error401,
                    403: error403,
                    404: error404,
                    500: error500
                }
            }
        },
    },
    {
        method: 'DELETE',
        path: '/payment/cancel-subscription/{id}',
        handler: paymentHandler.cancelSubscription,
        options: {
            tags: ['api', 'Payment - Subscription'],
            description: I18N.t('CANCEL_SUBSCRIPTION_DESCRIPTION'),
            notes: I18N.t('CANCEL_SUBSCRIPTION_NOTE'),
            auth: { strategies: ['jwt'], scope: ['admin', 'managePlans', 'user'] },
            plugins: { 'hapi-swagger': { order: 2 } },
            validate: {
                headers: Common.routeHeaders("authorized"),
                options: options,
                failAction: Common.failover,
                params: subscriptionIdentifier,
            },
            response: {
                status: {
                    // 201: planResponse,
                    400: error400,
                    401: error401,
                    403: error403,
                    404: error404,
                    500: error500
                }
            }
        },
    },
    {
        method: 'GET',
        path: '/payment/subscription/list',
        handler: paymentHandler.listUserSubscriptions,
        options: {
            tags: ['api', 'Payment - Subscription'],
            description: I18N.t('LIST_SUBSCRIPTION_ADMIN_DESCRIPTION'),
            notes: I18N.t('LIST_SUBSCRIPTION_ADMIN_NOTE'),
            plugins: { 'hapi-swagger': { order: 3 } },
            auth: { strategies: ['jwt'], scope: ['admin', 'managePlans'] },
            validate: {
                headers: Common.routeHeaders("authorized"),
                options: options,
                failAction: Common.failover,
                query: userSubscriptionListRequest
            },
            response: {
                status: {
                    200: subscriptionListResponse,
                    400: error400,
                    401: error401,
                    403: error403,
                    404: error404,
                    500: error500
                }
            }
        },
    },
    {
        method: 'GET',
        path: '/payment/status/{merchantOrderId}',
        handler: paymentHandler.fetchStatus,
        options: {
            tags: ['api', 'Payment - Subscription'],
            description: I18N.t('LIST_SUBSCRIPTION_ADMIN_DESCRIPTION'),
            notes: I18N.t('LIST_SUBSCRIPTION_ADMIN_NOTE'),
            plugins: { 'hapi-swagger': { order: 3 } },
            auth: false,
            validate: {
                headers: Common.routeHeaders(),
                options: options,
                failAction: Common.failover,
                params: merchantOrderStatusRequest
            },
            response: {
                status: {
                    // 200: subscriptionListResponse,
                    400: error400,
                    401: error401,
                    403: error403,
                    404: error404,
                    500: error500
                }
            }
        },
    },
    {
        method: 'GET',
        path: '/payment/subscriptions',
        handler: paymentHandler.listSubscriptions,
        options: {
            tags: ['api', 'Payment - Subscription'],
            description: I18N.t('LIST_SUBSCRIPTION_ADMIN_DESCRIPTION'),
            notes: I18N.t('LIST_SUBSCRIPTION_ADMIN_NOTE'),
            plugins: { 'hapi-swagger': { order: 3 } },
            auth: { strategies: ['jwt'], scope: ['admin', 'managePlans'] },
            validate: {
                headers: Common.routeHeaders("authorized"),
                options: options,
                failAction: Common.failover,
                query: subscriptionListRequest
            },
            response: {
                status: {
                    200: subscriptionListResponse,
                    400: error400,
                    401: error401,
                    403: error403,
                    404: error404,
                    500: error500
                }
            }
        },
    },
    {
        method: 'GET',
        path: '/payment/subscription/{id}',
        handler: paymentHandler.getSubscriptionById,
        options: {
            tags: ['api', 'Payment - Subscription'],
            description: I18N.t('GET_SUBSCRIPTION_BY_ID_DESCRIPTION'),
            notes: I18N.t('GET_SUBSCRIPTION_BY_ID_NOTE'),
            plugins: { 'hapi-swagger': { order: 4 } },
            auth: { strategies: ['jwt'], scope: ['admin', 'managePlans'] },
            validate: {
                headers: Common.routeHeaders("authorized"),
                options: options,
                failAction: Common.failover,
                params: subscriptionIdentifier
            },
            response: {
                status: {
                    200: subscriptionResponse,
                    400: error400,
                    401: error401,
                    403: error403,
                    404: error404,
                    500: error500
                }
            }
        },
    },

    {
        method: 'POST',
        path: '/payment/initate-payment',
        handler: paymentHandler.initiatePayment,
        options: {
            tags: ['api', 'Payment - Order'],
            description: I18N.t('BUY_SUBSCRIPTION_DESCRIPTION'),
            notes: I18N.t('BUY_SUBSCRIPTION_NOTE'),
            auth: { strategies: ['jwt'], scope: ['admin', 'managePlans', 'user'] },
            plugins: { 'hapi-swagger': { order: 1 } },
            validate: {
                headers: Common.routeHeaders("authorized"),
                options: options,
                failAction: Common.failover,
                payload: subscriptionRequest,
            },
            response: {
                status: {
                    201: paymentIntentResponse,
                    400: error400,
                    401: error401,
                    403: error403,
                    404: error404,
                    500: error500
                }
            }
        },
    },
    {
        method: 'POST',
        path: '/payment/upgrade-payment',
        handler: paymentHandler.updatePayment,
        options: {
            tags: ['api', 'Payment - Subscription'],
            description: I18N.t('BUY_SUBSCRIPTION_DESCRIPTION'),
            notes: I18N.t('BUY_SUBSCRIPTION_NOTE'),
            auth: { strategies: ['jwt'], scope: ['admin', 'managePlans', 'user'] },
            plugins: { 'hapi-swagger': { order: 1 } },
            validate: {
                headers: Common.routeHeaders("authorized"),
                options: options,
                failAction: Common.failover,
                payload: subscriptionRequest,
            },
            response: {
                status: {
                    201: paymentIntentResponse,
                    400: error400,
                    401: error401,
                    403: error403,
                    404: error404,
                    500: error500
                }
            }
        },
    },
    {
        method: 'GET',
        path: '/payment/tranasction/list',
        handler: paymentHandler.listUserTranasctions,
        options: {
            tags: ['api', 'Payment - Transaction'],
            description: I18N.t('LIST_TRANSACTION_ADMIN_DESCRIPTION'),
            notes: I18N.t('LIST_TRANSACTION_ADMIN_NOTE'),
            plugins: { 'hapi-swagger': { order: 1 } },
            auth: { strategies: ['jwt'], scope: ['admin', 'managePlans', 'user'] },
            validate: {
                headers: Common.routeHeaders("authorized"),
                options: options,
                failAction: Common.failover,
                query: userTransactionListRequest
            },
            response: {
                status: {
                    200: transactionListResponse,
                    400: error400,
                    401: error401,
                    403: error403,
                    404: error404,
                    500: error500
                }
            }
        },
    },
    {
        method: 'GET',
        path: '/payment/tranasctions',
        handler: paymentHandler.listTranasctions,
        options: {
            tags: ['api', 'Payment - Transaction'],
            description: I18N.t('LIST_TRANSACTION_DESCRIPTION'),
            notes: I18N.t('LIST_TRANSACTION_NOTE'),
            plugins: { 'hapi-swagger': { order: 1 } },
            auth: { strategies: ['jwt'], scope: ['admin', 'user'] },
            validate: {
                headers: Common.routeHeaders("authorized"),
                options: options,
                failAction: Common.failover,
                query: transactionListRequest
            },
            response: {
                status: {
                    200: transactionListResponse,
                    400: error400,
                    401: error401,
                    403: error403,
                    404: error404,
                    500: error500
                }
            }
        },
    },

    {
        method: 'POST',
        path: '/payment/subscription-webhook',
        handler: paymentHandler.subscriptionWebhook,
        options: {
            tags: ['api', 'Payment - Transaction'],
            description: I18N.t('LIST_TRANSACTION_DESCRIPTION'),
            notes: I18N.t('LIST_TRANSACTION_NOTE'),
            plugins: { 'hapi-swagger': { order: 1 } },
            auth: false,
            validate: {
                headers: Common.routeHeaders(),
                options: options,
                failAction: Common.failover
            }
        },
    },
    {
        method: 'POST',
        path: '/payment/webhook',
        handler: paymentHandler.paymentWebhook,
        options: {
            tags: ['api', 'Payment - Transaction'],
            description: I18N.t('LIST_TRANSACTION_DESCRIPTION'),
            notes: I18N.t('LIST_TRANSACTION_NOTE'),
            plugins: { 'hapi-swagger': { order: 1 } },
            auth: false,
            validate: {
                headers: Common.routeHeaders(),
                options: options,
                failAction: Common.failover
            }
        },
    },
    {
        method: 'GET',
        path: '/stats/revenue',
        handler: paymentHandler.revenueStats,
        options: {
            tags: ['api', 'Stats'],
            description: I18N.t('LIST_PAYMENT_METHOD_DESCRIPTION'),
            notes: I18N.t('LIST_PAYMENT_METHOD_NOTE'),
            plugins: { 'hapi-swagger': { order: 4 } },
            auth: { strategies: ['jwt'], scope: ['admin', 'user'] },
            validate: {
                headers: Common.routeHeaders("authorized"),
                options: options,
                failAction: Common.failover,
                query: statsRequest
            },
            response: {
                status: {
                    400: error400,
                    401: error401,
                    403: error403,
                    404: error404,
                    500: error500
                }
            }
        },
    },
    {
        method: 'GET',
        path: '/subscription/stats',
        handler: paymentHandler.subscriptionStats,
        options: {
            tags: ['api', 'Stats'],
            description: I18N.t('SUBSCRIPTION_STATS_DESCRIPTION'),
            notes: I18N.t('SUBSCRIPTION_STATS_NOTE'),
            plugins: { 'hapi-swagger': { order: 4 } },
            auth: { strategies: ['jwt'], scope: ['admin'] },
            validate: {
                headers: Common.routeHeaders("authorized"),
                options: options,
                failAction: Common.failover,
                query: statsRequest
            },
            response: {
                status: {
                    400: error400,
                    401: error401,
                    403: error403,
                    404: error404,
                    500: error500
                }
            }
        },
    },
    {
        method: 'GET',
        path: '/subscription/user/stats',
        handler: paymentHandler.subscriptionUserStats,
        options: {
            tags: ['api', 'Stats'],
            description: I18N.t('SUBSCRIPTION_USER_STATS_DESCRIPTION'),
            notes: I18N.t('SUBSCRIPTION_USER_STATS_NOTE'),
            plugins: { 'hapi-swagger': { order: 4 } },
            auth: { strategies: ['jwt'], scope: ['admin'] },
            validate: {
                headers: Common.routeHeaders("authorized"),
                options: options,
                failAction: Common.failover,
                query: statsRequest
            },
            response: {
                status: {
                    400: error400,
                    401: error401,
                    403: error403,
                    404: error404,
                    500: error500
                }
            }
        },
    },
    {
        method: 'POST',
        path: '/subscription/verify/appPurchase',
        handler: paymentHandler.verifyAppPurchase,
        options: {
            tags: ['api', 'Payment - Transaction'],
            description: I18N.t('SUBSCRIPTION_APP_PURCHASE_DESCRIPTION'),
            notes: I18N.t('SUBSCRIPTION_APP_PURCHASE_NOTE'),
            plugins: { 'hapi-swagger': { order: 5 } },
            auth: { strategies: ['jwt'], scope: ['admin', 'user'] },
            validate: {
                headers: Common.routeHeaders("authorized"),
                payload: verifyAppPurchaseRequest,
                failAction: Common.failover
            },

            response: {
                status: {
                    400: error400,
                    401: error401,
                    403: error403,
                    404: error404,
                    500: error500
                }
            }
        }
    },
    {
        method: 'POST',
        path: '/payment/verify',
        handler: paymentHandler.verifyPayment,
        options: {
            tags: ['api', 'Payment - Transaction'],
            description: I18N.t('VERIFY_PAYMENT_DESCRIPTION'),
            notes: I18N.t('VERIFY_PAYMENT_NOTE'),
            auth: { strategies: ['jwt'], scope: ['admin', 'managePlans', 'user'] },
            plugins: { 'hapi-swagger': { order: 1 } },
            validate: {
                headers: Common.routeHeaders("authorized"),
                options: options,
                failAction: Common.failover,
                payload: verifyPaymentRequest,
            },
            response: {
                status: {
                    // 200: successResponse,
                    400: error400,
                    401: error401,
                    403: error403,
                    404: error404,
                    500: error500
                }
            }
        },
    }
]
export default routes

