<?php

namespace App\Controllers\Result;

use App\Models\ValueSet\ValueSetModel;
use App\Traits\ResponseTrait;

class ResultValueSetController extends \CodeIgniter\Controller
{
    use ResponseTrait;

    protected $dbModel;

    public function __construct()
    {
        $this->dbModel = new ValueSetModel();
    }

    public function index()
    {
        $search = $this->request->getGet('search') ?? $this->request->getGet('param') ?? null;
        $VSetID = $this->request->getGet('VSetID') ?? null;

        $rows = $this->dbModel->getValueSets($search, $VSetID);

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

    public function show($id = null)
    {
        $row = $this->dbModel->getValueSet($id);
        if (!$row) {
            return $this->failNotFound("ValueSet item not found: $id");
        }

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

    public function create()
    {
        $input = $this->request->getJSON(true);
        if (!$input) {
            return $this->failValidationErrors(['Invalid JSON input']);
        }

        $data = [
            'SiteID' => $input['SiteID'] ?? 1,
            'VSetID' => $input['VSetID'] ?? null,
            'VOrder' => $input['VOrder'] ?? 0,
            'VValue' => $input['VValue'] ?? '',
            'VDesc' => $input['VDesc'] ?? '',
            'VCategory' => $input['VCategory'] ?? null
        ];

        if ($data['VSetID'] === null) {
            return $this->failValidationErrors(['VSetID is required']);
        }

        try {
            $id = $this->dbModel->insert($data, true);
            if (!$id) {
                return $this->failValidationErrors($this->dbModel->errors());
            }

            $newRow = $this->dbModel->getValueSet($id);
            return $this->respondCreated([
                'status' => 'success',
                'message' => 'ValueSet item created',
                'data' => $newRow
            ]);
        } catch (\Exception $e) {
            return $this->failServerError('Failed to create: ' . $e->getMessage());
        }
    }

    public function update($id = null)
    {
        $input = $this->request->getJSON(true);
        if (!$input) {
            return $this->failValidationErrors(['Invalid JSON input']);
        }

        $existing = $this->dbModel->getValueSet($id);
        if (!$existing) {
            return $this->failNotFound("ValueSet item not found: $id");
        }

        $data = [];
        if (isset($input['VSetID'])) $data['VSetID'] = $input['VSetID'];
        if (isset($input['VOrder'])) $data['VOrder'] = $input['VOrder'];
        if (isset($input['VValue'])) $data['VValue'] = $input['VValue'];
        if (isset($input['VDesc'])) $data['VDesc'] = $input['VDesc'];
        if (isset($input['SiteID'])) $data['SiteID'] = $input['SiteID'];
        if (isset($input['VCategory'])) $data['VCategory'] = $input['VCategory'];

        if (empty($data)) {
            return $this->respond([
                'status' => 'success',
                'message' => 'No changes to update',
                'data' => $existing
            ], 200);
        }

        try {
            $updated = $this->dbModel->update($id, $data);
            if (!$updated) {
                return $this->failValidationErrors($this->dbModel->errors());
            }

            $newRow = $this->dbModel->getValueSet($id);
            return $this->respond([
                'status' => 'success',
                'message' => 'ValueSet item updated',
                'data' => $newRow
            ], 200);
        } catch (\Exception $e) {
            return $this->failServerError('Failed to update: ' . $e->getMessage());
        }
    }

    public function delete($id = null)
    {
        $existing = $this->dbModel->getValueSet($id);
        if (!$existing) {
            return $this->failNotFound("ValueSet item not found: $id");
        }

        try {
            $this->dbModel->delete($id);
            return $this->respond([
                'status' => 'success',
                'message' => 'ValueSet item deleted'
            ], 200);
        } catch (\Exception $e) {
            return $this->failServerError('Failed to delete: ' . $e->getMessage());
        }
    }
}
