<?php

declare(strict_types=1);

namespace App\Controllers\Api\Admin;

use CodeIgniter\HTTP\ResponseInterface;

/**
 * Panel Ion Auth: daftar pengguna admin, grup, buat user, reset password.
 */
class PanelUsersApiController extends BaseAdminApiController
{
    public function users(): ResponseInterface
    {
        $auth = $this->requireAdminApiAccess('panel');
        if ($auth['response'] !== null) {
            return $auth['response'];
        }
        $this->auditAuthorized('api.admin.panel.users.list', $auth['actor'], ['request' => $this->auditRequestParams()]);

        return $this->respond($this->adminExtra->adminUsersList());
    }

    public function userShow(?string $id = null): ResponseInterface
    {
        $auth = $this->requireAdminApiAccess('panel');
        if ($auth['response'] !== null) {
            return $auth['response'];
        }
        $idInt = (int) ($id ?? 0);
        if ($idInt <= 0) {
            return $this->respond(['status' => 0, 'pesan' => 'ID tidak valid'], 400);
        }
        $this->auditAuthorized('api.admin.panel.users.show', $auth['actor'], [
            'request' => $this->auditRequestParams(),
            'user_id' => $idInt,
        ]);

        return $this->respond($this->adminExtra->adminUserShow($idInt));
    }

    public function userUpdate(int $id): ResponseInterface
    {
        $auth = $this->requireAdminApiAccess('panel');
        if ($auth['response'] !== null) {
            return $auth['response'];
        }
        if ($id <= 0) {
            return $this->respond(['status' => 0, 'pesan' => 'ID tidak valid'], 400);
        }
        $this->auditAuthorized('api.admin.panel.users.update', $auth['actor'], [
            'user_id' => $id,
            'request' => $this->auditRequestParams(),
        ]);

        return $this->respond($this->adminExtra->adminUserUpdate($id, $this->request->getPost()));
    }

    public function groups(): ResponseInterface
    {
        $auth = $this->requireAdminApiAccess('panel');
        if ($auth['response'] !== null) {
            return $auth['response'];
        }
        $this->auditAuthorized('api.admin.panel.groups.list', $auth['actor'], ['request' => $this->auditRequestParams()]);

        return $this->respond($this->adminExtra->adminGroupsList());
    }

    public function groupCreate(): ResponseInterface
    {
        $auth = $this->requireAdminApiAccess('panel');
        if ($auth['response'] !== null) {
            return $auth['response'];
        }
        $this->auditAuthorized('api.admin.panel.groups.create', $auth['actor'], ['request' => $this->auditRequestParams()]);

        return $this->respond($this->adminExtra->adminGroupCreate($this->request->getPost()));
    }

    public function groupUpdate(int $id): ResponseInterface
    {
        $auth = $this->requireAdminApiAccess('panel');
        if ($auth['response'] !== null) {
            return $auth['response'];
        }
        if ($id <= 0) {
            return $this->respond(['status' => 0, 'pesan' => 'ID grup tidak valid'], 400);
        }
        $this->auditAuthorized('api.admin.panel.groups.update', $auth['actor'], [
            'group_id' => $id,
            'request'  => $this->auditRequestParams(),
        ]);

        return $this->respond($this->adminExtra->adminGroupUpdate($id, $this->request->getPost()));
    }

    public function groupDelete(int $id): ResponseInterface
    {
        $auth = $this->requireAdminApiAccess('panel');
        if ($auth['response'] !== null) {
            return $auth['response'];
        }
        if ($id <= 0) {
            return $this->respond(['status' => 0, 'pesan' => 'ID grup tidak valid'], 400);
        }
        $this->auditAuthorized('api.admin.panel.groups.delete', $auth['actor'], ['group_id' => $id]);

        return $this->respond($this->adminExtra->adminGroupDelete($id));
    }

    public function userCreate(): ResponseInterface
    {
        $auth = $this->requireAdminApiAccess('panel');
        if ($auth['response'] !== null) {
            return $auth['response'];
        }
        $this->auditAuthorized('api.admin.panel.users.create', $auth['actor'], [
            'request' => $this->auditRequestParams(),
        ]);

        return $this->respond($this->adminExtra->adminUserCreate($this->request->getPost()));
    }

    public function userResetPassword(int $id): ResponseInterface
    {
        $auth = $this->requireAdminApiAccess('panel');
        if ($auth['response'] !== null) {
            return $auth['response'];
        }
        $this->auditAuthorized('api.admin.panel.users.reset_password', $auth['actor'], [
            'user_id' => $id,
            'request' => $this->auditRequestParams(),
        ]);

        return $this->respond($this->adminExtra->adminUserResetPassword($id, $this->request->getPost()));
    }
}
