<?php

class KIP2Controller extends BaseController
{
    public function getProfileSiswa()
    {
        $this->checkIsAuthenticated();

        // update 20230117, ambil dari app config
        $year = AppConfig::getValueByKode('PIP_YEAR');
        if (!$year) $year = date('Y');

        $params = $this->validate(
            $this->request->get(),
            [
                'nisn' => ['validators' => ['required']],
                'jenjang' => ['validators' =>  [[
                    'name' => 'inclusion_in',
                    'params' => ['sd', 'smp', 'sma', 'smk']
                ]]]
            ]
        );

        $kip2Client = new Kip2Client($this->config["kip2_prop"]);
        $result = [];

        try {
            $kip2Client->setLogger($this->logger);
            $response = $kip2Client->getProfileSiswa($year, $params['jenjang'], $params['nisn']);

            if (empty($response)) {
                throw new CustomErrorException(BaseResponse::UNEXPECTED_ERROR, "Gagal Menghubungi Service SIPINTAR KEMENDIKBUD");
            }

            if (array_key_exists('status', $response) && (int)$response['status'] == 0) {
                $result[] = $response['data'];
            }
        } catch (\Exception $e) {
        }

        return new CollectionPaginatedResponse($result);
    }

    public function pipSiswa($nisn) {
        $this->checkIsAuthenticated();
        $nisn = trim($nisn);
        try {
            $kip2Client = new Kip2Client($this->config["kip2_prop"]);
            $kip2Client->setLogger($this->logger);
            $result = $kip2Client->pipSiswa($nisn);
            return new SimpleResponse($result);
        } catch (CustomErrorException $e) {
            throw $e;
        } catch (\Exception $e) {
            throw new CustomErrorException(BaseResponse::UNEXPECTED_ERROR, "Gagal Menghubungi Service KEMENDIKBUD");
        }
    }

    public function getRekapAktivasiRekening()
    {
        $this->checkIsAuthenticated();

        $params = $this->validate(
            $this->request->get(),
            [
                'npsn' => ['validators' => ['required']],
                'tahun' => ['validators' => ['required', 'digit']],
            ]
        );

        $kip2Client = new Kip2Client($this->config["kip2_prop"]);
        $result = [];

        try {
            $kip2Client->setLogger($this->logger);
            $response = $kip2Client->getRekapAktivasiRekening($params['tahun'], $params['npsn']);
            if (empty($response)) {
                throw new CustomErrorException(BaseResponse::UNEXPECTED_ERROR, "Gagal Menghubungi Service SIPINTAR KEMENDIKBUD");
            }

            if (array_key_exists('status', $response) && (int)$response['status'] == 0) {
                $result = $response['data'];
            }
        }
        catch (Exception $e) {

        }

        return new CollectionPaginatedResponse($result);
    }

    public function getDataSekolah()
    {
        $this->checkIsAuthenticated();

        $params = $this->validate(
            $this->request->get(),
            [
                'npsn' => ['validators' => ['required']],
                'tahun' => ['validators' => ['required', 'digit']],
            ]
        );

        $kip2Client = new Kip2Client($this->config["kip2_prop"]);
        $result = [];

        try {
            $kip2Client->setLogger($this->logger);
            $response = $kip2Client->getDataSekolah($params['tahun'], $params['npsn']);
            if (empty($response)) {
                throw new CustomErrorException(BaseResponse::UNEXPECTED_ERROR, "Gagal Menghubungi Service SIPINTAR KEMENDIKBUD");
            }

            if (array_key_exists('status', $response) && (int)$response['status'] == 0) {
                $result = $response['data'];
            }
        }
        catch (Exception $e) {

        }

        return new CollectionPaginatedResponse($result);
    }

    public function getAlokasi()
    {
        $this->checkIsAuthenticated();

        $params = $this->validate(
            $this->request->get(),
            [
                'tahun' => ['validators' => ['required', 'digit']],
            ]
        );

        $kip2Client = new Kip2Client($this->config["kip2_prop"]);
        $result = [];

        try {
            $kip2Client->setLogger($this->logger);
            $response = $kip2Client->getAlokasi($params['tahun']);
            if (empty($response)) {
                throw new CustomErrorException(BaseResponse::UNEXPECTED_ERROR, "Gagal Menghubungi Service SIPINTAR KEMENDIKBUD");
            }

            if (array_key_exists('status', $response) && (int)$response['status'] == 0) {
                $result = $response['data'];
            }
        }
        catch (Exception $e) {

        }

        return new CollectionPaginatedResponse($result);
    }

    public function getRekap()
    {
        $params = $this->validate(
            $this->request->get(),
            [
                'tahun' => ['validators' => ['required', 'digit']],
            ]
        );

        $kip2Client = new Kip2Client($this->config["kip2_prop"]);
        $result = [];

        try {
            $kip2Client->setLogger($this->logger);
            $response = $kip2Client->getRekap($params['tahun']);
            if (empty($response)) {
                throw new CustomErrorException(BaseResponse::UNEXPECTED_ERROR, "Gagal Menghubungi Service SIPINTAR KEMENDIKBUD");
            }

            if (array_key_exists('status', $response) && (int)$response['status'] == 0) {
                $result = $response['data'];
            }
        }
        catch (Exception $e) {

        }

        return new CollectionPaginatedResponse($result);
    }
}
