<?php

namespace Modules\Inventory\Controllers;

use App\Controllers\BaseController;
use App\Models\UnitOfMeasureModel;
use Modules\Inventory\Services\UnitOfMeasureService;

class UnitOfMeasureController extends BaseController
{
    protected UnitOfMeasureModel $units;
    protected UnitOfMeasureService $service;

    public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
    {
        parent::initController($request, $response, $logger);
        $this->units   = new UnitOfMeasureModel();
        $this->service = new UnitOfMeasureService();
    }

    public function index()
    {
        $search = trim((string) $this->request->getGet('q'));
        $status = $this->request->getGet('status') ?: null;

        $units = $this->units
            ->scopeForList($search ?: null, $status)
            ->orderBy('name', 'ASC')
            ->findAll();

        return view('Modules\Inventory\Views\units_of_measure\index', array_merge($this->baseViewData(), [
            'title'   => 'Units of Measure',
            'units'   => $units,
            'filters' => compact('search', 'status'),
        ]));
    }

    public function createForm()
    {
        return view('Modules\Inventory\Views\units_of_measure\create', array_merge($this->baseViewData(), [
            'title' => 'Add Unit of Measure',
        ]));
    }

    public function store()
    {
        if (! $this->validate([
            'name' => 'required|min_length[1]|max_length[100]',
            'code' => 'required|max_length[20]',
        ])) {
            return redirect()->back()->withInput()->with('errors', $this->validator->getErrors());
        }

        $result = $this->service->create($this->request->getPost(), (int) $this->session->get('user_id'));
        if (! $result['success']) {
            return redirect()->back()->withInput()->with('errors', $result['errors']);
        }

        return redirect()->to('/inventory/units-of-measure')->with('success', 'Unit of measure created.');
    }

    public function editForm(int $id)
    {
        $unit = $this->units->find($id);
        if (! $unit) {
            return redirect()->to('/inventory/units-of-measure')->with('error', 'Unit of measure not found.');
        }

        return view('Modules\Inventory\Views\units_of_measure\edit', array_merge($this->baseViewData(), [
            'title' => 'Edit Unit of Measure',
            'unit'  => $unit,
        ]));
    }

    public function update(int $id)
    {
        if (! $this->validate([
            'name' => 'required|min_length[1]|max_length[100]',
            'code' => 'required|max_length[20]',
        ])) {
            return redirect()->back()->withInput()->with('errors', $this->validator->getErrors());
        }

        $result = $this->service->update($id, $this->request->getPost(), (int) $this->session->get('user_id'));
        if (! $result['success']) {
            return redirect()->back()->withInput()->with('errors', $result['errors']);
        }

        return redirect()->to('/inventory/units-of-measure')->with('success', 'Unit of measure updated.');
    }

    public function activate(int $id)
    {
        $this->service->activate($id, (int) $this->session->get('user_id'));

        return redirect()->back()->with('success', 'Unit of measure activated.');
    }

    public function deactivate(int $id)
    {
        $this->service->deactivate($id, (int) $this->session->get('user_id'));

        return redirect()->back()->with('success', 'Unit of measure deactivated.');
    }

    public function destroy(int $id)
    {
        $result = $this->service->delete($id, (int) $this->session->get('user_id'));
        if (! $result['success']) {
            return redirect()->to('/inventory/units-of-measure')->with('error', $result['error']);
        }

        return redirect()->to('/inventory/units-of-measure')->with('success', 'Unit of measure deleted.');
    }
}
