<?php

namespace App\Repositories\Auth;

use GuzzleHttp\Client as GuzzleHttpClient;

class IsdmRepository
{
    /**
     * Get data from isdm.
     *
     * @param  varchar  $nip
     * @return \Illuminate\Http\Response
     */
    public function nip($nip)
    {
        $client = new GuzzleHttpClient();
        $apiRequest = $client->request('GET', 'https://i-sdm.unesa.ac.id/biodataumum/' . $nip);
        $isdm = json_decode($apiRequest->getBody()->getContents());

        return $isdm;
    }

    /**
     * Get data from isdm.
     *
     * @param  varchar  $nidn
     * @return \Illuminate\Http\Response
     */
    public function nidn($nidn)
    {
        $client = new GuzzleHttpClient();
        $apiRequest = $client->request('GET', 'https://i-sdm.unesa.ac.id/api/biodataumum/' . $nidn);
        $isdm = json_decode($apiRequest->getBody()->getContents());

        return $isdm;
    }

    public function nuptk($nuptk)
    {
        $client = new GuzzleHttpClient();
        $apiRequest = $client->request('GET', 'https://i-sdm.unesa.ac.id/api/biodataumum/' . $nuptk);
        $isdm = json_decode($apiRequest->getBody()->getContents());

        return $isdm;
    }

    /**
     * Get data from i-sdm by NIDN or NIP (untuk NUPTK).
     * Coba NIDN dulu, jika gagal coba NIP.
     *
     * @param  string  $value  NIDN atau NUPTK/NIP
     * @return object[]|null  Data isdm atau null jika tidak ditemukan
     */
    public function getByNidnOrNip($value)
    {
        $value = trim($value);
        if ($value === '') {
            return null;
        }

        try {
            $isdm = $this->nidn($value);
            if (!empty($isdm) && isset($isdm[0])) {
                return $isdm;
            }
        } catch (\Exception $e) {
            // coba dengan NIP (biodataumum by nip/nuptk)
        }

        try {
            $isdm = $this->nip($value);
            if (!empty($isdm) && isset($isdm[0])) {
                return $isdm;
            }
        } catch (\Exception $e) {
            return null;
        }

        try {
            $isdm = $this->nuptk($value);
            if (!empty($isdm) && isset($isdm[0])) {
                return $isdm;
            }
        } catch (\Exception $e) {
            return null;
        }

        return null;
    }

    /**
     * Ambil NIDN atau NUPTK berdasarkan email: API SSO userid/{email} → userid → i-sdm biodataumum/{userid}.
     *
     * @param  string  $email
     * @return string|null  NIDN atau NUPTK, null jika tidak ditemukan
     */
    public function getNidnOrNuptkByEmail($email)
    {
        $data = $this->getBiodataByEmail($email);
        return $data ? $data['nidn_or_nuptk'] : null;
    }

    /**
     * Ambil userid (untuk nip), nidn, dan nidn_or_nuptk berdasarkan email: SSO userid/{email} → i-sdm biodataumum/{userid}.
     *
     * @param  string  $email
     * @return array|null  ['userid' => ..., 'nidn' => ..., 'nidn_or_nuptk' => ...] atau null
     */
    public function getBiodataByEmail($email)
    {
        $email = trim($email);
        if ($email === '') {
            return null;
        }

        try {
            $client = new GuzzleHttpClient();
            $apiRequest = $client->request('GET', 'https://sso.unesa.ac.id/userid/' . $email);
            $auth = json_decode($apiRequest->getBody()->getContents());
        } catch (\Exception $e) {
            return null;
        }

        if (!is_array($auth) || empty($auth) || !isset($auth[0]->userid)) {
            return null;
        }

        $userid = trim($auth[0]->userid);
        if ($userid === '') {
            return null;
        }

        try {
            $isdm = $this->nip($userid);
            if (!empty($isdm) && isset($isdm[0])) {
                $row = $isdm[0];
                $nidn = $row->nidn ?? null;
                $nidnOrNuptk = $row->nidn ?? $row->nuptk ?? $row->nip ?? null;
                return [
                    'userid' => $userid,
                    'nidn' => $nidn,
                    'nidn_or_nuptk' => $nidnOrNuptk,
                ];
            }
        } catch (\Exception $e) {
            return null;
        }

        return null;
    }
}
