<?php

namespace App\Controllers\Admin;

use App\Controllers\BaseController;
use App\Models\GasKitAddonModel;

class GasKitAddons extends BaseController
{
    public function index()
    {
        $builder = model(GasKitAddonModel::class)->orderBy('sort_order', 'asc')->orderBy('id', 'asc');
        $q = $this->request->getGet('q');
        if ($q !== null && $q !== '') {
            $q = trim($q);
            $builder->like('name', $q);
        }
        $addons  = $builder->findAll();
        $content = view('admin/gas_kit_addons_list', [
            'addons' => $addons,
            'filter_q' => $q ?? '',
        ]);
        return view('admin/layout', ['title' => 'Gas kit add-ons', 'content' => $content]);
    }

    public function add()
    {
        $name  = trim((string) $this->request->getPost('name'));
        $price = (float) $this->request->getPost('price');
        $sort  = (int) $this->request->getPost('sort_order');
        if ($name !== '') {
            model(GasKitAddonModel::class)->insert([
                'name'       => $name,
                'price'      => $price >= 0 ? $price : 0,
                'sort_order' => $sort,
            ]);
        }
        return redirect()->to(base_url('admin/gas-kit-addons'))->with('message', 'Add-on added.');
    }

    public function edit(int $id)
    {
        $addon = model(GasKitAddonModel::class)->find($id);
        if (! $addon) {
            return redirect()->to(base_url('admin/gas-kit-addons'))->with('error', 'Add-on not found.');
        }
        $content = view('admin/gas_kit_addon_edit', ['addon' => $addon]);
        return view('admin/layout', ['title' => 'Edit gas kit add-on', 'content' => $content]);
    }

    /**
     * POST-only: save edits then redirect to edit page. Stops redirect-from-POST turning into GET.
     */
    public function update(int $id)
    {
        $addon = model(GasKitAddonModel::class)->find($id);
        if (! $addon) {
            return redirect()->to(base_url('admin/gas-kit-addons'))->with('error', 'Add-on not found.');
        }
        $name  = trim((string) $this->request->getPost('name'));
        $price = (float) $this->request->getPost('price');
        $sort  = (int) $this->request->getPost('sort_order');
        if ($name === '') {
            return redirect()->to(base_url('admin/gas-kit-addons/edit/' . $id))->withInput()->with('error', 'Name required.');
        }
        $db = \Config\Database::connect();
        $db->table('gas_kit_addons')->where('id', $id)->update([
            'name'       => $name,
            'price'      => $price >= 0 ? $price : 0,
            'sort_order' => $sort,
            'updated_at' => date('Y-m-d H:i:s'),
        ]);
        return redirect()->to(base_url('admin/gas-kit-addons/edit/' . $id))->with('message', 'Add-on updated.');
    }

    public function delete(int $id)
    {
        model(GasKitAddonModel::class)->delete($id);
        return redirect()->to(base_url('admin/gas-kit-addons'))->with('message', 'Add-on deleted.');
    }
}
