<?php

namespace Modules\Sale\Controllers;

use App\Controllers\BaseController;
use App\Models\CustomerGroupModel;
use Modules\Sale\Services\CustomerGroupService;

class CustomerGroupController extends BaseController
{
    protected CustomerGroupModel $groups;
    protected CustomerGroupService $service;

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

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

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

        return view('Modules\Sale\Views\customer_groups\index', array_merge($this->baseViewData(), [
            'title'   => 'Customer Groups',
            'groups'  => $groups,
            'filters' => compact('search', 'status'),
        ]));
    }

    public function createForm()
    {
        return view('Modules\Sale\Views\customer_groups\create', array_merge($this->baseViewData(), [
            'title' => 'Add Customer Group',
        ]));
    }

    public function store()
    {
        if (! $this->validate([
            'name' => 'required|min_length[2]|max_length[100]',
            'code' => 'required|max_length[20]',
        ])) {
            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('/sale/customer-groups')->with('success', 'Customer group created.');
    }

    public function editForm(int $id)
    {
        $group = $this->groups->find($id);
        if (! $group) {
            return redirect()->to('/sale/customer-groups')->with('error', 'Customer group not found.');
        }

        return view('Modules\Sale\Views\customer_groups\edit', array_merge($this->baseViewData(), [
            'title' => 'Edit Customer Group',
            'group' => $group,
        ]));
    }

    public function update(int $id)
    {
        if (! $this->validate([
            'name' => 'required|min_length[2]|max_length[100]',
            'code' => 'required|max_length[20]',
        ])) {
            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('/sale/customer-groups')->with('success', 'Customer group updated.');
    }

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

        return redirect()->back()->with('success', 'Customer group activated.');
    }

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

        return redirect()->back()->with('success', 'Customer group deactivated.');
    }

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

        return redirect()->to('/sale/customer-groups')->with('success', 'Customer group deleted.');
    }
}
