<?php

use App\Classes\Modules\Documents\DataTransferObjects\DocumentObject;
use App\Classes\Modules\Documents\Services\CreatesDocument;
use App\Classes\Modules\Documents\Services\CreatesFiles;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\DocumentType;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Models\Booking;
use App\Models\Company;
use App\Models\Transaction;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Maatwebsite\Excel\Excel;
use Meneses\LaravelMpdf\Facades\LaravelMpdf;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('', function () {
    return view('pages.accounts.login');
})->name('login');

Route::get('/account/email/verification/{token}', function ($token) {
    return view('pages.accounts.email_verified', ['token' => $token]);
})->name('account.email.verification');

Route::get('/account/password/reset/{token}', function ($token) {
    return view('pages.accounts.reset_password', ['token' => $token]);
})->name('account.password.reset');

Route::get('/dashboard', function () {
    return view('pages.dashboards.index');
})->name('dashboard');

Route::get('/banks', function () {
    return view('pages.banks.index');
})->name('banks');


Route::get('/settings', function () {
    return view('pages.settings');
})->name('settings');

Route::get('/customers', function () {
    return view('pages.customers.index');
})->name('customers');

Route::get('/customer/{marking}', function ($marking) {
    $id = \App\Models\Company::where('reference', '=', $marking)->first()->id;
    return view('pages.customers.profile', ['id' => $id]);
})->name('customer.profile');

Route::get('/payments', function () {
    return view('pages.payments');
})->name('payments');

Route::get('/billings', function () {
    return view('pages.billings');
})->name('billings');

Route::get('/currency_orders', function () {
    return view('pages.currency_orders');
})->name('currency_orders');

Route::get('/transfers', function () {
    return view('pages.bookings.index');
})->name('bookings');

Route::get('/bookings/urgent', function () {
    return view('pages.urgent_list');
})->name('bookings.urgent');

Route::get('/transfer/{marking}', function ($marking) {
    return view('pages.bookings.profile', ['marking' => $marking]);
})->name('booking.details');

Route::get('/transfer/merge/{marking}', function ($marking) {
    return view('pages.bookings.merge', ['marking' => $marking]);
})->name('booking.merge');

Route::get('/online_payment/redirect', 'Billplz\CallbackBillplzController@callback')->name('online_payment.redirect');

Route::get('/export/customers/f614e339d7058904a831aad742e24d55', 'Exports\ExportCustomersToExcelController@export');
Route::get('/export/transactions/f614e339d7058904a831aad742e24d55', 'Exports\ExportCustomersToExcelController@transactions');

Route::get('/products', function (\App\Classes\Modules\Exports\Services\ExportsProducts $exportsProducts) {
    return $exportsProducts->download('products.csv', Excel::CSV, ['Content-Type' => 'text/csv']);
})->name('products.random');

Route::get('/fix_bills', function () {
    ini_set('max_execution_time', '1000000');
    Auth()->login(User::find(1));
    $bookings = \App\Models\Booking::where('status', 3)->whereHas('transactions', function ($query){
        return $query->where('type', 1)->whereHas('transactions', function($query){
           return $query->where('type', 3)->where('issuer', 2210);
        });
    })->get();
    foreach ($bookings as $booking){
        $booking->documents()->where('document_type', DocumentType::SUPPLIER_DELIVER_ORDER)->delete();
        $po = $booking->transactions()->where('type', TransactionType::PURCHASE_ORDER)->first();
        $supplier_deliver_order_transaction = $booking->transactions()->where('type', TransactionType::SUPPLIER_DELIVER)->first();
        $supplier = Company::find($booking->transactions()->where('type', TransactionType::PAYMENT)->where('status', 3)->first()->transactions()->first());
        $supplier_order_pdf = LaravelMpdf::loadView('pages.pdfs.supplier_deliver_order', ['supplier_deliver_order_transaction' => $supplier_deliver_order_transaction, 'po_order_transaction' => $po, 'supplier' => $supplier]);
        $document_object = new DocumentObject(
            DocumentType::SUPPLIER_DELIVER_ORDER,
            [chunk_split('data:application/pdf;base64,'.base64_encode($supplier_order_pdf->output()))],
            '',
            ApprovalStatus::COMPLETED,
            'supplier_delivery_orders'
        );

        $document = (App()->make(CreatesDocument::class))->execute($booking, $document_object);
        (App()->make(CreatesFiles::class))->execute($document, $document_object);
    }


})->name('products.random');

