<?php
use App\Feedback;
use App\User;
use App\Organization;
use Illuminate\Http\UploadedFile;
class FeedbackTest extends TestCase
{
    /**
     * Test to check get feedbacks
     *
     * @return void
     */
    public function testFeedback()
    {
       $response = $this->call('GET', 'api/v1/feedback/customers');
       $this->assertEquals(200, $response->status());
    }


    /**
     * Test to check get admin dashboard
     *
     * @return void
     */
    public function testGetAdminDashboard()
    {
       $response = $this->call('GET', 'api/v1/admin/dashboard');
       $this->assertEquals(200, $response->status());
    }
    
    /**
     * Test to check manager dashboard
     *
     * @return void
     */
    public function testManagerCustomers()
    {
        $response = $this->call('POST', 'api/v1/feedback/customers', [
           'organizationId' => 1,
        ]);
        $this->assertEquals(200, $response->status());
    }

     /**
     * Test to check manager dashboard
     *
     * @return void
     */
    // public function testCouponMail()
    // {
    //     $response = $this->call('POST', 'api/v1/feedback/customers', [
    //        'organization_id' => 1,
    //     ]);
    //     $this->assertEquals(200, $response->status());
    // }
    
    
    /**
     * Test to check send feedback link
     *
     * @return void
     */
    public function testSendFeedbackLink()
    {

        $user = User::where('role', 'ROLE_ADMIN')->select('email')->orderBy('id', 'desc')->first(); 

        $employee = User::where('role', 'ROLE_EMPLOYEE')->select('id')->orderBy('id', 'desc')->first(); 
        $email = str_random(5).'@aa.com';
        $response = $this->call('POST', 'api/v1/feedback/sendlink', [
           'email'       => $email,
           'mobile'      => 1234567890,
           'user_id'     => $employee->id,
           'organization_id' => 1,
           'place' => 'usa'
        ]);
        $this->assertEquals(200, $response->status());


        $this->post('api/v1/feedback/sendlink', [
           'email'       => $user->email,
           'mobile'      => 123456,
           'user_id'     => $employee->id,
           'organization_id' => 1,
           'place' => 'usa'
                ])
             ->seeJson([
                'status' => 'fail',
             ]);


    }
    




    /**
     * Test to check save feedback
     *
     * @return void
     */
    public function testSaveFeedback()
    {
        $result = DB::table('feedback')
                ->select('token')
                ->where('customer_name', NULL)
                ->where('status', 2)
                ->orderBy('id', 'desc')->first();
        $token = $result->token;
        $this->post('api/v1/feedback/save', [
            'name'        => 'asasas',
            'token'       => $token,
            'feedback'    => 'sample review',
            'rating'      => '3',
            'place'       => ''
                ])
             ->seeJson([
                'status' => 'success',
             ]);
        
        $this->post('api/v1/feedback/save', [
            'name'        => 'asasas',
            'token'       => 'asassa',
            'feedback'    => 'sample review',
            'rating'      => '3',
            'place'       => ''
                ])
             ->seeJson([
                'status' => 'fail',
             ]);
    }
    
    
public function testStoreFeedback()
{
        $user = DB::table('users')
                ->select('id')
                ->where('role', 'ROLE_EMPLOYEE')
                ->first();
        $manager = DB::table('users')
                ->select('id')
                ->where('role', 'ROLE_MANAGER')
                ->first();
        $email= str_random(5).'@bb.com';
        $last = Organization::max('id');
        
        $this->post('api/v1/feedback/storefeedback', [
            'name'        => 'sample',
            'email'       => $email,
            'feedback'    => 'this is feedback',
            'location'    => 'location',
            'rating'      => '2',
            'mobile'      => '123456',
            'user_id'     => $user->id,
            'email_media' => '0',
            'organization_id' => $last,
            'signature'=> UploadedFile::fake()->image('customers/113/testflower.jpg'),
            ])
            ->seeJson([
                'status' => 'success',
            ]);
        
        $this->post('api/v1/feedback/storefeedback', [
            'name'        => 'sample',
            'email'       => $email,
            'feedback'    => 'this is feedback',
            'location'    => 'location',
            'rating'      => '2',
            'mobile'      => '123456',
            'user_id'     => $manager->id,
            'email_media' => '0',
            'organization_id' => $last,
            'signature'=> UploadedFile::fake()->image('customers/113/testflower.jpg'),
            ])
            ->seeJson([
                'status' => 'fail',
            ]);
    }
    
    public function testFeedbackOld()
{
        $user = DB::table('users')
                ->select('id')
                ->where('role', 'ROLE_EMPLOYEE')
                ->first();
        $manager = DB::table('users')
                ->select('id')
                ->where('role', 'ROLE_MANAGER')
                ->first();
        $email= str_random(5).'@bb.com';
        $last = Organization::max('id');
        
        $this->post('api/v1/feedback/storefeedbackold', [
            'name'        => 'sample',
            'email'       => $email,
            'feedback'    => 'this is feedback',
            'location'    => 'location',
            'rating'      => '2',
            'mobile'      => '123456',
            'user_id'     => $user->id,
            'organization_id' => $last,
            'signature'=> UploadedFile::fake()->image('customers/113/testflower.jpg'),
            ])
            ->seeJson([
                'status' => 'success',
            ]);
        $this->post('api/v1/feedback/storefeedback', [
            'name'        => 'sample',
            'email'       => $email,
            'feedback'    => 'this is feedback',
            'location'    => 'location',
            'rating'      => '2',
            'mobile'      => '123456',
            'user_id'     => $manager->id,
            'email_media' => '0',
            'organization_id' => $last,
            'signature'=> UploadedFile::fake()->image('customers/113/testflower.jpg'),
            ])
            ->seeJson([
                'status' => 'fail',
            ]);
    }
    
    /**
     * Test to check get feedback
     *
     * @return void
     */
    public function testGetFeedback()
    {
       $response = $this->call('GET', 'api/v1/feedback/13');
       $this->assertEquals(200, $response->status());
       
       $response = $this->call('GET', 'api/v1/feedback/3');
       $this->assertEquals(200, $response->status());
       
       $this->get('api/v1/feedback/11212')
             ->seeJson([
                'status' => 'fail',
             ]);
    }
    
    
    /**
     * Test to check update template
     *
     * @return void
     */
    public function testUpdateFeedback()
    {
       
        $this->put('api/v1/feedback/14', [
             'customer_name'  => 'asasas',
             'mobile'         => '123456',
             'feedback'       => 'sample review',
             'email'          => 'fi6aa@aa.com',
             'location'       => 'asd',
             'status'         => '1',
             'rating'         => '2',
            'feedback_id'     => 14
            ])
            ->seeJson([
                'status' => 'success',
            ]);
       
          $this->put('api/v1/feedback/14222222', [
             'customer_name'  => 'asasas',
             'mobile'         => '123456',
             'feedback'       => 'sample review',
             'email'          => 'fi6aa@aa.com',
             'location'       => 'asd',
             'status'         => '1',
             'rating'         => '2',
             'feedback_id'   => 14222222
            ])
             ->seeJson([
                'status' => 'fail',
             ]);
    }
    
    /**
     * Test to check get templates
     *
     * @return void
     */
    public function testGetTemplate()
    {
       $response = $this->call('GET', 'api/v1/template/1');
       $this->assertEquals(200, $response->status());
       
       $this->get('api/v1/template/11212')
             ->seeJson([
                'status' => 'fail',
             ]);
    }
    
    
    
}
