<?php

namespace Modules\Leave\Controllers;

use App\Controllers\BaseController;
use App\Models\LeaveTypeModel;
use Modules\Leave\Services\LeaveTypeService;

class LeaveTypeController extends BaseController
{
    protected LeaveTypeModel $types;
    protected LeaveTypeService $service;

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

    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\Leave\Views\types\index', array_merge($this->baseViewData(), [
            'title'   => 'Leave Types',
            'types'   => $types,
            'filters' => compact('search', 'status'),
        ]));
    }

    public function createForm()
    {
        return view('Modules\Leave\Views\types\create', array_merge($this->baseViewData(), [
            'title' => 'Add Leave 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('/leave/types')->with('success', 'Leave type created.');
    }

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

        return view('Modules\Leave\Views\types\edit', array_merge($this->baseViewData(), [
            'title' => 'Edit Leave 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('/leave/types')->with('success', 'Leave type updated.');
    }

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

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

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

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

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

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