<?php

namespace Modules\Inventory\Controllers;

use App\Controllers\BaseController;
use App\Models\ProductBrandModel;
use Modules\Inventory\Services\BrandService;

class BrandController extends BaseController
{
    protected ProductBrandModel $brands;
    protected BrandService $service;

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

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

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

        return view('Modules\Inventory\Views\brands\index', array_merge($this->baseViewData(), [
            'title'   => 'Brands',
            'brands'  => $brands,
            'filters' => compact('search', 'status'),
        ]));
    }

    public function createForm()
    {
        return view('Modules\Inventory\Views\brands\create', array_merge($this->baseViewData(), [
            'title' => 'Add Brand',
        ]));
    }

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

    public function editForm(int $id)
    {
        $brand = $this->brands->find($id);
        if (! $brand) {
            return redirect()->to('/inventory/brands')->with('error', 'Brand not found.');
        }

        return view('Modules\Inventory\Views\brands\edit', array_merge($this->baseViewData(), [
            'title' => 'Edit Brand',
            'brand' => $brand,
        ]));
    }

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

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

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

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

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

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

        return redirect()->to('/inventory/brands')->with('success', 'Brand deleted.');
    }
}
