<?php

namespace Modules\Assets\Controllers;

use App\Controllers\BaseController;
use App\Models\AssetTypeModel;
use Modules\Assets\Services\AssetTypeService;

class AssetTypeController extends BaseController
{
    protected AssetTypeModel $types;
    protected AssetTypeService $service;

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

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

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

        return view('Modules\Assets\Views\types\index', array_merge($this->baseViewData(), [
            'title'   => 'Asset Types',
            'types'   => $types,
            'filters' => compact('search', 'status', 'category'),
        ]));
    }

    public function createForm()
    {
        return view('Modules\Assets\Views\types\create', array_merge($this->baseViewData(), [
            'title' => 'Add Asset Type',
        ]));
    }

    public function store()
    {
        if (! $this->validate([
            'name' => 'required|min_length[2]|max_length[100]',
            'code' => 'required|max_length[10]',
        ])) {
            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('/assets/types')->with('success', 'Asset type created.');
    }

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

        return view('Modules\Assets\Views\types\edit', array_merge($this->baseViewData(), [
            'title' => 'Edit Asset Type',
            'type'  => $type,
        ]));
    }

    public function update(int $id)
    {
        if (! $this->validate([
            'name' => 'required|min_length[2]|max_length[100]',
            'code' => 'required|max_length[10]',
        ])) {
            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('/assets/types')->with('success', 'Asset type updated.');
    }

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

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

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

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

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

        return redirect()->to('/assets/types')->with('success', 'Asset type deleted.');
    }
}
