<?php
namespace App\Controllers\Organization;

use App\Traits\PatchValidationTrait;
use App\Traits\ResponseTrait;
use App\Controllers\BaseController;

use App\Models\Organization\DisciplineModel;

class DisciplineController extends BaseController {
  use ResponseTrait;
  use PatchValidationTrait;

  protected $db;
  protected $model;

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

  public function index() {
    $filter = [
			'DisciplineCode' => $this->request->getVar('DisciplineCode'),
			'DisciplineName' => $this->request->getVar('DisciplineName'),
		];
		$rows = $this->model->getDisciplines($filter);
    
    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($DisciplineID = null) {
    $row = $this->model->where('DisciplineID', $DisciplineID)->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 = $this->requirePatchId($input['DisciplineID'] ?? null, 'DisciplineID');
      if ($id === null) {
        return;
      }
      $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);

    $validation = service('validation');
    $validation->setRules([
      'DisciplineCode' => 'required|string|max_length[50]',
      'DisciplineName' => 'required|string|max_length[255]',
      'Parent' => 'permit_empty|integer',
      'SeqScr' => 'permit_empty|integer',
      'SeqRpt' => 'permit_empty|integer',
    ]);

    if (!$validation->run($input)) {
      return $this->failValidationErrors($validation->getErrors());
    }

    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($DisciplineID = null) {
    $input = $this->requirePatchPayload($this->request->getJSON(true));
    if ($input === null) {
      return;
    }

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

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

    $validation = service('validation');
    $validation->setRules([
      'DisciplineCode' => 'permit_empty|string|max_length[50]',
      'DisciplineName' => 'permit_empty|string|max_length[255]',
      'Parent' => 'permit_empty|integer',
      'SeqScr' => 'permit_empty|integer',
      'SeqRpt' => 'permit_empty|integer',
    ]);

    if (!$validation->run($input)) {
      return $this->failValidationErrors($validation->getErrors());
    }

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