<?php

namespace App\Controllers;

use App\Models\MenuItemModel;

class MenuController extends BaseController
{
    public function index()
    {
        return view('pos/menu', [
            'pageTitle' => 'Menu Items',
            'items'     => (new MenuItemModel())->orderBy('category')->orderBy('name')->findAll(),
        ]);
    }

    public function create()
    {
        $model = new MenuItemModel();
        $data = [
            'name'         => $this->request->getPost('name'),
            'price'        => parse_money($this->request->getPost('price')),
            'category'     => $this->request->getPost('category') ?: null,
            'description'  => $this->request->getPost('description') ?: null,
            'is_available' => 1,
        ];

        $upload = handle_image_upload($this->request->getFile('photo'), 'menu-items');

        if ($upload['error'] !== null) {
            return redirect()->back()->withInput()->with('error', $upload['error']);
        }

        if ($upload['path'] !== null) {
            $data['photo_path'] = $upload['path'];
        }

        if (! $model->insert($data)) {
            return $this->redirectWithModelErrors($model);
        }

        $this->logActivity('pos', 'Added menu item "' . $data['name'] . '".');

        return redirect()->to('pos/menu')->with('success', 'Menu item added.');
    }

    public function edit(int $id)
    {
        $item = (new MenuItemModel())->find($id);

        if ($item === null) {
            return redirect()->to('pos/menu')->with('error', 'Menu item not found.');
        }

        return view('pos/menu_edit', [
            'pageTitle' => 'Edit Menu Item',
            'item'      => $item,
        ]);
    }

    public function update(int $id)
    {
        $model = new MenuItemModel();
        $item  = $model->find($id);

        if ($item === null) {
            return redirect()->to('pos/menu')->with('error', 'Menu item not found.');
        }

        $data = [
            'name'     => $this->request->getPost('name'),
            'price'    => parse_money($this->request->getPost('price')),
            'category' => $this->request->getPost('category') ?: null,
            'description' => $this->request->getPost('description') ?: null,
        ];

        $upload = handle_image_upload($this->request->getFile('photo'), 'menu-items');

        if ($upload['error'] !== null) {
            return redirect()->back()->withInput()->with('error', $upload['error']);
        }

        if ($upload['path'] !== null) {
            $data['photo_path'] = $upload['path'];
        }

        if (! $model->update($id, $data)) {
            return $this->redirectWithModelErrors($model);
        }

        $this->logActivity('pos', 'Updated menu item "' . $data['name'] . '".');

        return redirect()->to('pos/menu')->with('success', 'Menu item updated.');
    }

    public function toggleAvailable(int $id)
    {
        $model = new MenuItemModel();
        $item  = $model->find($id);

        if ($item === null) {
            return redirect()->to('pos/menu')->with('error', 'Menu item not found.');
        }

        $model->update($id, ['is_available' => $item->is_available ? 0 : 1]);

        return redirect()->to('pos/menu')->with('success', 'Menu item updated.');
    }

    public function delete(int $id)
    {
        $model = new MenuItemModel();
        $item  = $model->find($id);

        if ($item === null) {
            return redirect()->to('pos/menu')->with('error', 'Menu item not found.');
        }

        $model->delete($id);
        $this->logActivity('pos', 'Deleted menu item "' . $item->name . '".');

        return redirect()->to('pos/menu')->with('success', 'Menu item deleted.');
    }
}
