<?php

namespace Gaco\SnapBi\Tests\Unit;

use Gaco\SnapBi\Tests\TestCase;

class TokenResponseTest extends TestCase
{
    public function test_token_success()
    {
        $response = \SnapBi()->token()->response()->success([
            'accessToken' => 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9',
            'tokenType' => 'Bearer',
            'expiresIn' => '900',
            'additionalInfo' => [
                'deviceId' => '12345679237',
                'channel' => 'mobilephone'
            ]
        ]);

        $this->assertEquals(200, $response->getStatusCode());
        
        $data = json_decode($response->getContent(), true);
        
        $this->assertEquals('2007300', $data['responseCode']);
        $this->assertEquals('Successful', $data['responseMessage']);
        $this->assertEquals('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9', $data['accessToken']);
        $this->assertEquals('Bearer', $data['tokenType']);
        $this->assertEquals('900', $data['expiresIn']);
        $this->assertArrayHasKey('additionalInfo', $data);
        $this->assertEquals('12345679237', $data['additionalInfo']['deviceId']);
    }

    public function test_token_error()
    {
        $response = \SnapBi()->token()->response()->error([
            'httpCode' => 401,
            'caseCode' => 1,
            'message' => 'Invalid credentials'
        ]);

        $this->assertEquals(401, $response->getStatusCode());
        
        $data = json_decode($response->getContent(), true);
        
        $this->assertEquals('Invalid credentials', $data['responseMessage']);
        $this->assertArrayHasKey('responseCode', $data);
        // Error response should only have responseCode and responseMessage
        $this->assertArrayNotHasKey('errorData', $data);
    }
}

