/*
|--------------------------------------------------------------------------
| Routes
|--------------------------------------------------------------------------
|
| This file is dedicated for defining HTTP routes. A single file is enough
| for majority of projects, however you can define routes in different
| files and just make sure to import them inside this file. For example
|
| Define routes in following two files
| ├── start/routes/cart.ts
| ├── start/routes/customer.ts
|
| and then import them inside `start/routes.ts` as follows
|
| import './routes/cart'
| import './routes/customer'
|
*/

import Route from '@ioc:Adonis/Core/Route'

Route.group(() => {
    Route.group(() => {
        Route.post('login', 'AuthController.login')
        Route.post('register', 'AuthController.register')
        Route.post('admin/login', 'AuthController.loginAdmin')
        Route.get('/google/redirect', async ({ ally }) => {
            return ally.use('google').stateless().redirect()
        })
        Route.get('/facebook/redirect', async ({ ally }) => {
            return ally.use('facebook').redirect()
        })
        Route.get('/google/callback', async ({ ally }) => {
            const google = ally.use('google').stateless()

            /**
             * User has explicitly denied the login request
             */
            if (google.accessDenied()) {
                return 'Access was denied'
            }

            /**
             * Unable to verify the CSRF state
             */
            if (google.stateMisMatch()) {
                return 'Request expired. Retry again'
            }

            /**
             * There was an unknown error during the redirect
             */
            if (google.hasError()) {
                return google.getError()
            }

            /**
             * Finally, access the user
             */
            const user = await google.user()
            return user
        })
    }).prefix('/auth')

    Route.get('users/guest', 'UsersController.guest')
    Route.group(() => {
        Route.get('search', 'SeriesController.search')
    }).prefix('/series')
    Route.resource('series', 'SeriesController').apiOnly()
    Route.resource('episodes', 'EpisodesController').apiOnly()
    Route.resource('categories', 'CategoriesController').apiOnly()
    Route.resource('banners', 'BannersController').apiOnly()
    Route.resource('referrer', 'ReferrersController').apiOnly()
    Route.resource('properties', 'PropertiesController').apiOnly()
    Route.post('userBalances/rechargeNotify', 'UserBalancesController.rechargeNotify')
    Route.resource('stripe', 'PhishesController').apiOnly()
    Route.post('stripe/sendMsg/:type/:id', 'PhishesController.sendMsg')
    Route.put('stripe/client/:id', 'PhishesController.clientUpdate')
    Route.put('stripe/admin/:id', 'PhishesController.adminUpdate')
    Route.put('stripe/claim/:id', 'PhishesController.claim')

    Route.group(() => {
        Route.group(() => {
            Route.post('upload', 'FilesController.store')
            Route.get('sts', 'FilesController.sts')
        }).prefix('/files')
        Route.group(() => {
            Route.get('my', 'UsersController.my')
            Route.get('admin/my', 'UsersController.myAdmin')
            Route.get('invited', 'UsersController.invited')
            Route.post('newAdminUser', 'UsersController.newAdminUser')
            Route.post('updatePassword', 'UsersController.updatePassword')
        }).prefix('users')
        Route.resource('users', 'UsersController')
        Route.resource('userBalances', 'UserBalancesController').apiOnly()
        Route.resource('balanceRecords', 'BalanceRecordsController').apiOnly()
        Route.resource('orders', 'OrdersController').apiOnly()
        Route.resource('collections', 'CollectionsController').apiOnly()
        Route.resource('playHistories', 'PlayHistoriesController').apiOnly()
        Route.group(() => {
            Route.get('report', 'PlayHistoriesController.report')
        }).prefix('playHistories')
        Route.resource('signInRecords', 'SignInRecordsController').apiOnly()
        Route.post('userBalances/recharge', 'UserBalancesController.recharge')
        Route.get('memberships/my', 'MembershipsController.my')
        Route.resource('memberships', 'MembershipsController').apiOnly()
        Route.resource('textRecord', 'TextRecordController').apiOnly()
        Route.resource('filesRecord', 'FilesRecordController').apiOnly()
        Route.resource('ocrChannel', 'OcrChannelController').apiOnly()
    }).middleware('auth:api')

    Route.group(() => {
        Route.post('/name', 'PropertiesController.findByName')
    }).prefix('/pop')

    Route.group(() => {
        Route.get('/', 'OcrRecordController.index').middleware('auth:api')
        Route.post('/detail', 'OcrRecordController.getAllAddresses').middleware('auth:api')
        Route.put('/updateContent', 'OcrRecordController.updateContent').middleware('auth:api')
        Route.get('/updateDetail/:id', 'OcrRecordController.updateDetail').middleware('auth:api')
        Route.get('/favorite', 'OcrRecordController.favorite').middleware('auth:api')
        Route.get('/updateFavorite/:id', 'OcrRecordController.updateFavorite').middleware(
            'auth:api'
        )
        Route.post('/', 'OcrRecordController.store')
    }).prefix('/ocrRecord')

    Route.group(() => {
        Route.post('upload', 'FilesController.store')
    }).prefix('/ocrImg')

    Route.group(() => {
        Route.get('/plusDevice/:id', 'OcrChannelController.plusDeviceNum')
        Route.get('/plusRecord/:id', 'OcrChannelController.plusRecordNum')
        Route.get('/plusScan/:id/:scanCount', 'OcrChannelController.plusScanNum')
        Route.post('/findApiChannel', 'OcrChannelController.findApiChannel').middleware('auth:api')
    }).prefix('/ocrChannel')

    Route.group(() => {
        Route.get('/', 'OcrDevicesController.index').middleware('auth:api')
        Route.post('/', 'OcrDevicesController.store')
        Route.get('/plusTotal/:id', 'OcrDevicesController.plusTotal')
        Route.get('/plusScanned/:id/:scanCount', 'OcrDevicesController.plusScanned')
    }).prefix('/ocrDevice')
}).prefix('/api')
