<?php

namespace Modules\Payroll\Controllers;

use App\Controllers\BaseController;
use Modules\Payroll\Services\PayrollSettingsService;

class PayrollSettingsController extends BaseController
{
    protected PayrollSettingsService $service;

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

    public function index()
    {
        return view('Modules\Payroll\Views\settings\index', array_merge($this->baseViewData(), [
            'title'    => 'Payroll Settings',
            'settings' => $this->service->get(),
        ]));
    }

    public function update()
    {
        $result = $this->service->update($this->request->getPost(), (int) $this->session->get('user_id'));
        if (! $result['success']) {
            return redirect()->back()->withInput()->with('errors', $result['errors'] ?? []);
        }

        return redirect()->to('/payroll/settings')->with('success', 'Payroll settings updated.');
    }
}
