<?php

require_once 'ABaseTest.php';

class LitbangUserTest extends BaseTest {
    protected $token;
    protected $userId;

    public function setUp()
    {
        parent::setUp();
        $loginInfo = $this->doLogin();
        $this->token = $loginInfo['token'];
        $this->userId = $loginInfo['user_id'];
    }

    public function doLogin()
    {
        $login = $this->login(getenv("TEST_ADMIN_USERNAME"), getenv("TEST_ADMIN_PASSWORD"));
        $this->assertContains(
            [
                'success' => true
            ],
            $login
        );
        return $login['data'];
    }

    #region PRIVATE
    private function doGetListSatgas() {
        $response = $this->client->request(
            'GET',
            'litbang/satgas',
            [
                'headers' => [
                    'Authorization' => 'Bearer ' . $this->token
                ]
            ]
        );

        $statusCode = $response->getStatusCode();
        $jsonResponse = json_decode($response->getBody(), TRUE);
        $this->assertEquals(200, $statusCode);
        $this->assertContains(
            [
                'success' => true
            ],
            $jsonResponse
        );
        $this->assertGreaterThan(0, $jsonResponse['data']['total_record']);

        self::$messages = array_merge(
            self::$messages,
            ["indexSatgas ...passed"]
        );

        return $jsonResponse;
    }
    private function doGetListUser() {
        $response = $this->client->request(
            'GET',
            'litbang/user',
            [
                'headers' => [
                    'Authorization' => 'Bearer ' . $this->token
                ]
            ]
        );

        $statusCode = $response->getStatusCode();
        $jsonResponse = json_decode($response->getBody(), TRUE);
        $this->assertEquals(200, $statusCode);
        $this->assertContains(
            [
                'success' => true
            ],
            $jsonResponse
        );

        self::$messages = array_merge(
            self::$messages,
            ["indexUser ...passed"]
        );

        return $jsonResponse;
    }
    #endregion

    #region TEST
    public function testCreate()
    {
        self::$messages = array_merge(
            self::$messages,
            ["\n--- Start Create Litbang User ---"]
        );

        $listSatgas = $this->doGetListSatgas();
        $satgas = $listSatgas['data']['result'][0];
        $listUser = $this->doGetListUser();
        $countUser = ((int)$listUser['data']['total_record'] + 1);

        $response = $this->client->request(
            'POST',
            'litbang/user/create',
            [
                'json' => [
                    "confirm_password" => "password12345",
                    "email" => str_replace(' ', '.', $satgas['nama']) . ".Anggota" . $countUser . "@mail.id",
                    "jenis_kelamin" => "L",
                    "nama" =>"Anggota " . $countUser . " " . $satgas['nama'],
                    "password" => "password12345",
                    "phone" => "0811" . random_int(10000000, 99999999),
                    "satgas_id" => $satgas['id'],
                    "roles" => ["anggota_litbang"],
                    "username" => "Anggota." . str_replace(' ', '.', $satgas['nama']) . "." . $countUser

                ],
                'headers' => [
                    'Authorization' => 'Bearer ' . $this->token
                ]
            ]
        );

        $statusCode = $response->getStatusCode();
        $jsonResponse = json_decode($response->getBody(), TRUE);
        $this->assertEquals(200, $statusCode);
        $this->assertContains(
            [
                'success' => true
            ],
            $jsonResponse
        );

        self::$messages = array_merge(
            self::$messages,
            ["createUser ...passed"]
        );

        self::$messages = array_merge(
            self::$messages,
            ["--- End Create Litbang User ---"]
        );

        $data = $jsonResponse['data'];
        $data['satgas_id'] = $satgas['id'];

        return $data;
    }

