<?php

namespace Modules\Inventory\Controllers;

use App\Controllers\BaseController;
use App\Models\InventoryAssetModel;
use App\Models\ProductModel;
use Modules\Inventory\Services\AssetInventoryService;

class AssetInventoryController extends BaseController
{
    protected InventoryAssetModel $assets;
    protected AssetInventoryService $service;

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

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

        $assets = $this->assets
            ->scopeForList($search ?: null, $status)
            ->orderBy('inventory_assets.asset_tag', 'ASC')
            ->paginate(25);

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

    public function createForm()
    {
        return view('Modules\Inventory\Views\asset_inventory\create', array_merge($this->baseViewData(), [
            'title'    => 'Add Asset',
            'products' => (new ProductModel())->getActive(),
        ]));
    }

    public function store()
    {
        if (! $this->validate([
            'name'   => 'required|min_length[2]|max_length[200]',
            'status' => 'required|in_list[active,in_repair,disposed,lost]',
        ])) {
            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/asset-inventory')->with('success', 'Asset created.');
    }

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

        return view('Modules\Inventory\Views\asset_inventory\edit', array_merge($this->baseViewData(), [
            'title'    => 'Edit Asset',
            'asset'    => $asset,
            'products' => (new ProductModel())->getActive(),
        ]));
    }

    public function update(int $id)
    {
        if (! $this->validate([
            'name'   => 'required|min_length[2]|max_length[200]',
            'status' => 'required|in_list[active,in_repair,disposed,lost]',
        ])) {
            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/asset-inventory')->with('success', 'Asset updated.');
    }

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

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