Route::get('/wallet/{marking}/details', function ($marking) {
    $id = \App\Models\Company::where('reference', '=', $marking)->first()->id;
    return view('pages.wallet.index', ['id' => $id]);
})->name('wallet.details');

Route::get('/wallets', function () {
    return view('pages.wallet.wallets');
})->name('wallet.wallets');

Route::get('/bookings/billplz', function () {
    return view('pages.billplz_redirect');
})->name('bookings.billplz');

Route::get('/document/download', 'Bookings\DownloadBookingDocumentController@download')->name('documents.download');

Route::get('billplz/bills/{bill_no}', function($bill_no){
   return redirect(env('BILLPLZ_BASE_URL').'/bills/'.$bill_no);
})->name('billplz.bill');


Route::get('/export/customers/f614e339d7058904a831aad742e24d55', 'Exports\ExportCustomersToExcelController@export');
Route::get('/export/transactions/f614e339d7058904a831aad742e24d55', 'Exports\ExportCustomersToExcelController@transactions');
Route::get('/export/null-debtor/f614e339d7058904a831aad742e24d55', 'Exports\ExportCustomersToExcelController@nullDebtor')->name('newDebtor.export');
Route::get('/export/payment-transactions/f614e339d7058904a831aad742e24d55', 'Exports\ExportCustomersToExcelController@paymentTransactions')->name('paymentTransactions.export');
Route::get('/export/wallet-transactions/f614e339d7058904a831aad742e24d55', 'Exports\ExportCustomersToExcelController@walletTransactions')->name('walletTransactions.export');

Route::get('/products', function (\App\Classes\Modules\Exports\Services\ExportsProducts $exportsProducts) {
    $bookings = Booking::where(function($query){
        return $query->whereMonth('created_at', 7)->orWhereMonth('created_at', 8)->orWhereMonth('created_at', 9)->orWhereMonth('created_at', 10)->orWhereMonth('created_at', 11)->orWhereMonth('created_at', 12);
    })->whereDoesntHave('transactions', function($q){
        $q->where('type', TransactionType::PURCHASE_ORDER);
        $q->whereIn('status', [ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::APPROVED]);
    })->get();
    dd($bookings->count());
    return $exportsProducts->download('products.csv', Excel::CSV, ['Content-Type' => 'text/csv']);
})->name('products.random');

Route::get('/auto-purchase-order-fill', 'Bookings\AutoPurchaseOrderFillController@auto')->name('assign');

Route::get('purchase/sensitive/', function(Request $request){
    $keywords = json_decode($request->input('keywords'));
    $items = \App\Models\TransactionDetail::query();
    foreach ($keywords as $key => $keyword){
        if(!$key){
            $items->Where('product_name', 'like', '%'.$keyword.'%');
            continue;
        }

        $items->orWhere('product_name', 'like', '%'.$keyword.'%');
    }

    $items = $items->get();

    foreach ($items as $key => $item){
        echo ($key+1).'. '.$item->product_name.' => <a href="'.\route('booking.details', $item->transaction->owner->marking).'" target="_blank">'.$item->transaction->owner->marking.'</a><br><br>';
    }
});

Route::get('/transactions/supplier/{id}/mock_up', 'Transactions\DownloadMockUpWhiteFormPdfController@download')->name('whiteForm.mockUp');

Route::get('/notifications/list', 'Notifications\ListNotificationsController@list')->name('notifications.list');
