<?php
namespace App\Controllers;

use CodeIgniter\API\ResponseTrait;
use App\Controllers\BaseController;
use App\Models\PatVisit\PatVisitModel;
use App\Models\PatVisit\PatVisitADTModel;

class PatVisitController extends BaseController {
	use ResponseTrait;
	
	protected $model;

	public function __construct() {	
		$this->model = new PatVisitModel();
	}

	public function show($PVID = null) {
		try {
			$row = $this->model->show($PVID);
			if (empty($row)) { 
				return $this->respond([ 'status' => 'success', 'message'=> "data not found", 'data' => null ], 200); 
			} 
			return $this->respond([ 'status' => 'success', 'message'=> "data found", 'data' => $row ], 200);
		} catch (\Exception $e) {
			return $this->failServerError('Something went wrong '.$e->getMessage());
		}
	}

	public function showByPatient($InternalPID = null) {
		try {
			$rows = $this->model->showByPatient($InternalPID);
			if($rows == []) { $message = "data not found"; } 
			else { $message = "data found"; }
			return $this->respond(['status' => 'success', 'message'=> $message, 'data' => $rows ], 200);
		} catch (\Exception $e) {
			return $this->failServerError('Something went wrong '.$e->getMessage());
		}
	}

	public function update() {
		$input = $this->request->getJSON(true);
		try {
			if (!$input["InternalPVID"] || !is_numeric($input["InternalPVID"])) { return $this->respond(['status' => 'error', 'message' => 'Invalid or missing ID'], 400); }
			$data = $this->model->updatePatVisit($input);
			return $this->respond(['status'  => 'success', 'message' => 'Data updated successfully', 'data' => $data], 201);
		} catch (\Exception $e) {
			return $this->failServerError('Something went wrong: ' . $e->getMessage());
		}
	}

	public function create() {
		$input = $this->request->getJSON(true);
		try {
			$data = $this->model->createPatVisit($input);
			return $this->respond(['status'  => 'success', 'message' => 'Data created successfully', 'data' => $data], 201);
		} catch (\Exception $e) {
			return $this->failServerError('Something went wrong: ' . $e->getMessage());
		}
	}

	public function createADT() {
		$input = $this->request->getJSON(true);
		if (!$input["InternalPVID"] || !is_numeric($input["InternalPVID"])) { return $this->respond(['status' => 'error', 'message' => 'Invalid or missing ID'], 400); }
		$modelPVA = new PatVisitADTModel();
		try {
			$data = $modelPVA->insert($input, true);
			return $this->respond(['status'  => 'success', 'message' => 'Data created successfully', 'data' => $data], 201);
		} catch (\Exception $e) {
			return $this->failServerError('Something went wrong: ' . $e->getMessage());
		}
	}

	public function updateADT() {
		$input = $this->request->getJSON(true);
		if (!$input["PVADTID"] || !is_numeric($input["PVADTID"])) { return $this->respond(['status' => 'error', 'message' => 'Invalid or missing ID'], 400); }
		$modelPVA = new PatVisitADTModel();
		try {
			$data = $modelPVA->update($input['PVADTID'], $input);
			return $this->respond(['status'  => 'success', 'message' => 'Data updated successfully', 'data' => $data], 201);
		} catch (\Exception $e) {
			return $this->failServerError('Something went wrong: ' . $e->getMessage());
		}
	}
}
