<?php

namespace App\Controllers\Api;

use App\Controllers\BaseController;
use App\Libraries\ApiParityLogger;
use App\Libraries\LegacyUtf8Encoder;
use App\Services\Mobile\MobileJsonService;
use CodeIgniter\HTTP\ResponseInterface;

/**
 * Pengganti CI3 `application/controllers/Json.php`.
 *
 * URL CI4: POST /api/mobile/{method}
 * URL CI3: POST /index.php/json/{method}
 *
 * @see docs/migration/json_api_map.md
 */
class MobileJsonController extends BaseController
{
    protected MobileJsonService $mobileJson;

    public function initController(\CodeIgniter\HTTP\RequestInterface $request, ResponseInterface $response, \Psr\Log\LoggerInterface $logger): void
    {
        parent::initController($request, $response, $logger);
        $this->mobileJson = new MobileJsonService();
        $this->response->setHeader('Access-Control-Allow-Origin', '*');

        if (ApiParityLogger::enabled()) {
            ApiParityLogger::logRequest($this->request);
        }
    }

    /**
     * @param array<string, mixed> $payload
     */
    private function respondLegacy(array $payload): ResponseInterface
    {
        $body = LegacyUtf8Encoder::utf8ize($payload);
        // CI3 memakai json_encode() default (tanpa JSON_UNESCAPED_UNICODE / PRETTY_PRINT).
        $json = json_encode($body, 0, 512);
        if ($json === false) {
            $json = '{"status":0,"pesan":"JSON_ENCODE_ERROR"}';
        }

        if (ApiParityLogger::enabled()) {
            ApiParityLogger::logResponse($json);
        }

        return $this->response->setStatusCode(200)
            ->setContentType('application/json', 'UTF-8')
            ->setBody($json);
    }

    public function login_w_token(): ResponseInterface
    {
        $token = (string) $this->request->getPost('token');

        return $this->respondLegacy($this->mobileJson->loginWToken($token));
    }

    public function login(): ResponseInterface
    {
        $user = (string) $this->request->getPost('username');
        $pass = (string) $this->request->getPost('password');

        return $this->respondLegacy($this->mobileJson->login($user, $pass));
    }

    public function profil(): ResponseInterface
    {
        $token = (string) $this->request->getPost('token');

        return $this->respondLegacy($this->mobileJson->profil($token));
    }

    public function save_cuti(): ResponseInterface
    {
        return $this->respondLegacy($this->mobileJson->saveCuti(
            (string) $this->request->getPost('token'),
            (string) $this->request->getPost('nama_photo'),
            (string) $this->request->getPost('photo'),
            (string) $this->request->getPost('tanggal'),
            (string) $this->request->getPost('alasan'),
            (string) $this->request->getPost('tipe'),
        ));
    }

    public function batalkan_cuti(): ResponseInterface
    {
        return $this->respondLegacy($this->mobileJson->batalkanCuti(
            (string) $this->request->getPost('token'),
            $this->request->getPost('id'),
        ));
    }

    public function save_aktifitas(): ResponseInterface
    {
        return $this->respondLegacy($this->mobileJson->saveAktifitas(
            (string) $this->request->getPost('token'),
            (string) $this->request->getPost('nama_photo'),
            (string) $this->request->getPost('photo'),
            (string) $this->request->getPost('tanggal'),
            (string) $this->request->getPost('deksripsi'),
        ));
    }

    public function save_masuk(): ResponseInterface
    {
        return $this->respondLegacy($this->mobileJson->saveMasuk(
            (string) $this->request->getPost('token'),
            (string) $this->request->getPost('nama_photo'),
            (string) $this->request->getPost('photo'),
            (string) $this->request->getPost('lat'),
            (string) $this->request->getPost('lng'),
            (string) $this->request->getPost('jarak'),
        ));
    }

    public function save_pulang(): ResponseInterface
    {
        return $this->respondLegacy($this->mobileJson->savePulang(
            (string) $this->request->getPost('token'),
            (string) $this->request->getPost('nama_photo'),
            (string) $this->request->getPost('photo'),
            (string) $this->request->getPost('lat'),
            (string) $this->request->getPost('lng'),
            (string) $this->request->getPost('jarak'),
        ));
    }

    public function save_istirahat(): ResponseInterface
    {
        return $this->respondLegacy($this->mobileJson->saveIstirahat(
            (string) $this->request->getPost('token'),
            (string) $this->request->getPost('mulai'),
            (string) $this->request->getPost('selesai'),
        ));
    }

    public function presensi_today(): ResponseInterface
    {
        return $this->respondLegacy($this->mobileJson->presensiToday((string) $this->request->getPost('token')));
    }

    public function presensi(): ResponseInterface
    {
        return $this->respondLegacy($this->mobileJson->presensi((string) $this->request->getPost('token')));
    }

    public function daftar_today(): ResponseInterface
    {
        return $this->respondLegacy($this->mobileJson->daftarToday((string) $this->request->getPost('token')));
    }

    public function berita(): ResponseInterface
    {
        return $this->respondLegacy($this->mobileJson->berita(
            (string) $this->request->getPost('token'),
            $this->request->getPost('dari'),
            $this->request->getPost('jumlah'),
        ));
    }

    public function cuti(): ResponseInterface
    {
        return $this->respondLegacy($this->mobileJson->cuti(
            (string) $this->request->getPost('token'),
            $this->request->getPost('dari'),
            $this->request->getPost('jumlah'),
        ));
    }

    public function lembur(): ResponseInterface
    {
        return $this->respondLegacy($this->mobileJson->lembur(
            (string) $this->request->getPost('token'),
            $this->request->getPost('dari'),
            $this->request->getPost('jumlah'),
        ));
    }

    public function libur(): ResponseInterface
    {
        return $this->respondLegacy($this->mobileJson->libur((string) $this->request->getPost('token')));
    }

    public function aktifitas(): ResponseInterface
    {
        return $this->respondLegacy($this->mobileJson->aktifitas(
            (string) $this->request->getPost('token'),
            $this->request->getPost('dari'),
            $this->request->getPost('jumlah'),
        ));
    }

    public function save_pp(): ResponseInterface
    {
        return $this->respondLegacy($this->mobileJson->savePp(
            (string) $this->request->getPost('token'),
            (string) $this->request->getPost('nama_photo'),
            (string) $this->request->getPost('photo'),
        ));
    }

    public function save_password(): ResponseInterface
    {
        return $this->respondLegacy($this->mobileJson->savePassword(
            (string) $this->request->getPost('token'),
            (string) $this->request->getPost('pass_lama'),
            (string) $this->request->getPost('pass_baru'),
        ));
    }
}
