<?php

declare(strict_types=1);

namespace App\Controllers\Api\Admin;

use CodeIgniter\HTTP\ResponseInterface;

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

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

        $status  = (string) ($this->request->getGet('status') ?? 'Waiting');
        $page    = max(1, (int) ($this->request->getGet('page') ?? 1));
        $perPage = max(5, min(200, (int) ($this->request->getGet('per_page') ?? 30)));

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

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

        $this->auditAuthorized('api.admin.cuti.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->cutiShow($idInt, $cb['kid']));
    }

    public function approve(): ResponseInterface
    {
        $auth = $this->requireAdminApiAccess('cuti');
        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_cuti') ?? 0);
        if ($id <= 0) {
            return $this->respond(['status' => 0, 'pesan' => 'id_cuti wajib'], 400);
        }

        $this->auditAuthorized('api.admin.cuti.approve', $auth['actor'], [
            'cuti' => ['id_cuti' => $id],
        ]);

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

    public function reject(): ResponseInterface
    {
        $auth = $this->requireAdminApiAccess('cuti');
        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_cuti') ?? 0);
        if ($id <= 0) {
            return $this->respond(['status' => 0, 'pesan' => 'id_cuti wajib'], 400);
        }

        $alasan = (string) ($this->request->getPost('alasan_tolak') ?? '');

        $this->auditAuthorized('api.admin.cuti.reject', $auth['actor'], [
            'cuti' => [
                'id_cuti'       => $id,
                'alasan_tolak'  => function_exists('mb_substr') ? mb_substr($alasan, 0, 500) : substr($alasan, 0, 500),
            ],
        ]);

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