<?php

namespace App\Modules\Discipline\Controllers;

use App\Core\BaseApiController;
use App\Modules\Discipline\Models\ViolationModel;
use CodeIgniter\HTTP\ResponseInterface;

/**
 * Admin CRUD untuk master violations (jenis pelanggaran).
 * Akses via /api/discipline/violations-admin (hanya ADMIN).
 */
class ViolationAdminController extends BaseApiController
{
    /**
     * POST /api/discipline/violations-admin
     */
    public function create(): ResponseInterface
    {
        $payload = $this->request->getJSON(true) ?? [];
        $categoryId = (int) ($payload['category_id'] ?? 0);
        $title      = trim((string) ($payload['title'] ?? ''));
        $score      = (int) ($payload['score'] ?? 0);
        $description = $payload['description'] ?? null;
        $isActive   = array_key_exists('is_active', $payload) ? (int) (bool) $payload['is_active'] : 1;

        if ($categoryId <= 0 || $title === '') {
            return $this->errorResponse('category_id dan title wajib diisi', null, null, ResponseInterface::HTTP_UNPROCESSABLE_ENTITY);
        }

        $model = new ViolationModel();
        $model->insert([
            'category_id' => $categoryId,
            'title'       => $title,
            'description' => $description,
            'score'       => $score,
            'is_active'   => $isActive,
        ]);

        if ($model->errors()) {
            return $this->errorResponse(implode(' ', $model->errors()), $model->errors(), null, ResponseInterface::HTTP_UNPROCESSABLE_ENTITY);
        }

        return $this->successResponse(null, 'Pelanggaran berhasil ditambahkan');
    }

    /**
     * PUT /api/discipline/violations-admin/{id}
     */
    public function update(int $id): ResponseInterface
    {
        $model = new ViolationModel();
        $row   = $model->find($id);
        if (! $row) {
            return $this->errorResponse('Pelanggaran tidak ditemukan', null, null, ResponseInterface::HTTP_NOT_FOUND);
        }

        $payload = $this->request->getJSON(true) ?? [];
        $data    = [];

        if (isset($payload['category_id'])) {
            $data['category_id'] = (int) $payload['category_id'];
        }
        if (isset($payload['title'])) {
            $data['title'] = trim((string) $payload['title']);
        }
        if (isset($payload['description'])) {
            $data['description'] = $payload['description'];
        }
        if (isset($payload['score'])) {
            $data['score'] = (int) $payload['score'];
        }
        if (isset($payload['is_active'])) {
            $data['is_active'] = (int) (bool) $payload['is_active'];
        }

        if ($data === []) {
            return $this->successResponse(null, 'Tidak ada perubahan');
        }

        $model->update($id, $data);

        if ($model->errors()) {
            return $this->errorResponse(implode(' ', $model->errors()), $model->errors(), null, ResponseInterface::HTTP_UNPROCESSABLE_ENTITY);
        }

        return $this->successResponse(null, 'Pelanggaran berhasil diperbarui');
    }
}

