<?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Seeder;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Support\Facades\Storage;
use Illuminate\Http\UploadedFile;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider;
use Auth;
use Artisan;
use App\User;
use App\Booking;
use App\UserBankSlip;
use App\Role; 
use App\SettingCredit;

class BookingTest extends TestCase
{
    protected $user;
    protected $booking;
    protected $userBankSlip;
    protected $setting_credit;
    
    public function setUp()
    {   
        parent::setUp();

        \DB::statement('SET FOREIGN_KEY_CHECKS=0');
        Artisan::call('db:seed');
        \DB::statement('SET FOREIGN_KEY_CHECKS=1');

        $this->user = factory(User::class)->create();
        $this->setting_credit = factory(SettingCredit::class)->create();
        $this->booking = factory(Booking::class)->create([
            'user_id' => $this->user->id
        ]);
        $this->userBankSlip = factory(UserBankSlip::class)->create([
            'booking_id' => $this->booking->id
        ]);
    }

    public function testCreate()
    {
      $response =
      $this->actingAs($this->user)
        ->postJson('/api/booking/',  [
            'account_name' => 'johnny',
            'account_num' => '1234567',
            'bank_name' => 'myabnak',
            'bank_branch' => 'banking',
            'company_name' => 'besaras',
            'company_address' => 'californina',
            'bank_address' => 'cyber',
            'term' => 'x2_cash',
            'amount' => '30000',
            'rmb_book_amount' => '2131',
            'rmb_book_pay_method' => '2131rea',
            'rmb_book_account_name' => '2131rew',
            'rmb_book_acc_no' => '2131',
            'rmb_book_bank_branch' => '2131rew'
        ]);

        if(14000 > $this->setting_credit->rmb_credit_limit){
            $response->assertStatus(401);  
        }
        else{
            $response->assertStatus(201);   
        }
    }

    // public function testUploadUserSlip()
    // {
    //     Storage::fake('file');
    //     $size_in_kb = 3072;    // 3072KB = 3MB
    //     $bankslip_path = UploadedFile::fake()->image('comp.jpg')->size($size_in_kb);

    //     $response = 
    //     $this->actingAs($this->user)
    //         ->json('POST', '/api/booking/' . $this->booking->id . '/upload-user-bankslip', [
    //             'file' => $bankslip_path
    //         ]);

    //     if(!file_exists($bankslip_path) || $size_in_kb > 3072){
    //         $response->assertStatus(400);    
    //     }
    //     else{
    //         $response->assertSuccessful();
    //     }
    // }

    public function testUpdateBankslipAmount()
    {
        $this->actingAs($this->user)
            ->json('PATCH', '/api/booking/' . $this->booking->id . '/bankslip-amount', [
                'transfer_amount' => "9000",
                'slip_image' => "data:image/png;base64,[encoded data, 49676 characters]"
            ])
            ->assertSuccessful();        
    }


    // public function testCancel()
    // {
    //     $this->actingAs($this->user)
    //         ->json('POST','/api/booking/' . $this->booking->id . '/cancel', [])
    //         ->assertSuccessful();
    // }
    
    public function testApproveBankSlip()
    {
        $this->actingAs($this->user)
            ->json('POST','/api/booking/' . $this->booking->id . '/approve-bank-slip', [
            ])
            ->assertSuccessful();
    }

    public function testRejectBankSlip()
    {
        $this->actingAs($this->user)
            ->json('POST','/api/booking/' . $this->booking->id . '/reject-bank-slip', [
                'reject_reason' => 'Amount no enough'
            ])
            ->assertSuccessful();
    }

    // Only this is Admin permision required
    // public function testUploadInvoice(){
    //     $this->user->roles()->sync([]);
    //     $this->user->attachRole(Role::Where('name','admin')->first());

    //     $size_in_kb = 3072;
    //     Storage::fake('invoice_path');
    //     $invoice_path = UploadedFile::fake()->create('invoice.pdf',$size_in_kb);
        
    //     $response = 
    //     $this->actingAs($this->user)
    //         ->json('POST', '/api/booking/' . $this->booking->id . '/upload-invoice', [
    //             'invoice_path' => $invoice_path,
    //             'amount' => '12345'
    //         ]);
        
    //     if(!file_exists($invoice_path) || $size_in_kb > 3072){
    //         $response->assertStatus(400);    
    //     }
    //     else{
    //         $response->assertSuccessful();
    //     }
    // }
}
