<?php

declare(strict_types=1);

namespace App\Controllers\Api\Admin;

use CodeIgniter\HTTP\ResponseInterface;

/**
 * Master data perusahaan (kantor, unit kerja, golongan, jabatan, berita).
 */
class CompanyDataApiController extends BaseAdminApiController
{
    public function kantor(): ResponseInterface
    {
        $auth = $this->requireAdminApiAccess('perusahaan');
        if ($auth['response'] !== null) {
            return $auth['response'];
        }
        $this->auditAuthorized('api.admin.company.kantor.list', $auth['actor'], ['request' => $this->auditRequestParams()]);

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

    public function kantorSave(): ResponseInterface
    {
        $auth = $this->requireAdminApiAccess('perusahaan');
        if ($auth['response'] !== null) {
            return $auth['response'];
        }
        $post = $this->request->getPost();
        $id   = (int) ($post['id_kantor'] ?? 0);

        $this->auditAuthorized('api.admin.company.kantor.save', $auth['actor'], ['id_kantor' => $id ?: null]);

        return $this->respond($this->adminExtra->kantorSave($post, $id > 0 ? $id : null));
    }

    public function kantorDelete(int $id): ResponseInterface
    {
        $auth = $this->requireAdminApiAccess('perusahaan');
        if ($auth['response'] !== null) {
            return $auth['response'];
        }
        $this->auditAuthorized('api.admin.company.kantor.delete', $auth['actor'], ['id_kantor' => $id]);

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

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

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

    public function unitKerjaSave(): ResponseInterface
    {
        $auth = $this->requireAdminApiAccess('perusahaan');
        if ($auth['response'] !== null) {
            return $auth['response'];
        }
        $post = $this->request->getPost();
        $id   = (int) ($post['id_unit_kerja'] ?? 0);
        $this->auditAuthorized('api.admin.company.unit_kerja.save', $auth['actor'], ['id' => $id ?: null]);

        return $this->respond($this->adminExtra->unitKerjaSave($post, $id > 0 ? $id : null));
    }

    public function unitKerjaDelete(int $id): ResponseInterface
    {
        $auth = $this->requireAdminApiAccess('perusahaan');
        if ($auth['response'] !== null) {
            return $auth['response'];
        }
        $this->auditAuthorized('api.admin.company.unit_kerja.delete', $auth['actor'], ['id' => $id]);

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

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

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

    public function golonganSave(): ResponseInterface
    {
        $auth = $this->requireAdminApiAccess('perusahaan');
        if ($auth['response'] !== null) {
            return $auth['response'];
        }
        $post = $this->request->getPost();
        $id   = (int) ($post['id_golongan'] ?? 0);
        $this->auditAuthorized('api.admin.company.golongan.save', $auth['actor'], ['id' => $id ?: null]);

        return $this->respond($this->adminExtra->golonganSave($post, $id > 0 ? $id : null));
    }

    public function golonganDelete(int $id): ResponseInterface
    {
        $auth = $this->requireAdminApiAccess('perusahaan');
        if ($auth['response'] !== null) {
            return $auth['response'];
        }
        $this->auditAuthorized('api.admin.company.golongan.delete', $auth['actor'], ['id' => $id]);

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

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

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

    public function jabatanSave(): ResponseInterface
    {
        $auth = $this->requireAdminApiAccess('perusahaan');
        if ($auth['response'] !== null) {
            return $auth['response'];
        }
        $post = $this->request->getPost();
        $id   = (int) ($post['id_jabatan'] ?? 0);
        $this->auditAuthorized('api.admin.company.jabatan.save', $auth['actor'], ['id' => $id ?: null]);

        return $this->respond($this->adminExtra->jabatanSave($post, $id > 0 ? $id : null));
    }

    public function jabatanDelete(int $id): ResponseInterface
    {
        $auth = $this->requireAdminApiAccess('perusahaan');
        if ($auth['response'] !== null) {
            return $auth['response'];
        }
        $this->auditAuthorized('api.admin.company.jabatan.delete', $auth['actor'], ['id' => $id]);

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

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

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

    public function beritaSave(): ResponseInterface
    {
        $auth = $this->requireAdminApiAccess('perusahaan');
        if ($auth['response'] !== null) {
            return $auth['response'];
        }
        $post = $this->request->getPost();
        $id   = (int) ($post['id_berita'] ?? 0);
        $this->auditAuthorized('api.admin.company.berita.save', $auth['actor'], ['id' => $id ?: null]);

        return $this->respond($this->adminExtra->beritaSave($post, $id > 0 ? $id : null));
    }

    public function beritaDelete(int $id): ResponseInterface
    {
        $auth = $this->requireAdminApiAccess('perusahaan');
        if ($auth['response'] !== null) {
            return $auth['response'];
        }
        $this->auditAuthorized('api.admin.company.berita.delete', $auth['actor'], ['id' => $id]);

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