<?php

namespace Modules\Documents\Controllers;

use App\Controllers\BaseController;
use App\Models\DocumentTypeModel;
use Modules\Documents\Services\DocumentTypeService;

class DocumentTypeController extends BaseController
{
    protected DocumentTypeModel $types;
    protected DocumentTypeService $service;

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

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

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

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

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

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

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

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

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

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

        return redirect()->to('/documents/types')->with('success', 'Document type activated.');
    }

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

        return redirect()->to('/documents/types')->with('success', 'Document type deactivated.');
    }

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

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