<?php

namespace Modules\Inventory\Controllers;

use App\Controllers\BaseController;
use App\Models\WarehouseModel;
use Modules\Inventory\Services\WarehouseService;

class WarehouseController extends BaseController
{
    protected WarehouseModel $warehouses;
    protected WarehouseService $service;

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

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

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

        return view('Modules\Inventory\Views\warehouses\index', array_merge($this->baseViewData(), [
            'title'      => 'Warehouses',
            'warehouses' => $warehouses,
            'filters'    => compact('search', 'status'),
        ]));
    }

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

    public function store()
    {
        if (! $this->validate([
            'name' => 'required|min_length[2]|max_length[150]',
            'code' => 'required|max_length[30]',
        ])) {
            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/warehouses')->with('success', 'Warehouse created.');
    }

    public function editForm(int $id)
    {
        $warehouse = $this->warehouses->find($id);
        if (! $warehouse) {
            return redirect()->to('/inventory/warehouses')->with('error', 'Warehouse not found.');
        }

        return view('Modules\Inventory\Views\warehouses\edit', array_merge($this->baseViewData(), [
            'title'     => 'Edit Warehouse',
            'warehouse' => $warehouse,
        ]));
    }

    public function update(int $id)
    {
        if (! $this->validate([
            'name' => 'required|min_length[2]|max_length[150]',
            'code' => 'required|max_length[30]',
        ])) {
            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/warehouses')->with('success', 'Warehouse updated.');
    }

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

        return redirect()->back()->with('success', 'Warehouse activated.');
    }

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

        return redirect()->back()->with('success', 'Warehouse deactivated.');
    }

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

        return redirect()->to('/inventory/warehouses')->with('success', 'Warehouse deleted.');
    }
}
