<?php

namespace App\Controllers\Infrastructure;

use App\Controllers\BaseController;
use App\Traits\ResponseTrait;
use App\Models\Infrastructure\EquipmentListModel;

class EquipmentListController extends BaseController {
    use ResponseTrait;

    protected $db;
    protected $model;

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

    public function index() {
        $filter = [
            'IEID' => $this->request->getVar('IEID'),
            'InstrumentName' => $this->request->getVar('InstrumentName'),
            'DepartmentID' => $this->request->getVar('DepartmentID'),
            'WorkstationID' => $this->request->getVar('WorkstationID'),
            'isEnable' => $this->request->getVar('isEnable'),
        ];

        $rows = $this->model->getEquipmentLists($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($EID = null) {
        $row = $this->model->getEquipmentList($EID);

        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 create() {
        $input = $this->request->getJSON(true);

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

    public function update($EID = null) {
        $input = $this->request->getJSON(true);

        try {
            if (!$EID || !ctype_digit((string) $EID)) {
                return $this->failValidationErrors('EID is required.');
            }
            $input['EID'] = (int) $EID;
            $this->model->update($EID, $input);
            return $this->respond([
                'status' => 'success',
                'message' => 'data updated successfully',
                'data' => $EID
            ], 200);
        } catch (\Throwable $e) {
            return $this->failServerError('Something went wrong: ' . $e->getMessage());
        }
    }

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

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

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