<?php
namespace App\Controllers\ValueSet;

use CodeIgniter\API\ResponseTrait;
use App\Controllers\BaseController;
use App\Models\ValueSet\ValueSetModel;

class ValueSetController extends BaseController {
    use ResponseTrait;

    protected $db;
    protected $model;
    protected $rules;

    public function __construct() {
        $this->db = \Config\Database::connect();
        $this->model = new ValueSetModel;
        $this->rules = [
            'VSetID' => 'required',
            'VValue' => 'required',
        ];
    }

    public function index() {
        $param = $this->request->getVar('param');
        $VSetID = $this->request->getVar('VSetID');
        $page = $this->request->getVar('page') ?? 1;
        $limit = $this->request->getVar('limit') ?? 20;

        $result = $this->model->getValueSets($param, $page, $limit, $VSetID);
        
        return $this->respond([
            'status' => 'success',
            'message'=> "Data fetched successfully",
            'data' => $result['data'],
            'pagination' => [
                'currentPage' => (int)$page,
                'totalPages' => $result['pager']->getPageCount(),
                'totalItems' => $result['pager']->getTotal(),
                'limit' => (int)$limit
            ]
        ], 200); 
    }

    public function show($VID = null) {
        $row = $this->model->getValueSet($VID);
        if (empty($row)) { 
            return $this->respond([ 'status'  => 'success', 'message' => "ValueSet with ID $VID not found.", 'data' => null ], 200); 
        }
        return $this->respond([ 'status' => 'success', 'message'=> "Data fetched successfully", 'data'   => $row], 200);
    }

    public function showByValueSetDef($VSetID = null) {
        $rows = $this->model->getValueSetByValueSetDef($VSetID);
        if (empty($rows)) {
            return $this->respond([ 'status'  => 'success', 'message' => "ValueSet not found.", 'data' => [] ], 200);
        }
        return $this->respond([ 'status' => 'success', 'message'=> "Data fetched successfully", 'data' => $rows ], 200);
    }

    public function create() {
        $input = $this->request->getJSON(true);
        if (!$this->validateData($input, $this->rules)) { return $this->failValidationErrors($this->validator->getErrors()); }
        try {
            $VID = $this->model->insert($input);
            return $this->respondCreated([ 'status'  => 'success', 'message' => "data $VID created successfully" ]);
        } catch (\Exception $e) {
            return $this->failServerError('Something went wrong: ' . $e->getMessage());
        }
    }

    public function update() {
        $input = $this->request->getJSON(true);
        $VID = $input["VID"];
        if (!$VID) { return $this->failValidationErrors('VID is required.'); }
        if (!$this->validateData($input, $this->rules)) { return $this->failValidationErrors( $this->validator->getErrors() ); }
        try {
            $this->model->update($VID,$input);
            return $this->respondCreated([ 'status'  => 'success', 'message' => "data $VID updated successfully" ]);
        } catch (\Exception $e) {
            return $this->failServerError('Something went wrong: ' . $e->getMessage());
        }
    }

    public function delete() {
        $input = $this->request->getJSON(true);
        $VID = $input["VID"];
        if (!$VID) { return $this->failValidationErrors('VID is required.'); }
        try {
            $this->model->delete($VID);
            return $this->respondDeleted(['status' => 'success', 'message' => "Data $VID deleted successfully."]);
        } catch (\Throwable $e) {
            return $this->failServerError('Something went wrong: ' . $e->getMessage());
        }
    }

}
