<?php

namespace Modules\Sale\Controllers;

use App\Controllers\BaseController;
use App\Models\QuotationTemplateModel;
use Modules\Sale\Services\QuotationTemplateService;

class QuotationTemplateController extends BaseController
{
    protected QuotationTemplateModel $templates;
    protected QuotationTemplateService $service;

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

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

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

        return view('Modules\Sale\Views\quotation_templates\index', array_merge($this->baseViewData(), [
            'title'     => 'Quotation Templates',
            'templates' => $templates,
            'filters'   => compact('search', 'status'),
        ]));
    }

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

    public function store()
    {
        if (! $this->validate(['name' => 'required|min_length[2]|max_length[150]'])) {
            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/quotation-templates')->with('success', 'Template created.');
    }

    public function editForm(int $id)
    {
        $template = $this->templates->find($id);
        if (! $template) {
            return redirect()->to('/sale/quotation-templates')->with('error', 'Template not found.');
        }

        return view('Modules\Sale\Views\quotation_templates\edit', array_merge($this->baseViewData(), [
            'title'    => 'Edit Quotation Template',
            'template' => $template,
        ]));
    }

    public function update(int $id)
    {
        if (! $this->validate(['name' => 'required|min_length[2]|max_length[150]'])) {
            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/quotation-templates')->with('success', 'Template updated.');
    }

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

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

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

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

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

        return redirect()->to('/sale/quotation-templates')->with('success', 'Template deleted.');
    }
}
