<?php

declare(strict_types=1);

namespace App\Controllers\Api\Admin;

use CodeIgniter\HTTP\ResponseInterface;

class PegawaiController extends BaseAdminApiController
{
    public function index(): ResponseInterface
    {
        $auth = $this->requireAdminApiAccess('pegawai');
        if ($auth['response'] !== null) {
            return $auth['response'];
        }
        $cb = $this->cabangKantorAfterAuth($auth['actor']);
        if ($cb['response'] !== null) {
            return $cb['response'];
        }

        $this->auditAuthorized('api.admin.pegawai.index', $auth['actor'], [
            'request' => $this->auditRequestParams(),
        ]);

        $page    = (int) ($this->request->getGet('page') ?? 1);
        $perPage = (int) ($this->request->getGet('per_page') ?? 20);
        $search  = (string) ($this->request->getGet('q') ?? '');

        return $this->respond($this->adminApi->pegawaiList($page, $perPage, $search, $cb['kid']));
    }

    public function show(?string $id = null): ResponseInterface
    {
        $auth = $this->requireAdminApiAccess('pegawai');
        if ($auth['response'] !== null) {
            return $auth['response'];
        }
        $cb = $this->cabangKantorAfterAuth($auth['actor']);
        if ($cb['response'] !== null) {
            return $cb['response'];
        }

        $this->auditAuthorized('api.admin.pegawai.show', $auth['actor'], [
            'request' => $this->auditRequestParams(),
            'id'      => $id,
        ]);

        $idInt = (int) ($id ?? 0);
        if ($idInt <= 0) {
            return $this->respond(['status' => 0, 'pesan' => 'ID tidak valid'], 400);
        }

        return $this->respond($this->adminApi->pegawaiShow($idInt, $cb['kid']));
    }

    public function create(): ResponseInterface
    {
        $auth = $this->requireAdminApiAccess('pegawai_tambah');
        if ($auth['response'] !== null) {
            return $auth['response'];
        }
        $cb = $this->cabangKantorAfterAuth($auth['actor']);
        if ($cb['response'] !== null) {
            return $cb['response'];
        }

        $input = $this->normalizePegawaiInput($this->request->getPost());

        $this->auditAuthorized('api.admin.pegawai.create', $auth['actor'], [
            'pegawai' => $input,
        ]);

        return $this->respond($this->adminApi->pegawaiCreate($input, $cb['kid']));
    }

    public function update(): ResponseInterface
    {
        $auth = $this->requireAdminApiAccess('pegawai');
        if ($auth['response'] !== null) {
            return $auth['response'];
        }
        $cb = $this->cabangKantorAfterAuth($auth['actor']);
        if ($cb['response'] !== null) {
            return $cb['response'];
        }

        $id = (int) ($this->request->getPost('id_pegawai') ?? 0);
        if ($id <= 0) {
            return $this->respond(['status' => 0, 'pesan' => 'id_pegawai wajib'], 400);
        }

        $input = $this->normalizePegawaiInput($this->request->getPost());

        $this->auditAuthorized('api.admin.pegawai.update', $auth['actor'], [
            'pegawai' => array_merge(['id_pegawai' => $id], $input),
        ]);

        return $this->respond($this->adminApi->pegawaiUpdate($id, $input, $cb['kid']));
    }

    public function delete(): ResponseInterface
    {
        $auth = $this->requireAdminApiAccess('pegawai');
        if ($auth['response'] !== null) {
            return $auth['response'];
        }
        $cb = $this->cabangKantorAfterAuth($auth['actor']);
        if ($cb['response'] !== null) {
            return $cb['response'];
        }

        $id = (int) ($this->request->getPost('id_pegawai') ?? 0);
        if ($id <= 0) {
            return $this->respond(['status' => 0, 'pesan' => 'id_pegawai wajib'], 400);
        }

        $this->auditAuthorized('api.admin.pegawai.delete', $auth['actor'], [
            'pegawai' => ['id_pegawai' => $id],
        ]);

        return $this->respond($this->adminApi->pegawaiDelete($id, $cb['kid']));
    }

    public function resetPassword(): ResponseInterface
    {
        $auth = $this->requireAdminApiAccess('pegawai');
        if ($auth['response'] !== null) {
            return $auth['response'];
        }
        $cb = $this->cabangKantorAfterAuth($auth['actor']);
        if ($cb['response'] !== null) {
            return $cb['response'];
        }

        $id = (int) ($this->request->getPost('id_pegawai') ?? 0);
        if ($id <= 0) {
            return $this->respond(['status' => 0, 'pesan' => 'id_pegawai wajib'], 400);
        }

        $this->auditAuthorized('api.admin.pegawai.reset_password', $auth['actor'], [
            'pegawai' => ['id_pegawai' => $id],
        ]);

        return $this->respond($this->adminApi->pegawaiResetPassword($id, $cb['kid']));
    }

    /**
     * @param array<string, mixed> $post
     *
     * @return array<string, scalar|null>
     */
    private function normalizePegawaiInput(array $post): array
    {
        $out = [];
        foreach ($post as $k => $v) {
            if (is_scalar($v) || $v === null) {
                $out[(string) $k] = $v;
            }
        }

        return $out;
    }
}
