<?php

declare(strict_types=1);

namespace App\Controllers\Api\Admin;

use CodeIgniter\HTTP\ResponseInterface;

/**
 * Presensi: tugas luar, lembur, libur, jadwal, aktivitas harian.
 */
class PresensiToolsApiController extends BaseAdminApiController
{
    public function dilapangan(): ResponseInterface
    {
        $auth = $this->requireAdminApiAccess('presensi');
        if ($auth['response'] !== null) {
            return $auth['response'];
        }
        $cb = $this->cabangKantorAfterAuth($auth['actor']);
        if ($cb['response'] !== null) {
            return $cb['response'];
        }
        $this->auditAuthorized('api.admin.presensi.dilapangan.list', $auth['actor'], ['request' => $this->auditRequestParams()]);

        return $this->respond($this->adminExtra->dilapanganList($cb['kid']));
    }

    public function dilapanganSave(): ResponseInterface
    {
        $auth = $this->requireAdminApiAccess('presensi');
        if ($auth['response'] !== null) {
            return $auth['response'];
        }
        $cb = $this->cabangKantorAfterAuth($auth['actor']);
        if ($cb['response'] !== null) {
            return $cb['response'];
        }
        $post = $this->request->getPost();
        $id   = (int) ($post['id_dilapangan'] ?? 0);
        $this->auditAuthorized('api.admin.presensi.dilapangan.save', $auth['actor'], ['id' => $id ?: null]);

        return $this->respond($this->adminExtra->dilapanganSave($post, $id > 0 ? $id : null, $cb['kid']));
    }

    public function dilapanganDelete(int $id): ResponseInterface
    {
        $auth = $this->requireAdminApiAccess('presensi');
        if ($auth['response'] !== null) {
            return $auth['response'];
        }
        $cb = $this->cabangKantorAfterAuth($auth['actor']);
        if ($cb['response'] !== null) {
            return $cb['response'];
        }
        $this->auditAuthorized('api.admin.presensi.dilapangan.delete', $auth['actor'], ['id' => $id]);

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

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

        return $this->respond($this->adminExtra->lemburList($cb['kid']));
    }

    public function lemburSave(): ResponseInterface
    {
        $auth = $this->requireAdminApiAccess('presensi');
        if ($auth['response'] !== null) {
            return $auth['response'];
        }
        $cb = $this->cabangKantorAfterAuth($auth['actor']);
        if ($cb['response'] !== null) {
            return $cb['response'];
        }
        $post = $this->request->getPost();
        $id   = (int) ($post['id_lembur'] ?? 0);
        $this->auditAuthorized('api.admin.presensi.lembur.save', $auth['actor'], ['id' => $id ?: null]);

        return $this->respond($this->adminExtra->lemburSave($post, $id > 0 ? $id : null, $cb['kid']));
    }

    public function lemburDelete(int $id): ResponseInterface
    {
        $auth = $this->requireAdminApiAccess('presensi');
        if ($auth['response'] !== null) {
            return $auth['response'];
        }
        $cb = $this->cabangKantorAfterAuth($auth['actor']);
        if ($cb['response'] !== null) {
            return $cb['response'];
        }
        $this->auditAuthorized('api.admin.presensi.lembur.delete', $auth['actor'], ['id' => $id]);

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

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

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

    public function liburSave(): ResponseInterface
    {
        $auth = $this->requireAdminApiAccess('presensi_libur');
        if ($auth['response'] !== null) {
            return $auth['response'];
        }
        $post = $this->request->getPost();
        $id   = (int) ($post['id_libur'] ?? 0);
        $this->auditAuthorized('api.admin.presensi.libur.save', $auth['actor'], ['id' => $id ?: null]);

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

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

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

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

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

    public function jadwalSave(): ResponseInterface
    {
        $auth = $this->requireAdminApiAccess('presensi_jadwal');
        if ($auth['response'] !== null) {
            return $auth['response'];
        }
        $post = $this->request->getPost();
        $id   = (int) ($post['id_jadwal'] ?? 0);
        $this->auditAuthorized('api.admin.presensi.jadwal.save', $auth['actor'], ['id' => $id ?: null]);

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

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

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

    public function aktivitas(): ResponseInterface
    {
        $auth = $this->requireAdminApiAccess('presensi');
        if ($auth['response'] !== null) {
            return $auth['response'];
        }
        $cb = $this->cabangKantorAfterAuth($auth['actor']);
        if ($cb['response'] !== null) {
            return $cb['response'];
        }
        $page    = max(1, (int) ($this->request->getGet('page') ?? 1));
        $perPage = max(5, min(100, (int) ($this->request->getGet('per_page') ?? 20)));
        $this->auditAuthorized('api.admin.presensi.aktivitas.list', $auth['actor'], ['request' => $this->auditRequestParams()]);

        return $this->respond($this->adminExtra->aktifitasList($page, $perPage, $cb['kid']));
    }

    public function aktivitasDelete(int $id): ResponseInterface
    {
        $auth = $this->requireAdminApiAccess('presensi');
        if ($auth['response'] !== null) {
            return $auth['response'];
        }
        $cb = $this->cabangKantorAfterAuth($auth['actor']);
        if ($cb['response'] !== null) {
            return $cb['response'];
        }
        $this->auditAuthorized('api.admin.presensi.aktivitas.delete', $auth['actor'], ['id' => $id]);

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