<?php

namespace App\Controllers\Organization;

use App\Traits\PatchValidationTrait;
use App\Traits\ResponseTrait;
use App\Controllers\BaseController;
use App\Models\Organization\CodingSysModel;

class CodingSysController extends BaseController {
    use ResponseTrait;
    use PatchValidationTrait;

    protected $db;
    protected $model;

    public function __construct() {
        $this->db = \Config\Database::connect();
        $this->model = new CodingSysModel();
    }

    public function index() {
        $filter = [
            'CodingSysAbb' => $this->request->getVar('CodingSysAbb'),
            'FullText' => $this->request->getVar('FullText'),
        ];

        $builder = $this->model;

        if (!empty($filter['CodingSysAbb'])) {
            $builder->like('CodingSysAbb', $filter['CodingSysAbb'], 'both');
        }
        if (!empty($filter['FullText'])) {
            $builder->like('FullText', $filter['FullText'], 'both');
        }

        $rows = $builder->findAll();

        if (empty($rows)) {
            return $this->respond(['status' => 'success', 'message' => 'no Data.', 'data' => []], 200);
        }

        return $this->respond(['status' => 'success', 'message' => 'fetch success', 'data' => $rows], 200);
    }

    public function show($CodingSysID = null) {
        $row = $this->model->where('CodingSysID', $CodingSysID)->first();

        if (empty($row)) {
            return $this->respond(['status' => 'success', 'message' => 'no Data.', 'data' => null], 200);
        }

        return $this->respond(['status' => 'success', 'message' => 'fetch success', 'data' => $row], 200);
    }

    public function delete() {
        try {
            $input = $this->request->getJSON(true);
            $id = $input['CodingSysID'] ?? null;

            if (!$id) {
                return $this->failValidationErrors('CodingSysID is required.');
            }

            $this->model->delete($id);
            return $this->respondDeleted(['status' => 'success', 'message' => "{$id} deleted successfully."]);
        } catch (\Throwable $e) {
            return $this->failServerError('Something went wrong: ' . $e->getMessage());
        }
    }

    public function create() {
        $input = $this->request->getJSON(true);

        try {
            $id = $this->model->insert($input, true);
            return $this->respondCreated(['status' => 'success', 'message' => 'data created successfully', 'data' => $id], 201);
        } catch (\Throwable $e) {
            return $this->failServerError('Something went wrong: ' . $e->getMessage());
        }
    }

    public function update($CodingSysID = null) {
        $input = $this->requirePatchPayload($this->request->getJSON(true));
        if ($input === null) {
            return;
        }

        $id = $this->requirePatchId($CodingSysID, 'CodingSysID');
        if ($id === null) {
            return;
        }

        $existing = $this->model->find($id);
        if (!$existing) {
            return $this->respond(['status' => 'failed', 'message' => 'CodingSys not found', 'data' => []], 404);
        }

        if (isset($input['CodingSysID']) && (string) $input['CodingSysID'] !== (string) $id) {
            return $this->failValidationErrors('CodingSysID in URL does not match body.');
        }

        $input['CodingSysID'] = $id;

        try {
            $this->model->update($id, $input);
            return $this->respondCreated(['status' => 'success', 'message' => 'data updated successfully', 'data' => $id], 201);
        } catch (\Throwable $e) {
            return $this->failServerError('Something went wrong: ' . $e->getMessage());
        }
    }
}