    /**
     * @depends testCreate
     * @param $data
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function testUpdate($data) {
        self::$messages = array_merge(
            self::$messages,
            ["\n--- Start Update Litbang User ---"]
        );

        $response = $this->client->request(
            'POST',
            'litbang/user/update/' . $data['um_id'],
            [
                'json' => [
                    "confirm_password" => "password12345",
                    "email" => $data['email'],
                    "jenis_kelamin" => "L",
                    "nama" => $data['nama'] . " Edit",
                    "password" => "password12345",
                    "phone" => $data['phone'],
                    "satgas_id" => $data['satgas_id'],
                    "roles" => ["anggota_litbang"],
                    "username" => $data['um_user_name']

                ],
                'headers' => [
                    'Authorization' => 'Bearer ' . $this->token
                ]
            ]
        );

        $statusCode = $response->getStatusCode();
        $jsonResponse = json_decode($response->getBody(), TRUE);
        $this->assertEquals(200, $statusCode);
        $this->assertContains(
            [
                'success' => true
            ],
            $jsonResponse
        );

        self::$messages = array_merge(
            self::$messages,
            ["updateUser ...passed"]
        );

        self::$messages = array_merge(
            self::$messages,
            ["--- End Update Litbang User ---"]
        );

        return $jsonResponse['data'];
    }

    /**
     * @depends testUpdate
     * @param $data
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function testDelete($data) {
        self::$messages = array_merge(
            self::$messages,
            ["\n--- Start Delete Litbang User ---"]
        );

        $response = $this->client->request(
            'POST',
            'litbang/user/delete/' . $data['um_id'],
            [
                'headers' => [
                    'Authorization' => 'Bearer ' . $this->token,
                    'Content-Type' => 'application/json'
                ]
            ]
        );

        $statusCode = $response->getStatusCode();
        $jsonResponse = json_decode($response->getBody(), TRUE);
        $this->assertEquals(200, $statusCode);
        $this->assertContains(
            [
                'success' => true
            ],
            $jsonResponse
        );

        self::$messages = array_merge(
            self::$messages,
            ["deleteUser ...passed"]
        );

        self::$messages = array_merge(
            self::$messages,
            ["--- End Delete Litbang User ---"]
        );

        return $jsonResponse['data'];
    }

    /**
     * @depends testDelete
     * @param $data
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function testRestore($data) {
        self::$messages = array_merge(
            self::$messages,
            ["\n--- Start Restore Litbang User ---"]
        );

        $response = $this->client->request(
            'POST',
            'litbang/user/restore/' . $data['um_id'],
            [
                'headers' => [
                    'Authorization' => 'Bearer ' . $this->token,
                    'Content-Type' => 'application/json'
                ]
            ]
        );

        $statusCode = $response->getStatusCode();
        $jsonResponse = json_decode($response->getBody(), TRUE);
        $this->assertEquals(200, $statusCode);
        $this->assertContains(
            [
                'success' => true
            ],
            $jsonResponse
        );

        self::$messages = array_merge(
            self::$messages,
            ["restoreUser ...passed"]
        );

        self::$messages = array_merge(
            self::$messages,
            ["--- End Restore Litbang User ---"]
        );

        return $jsonResponse['data'];
    }

    /**
     * @depends testRestore
     * @param $data
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function testDetail($data) {
        self::$messages = array_merge(
            self::$messages,
            ["\n--- Start Detail Litbang User ---"]
        );

        $response = $this->client->request(
            'GET',
            'litbang/user/detail/' . $data['um_id'],
            [
                'headers' => [
                    'Authorization' => 'Bearer ' . $this->token
                ]
            ]
        );

        $statusCode = $response->getStatusCode();
        $jsonResponse = json_decode($response->getBody(), TRUE);
        $this->assertEquals(200, $statusCode);
        $this->assertEquals($data['um_id'], $jsonResponse['data']['um_user']['um_id']);

        self::$messages = array_merge(
            self::$messages,
            ["detailUser ...passed"]
        );

        self::$messages = array_merge(
            self::$messages,
            ["--- End Detail Litbang User ---"]
        );

        return $data;
    }

    public function testIndex() {
        self::$messages = array_merge(
            self::$messages,
            ["\n--- Start Index Litbang User ---"]
        );

        $this->doGetListUser();

        self::$messages = array_merge(
            self::$messages,
            ["--- End Index Litbang User ---"]
        );
    }

    public function testCreateDirektur()
    {
        self::$messages = array_merge(
            self::$messages,
            ["\n--- Start Create Litbang User Direktur ---"]
        );

        $listUser = $this->doGetListUser();
        $countUser = ((int)$listUser['data']['total_record'] + 1);

        $response = $this->client->request(
            'POST',
            'litbang/user/create',
            [
                'json' => [
                    "confirm_password" => "password12345",
                    "email" => 'direktur.litbang'.$countUser.'@mail.id',
                    "jenis_kelamin" => "L",
                    "nama" => "Direktur Litbang " . $countUser,
                    "password" => "password12345",
                    "phone" => "0811" . random_int(10000000, 99999999),
                    "satgas_id" => null,
                    "roles" => ["direktur_litbang"],
                    "username" => "direktur_litbang_" . $countUser

                ],
                'headers' => [
                    'Authorization' => 'Bearer ' . $this->token
                ]
            ]
        );

        $statusCode = $response->getStatusCode();
        $jsonResponse = json_decode($response->getBody(), TRUE);
        $this->assertEquals(200, $statusCode);
        $this->assertContains(
            [
                'success' => true
            ],
            $jsonResponse
        );

        self::$messages = array_merge(
            self::$messages,
            ["createUser ...passed"]
        );

        self::$messages = array_merge(
            self::$messages,
            ["--- End Create Litbang User Direktur ---"]
        );
    }
    #endregion
}
