<?php
/*
 * Copyright (c) 2026 AltumCode (https://altumcode.com/)
 *
 * This software is licensed exclusively by AltumCode and is sold only via https://altumcode.com/.
 * Unauthorized distribution, modification, or use of this software without a valid license is not permitted and may be subject to applicable legal actions.
 *
 * 🌍 View all other existing AltumCode projects via https://altumcode.com/
 * 📧 Get in touch for support or general queries via https://altumcode.com/contact
 * 📤 Download the latest version via https://altumcode.com/downloads
 *
 * 🐦 X/Twitter: https://x.com/AltumCode
 * 📘 Facebook: https://facebook.com/altumcode
 * 📸 Instagram: https://instagram.com/altumcode
 */

namespace Altum\Controllers;

use Altum\Alerts;
use Altum\Response;
use Altum\Title;

defined('ALTUMCODE') || die();

class Category extends Controller {

    public function index() {

        \Altum\Authentication::guard();

        $category_id = isset($this->params[0]) ? (int) $this->params[0] : null;

        if(!$category = db()->where('category_id', $category_id)->where('user_id', $this->user->user_id)->getOne('categories')) {
            redirect('dashboard');
        }

        $menu = db()->where('menu_id', $category->menu_id)->where('user_id', $this->user->user_id)->getOne('menus', ['menu_id', 'url']);

        $store = db()->where('store_id', $category->store_id)->where('user_id', $this->user->user_id)->getOne('stores', ['store_id', 'domain_id', 'url', 'currency']);

        /* Generate the store full URL base */
        $store->full_url = (new \Altum\Models\Store())->get_store_full_url($store, $this->user);

        /* Prepare the paginator */
        $total_rows = database()->query("SELECT COUNT(*) AS `total` FROM `items` WHERE `category_id` = {$category->category_id} AND `user_id` = {$this->user->user_id}")->fetch_object()->total ?? 0;
        $paginator = (new \Altum\Paginator($total_rows, $this->user->preferences->default_results_per_page ?? settings()->main->default_results_per_page, $_GET['page'] ?? 1, url('category/' . $category->category_id . '?page=%d')));

        /* Get the items */
        $items = [];
        $items_result = database()->query("
            SELECT
                *
            FROM
                `items`
            WHERE
                `category_id` = {$category->category_id}
                AND `user_id` = {$this->user->user_id}
            ORDER BY `order`

            {$paginator->get_sql_limit()}
        ");
        while($row = $items_result->fetch_object()) $items[] = $row;

        /* Prepare the pagination view */
        $pagination = (new \Altum\View('partials/pagination', (array) $this))->run(['paginator' => $paginator]);

        /* Establish the account sub menu view */
        $data = [
            'category_id' => $category->category_id,
            'resource_name' => $category->name,
            'external_url' => $store->full_url . $menu->url . '/' . $category->url
        ];
        $app_sub_menu = new \Altum\View('partials/app_sub_menu', (array) $this);
        $this->add_view_content('app_sub_menu', $app_sub_menu->run($data));

        /* Set a custom title */
        Title::set(sprintf(l('category.title'), $category->name));

        /* Prepare the view */
        $data = [
            'store' => $store,
            'menu' => $menu,
            'category' => $category,
            'items' => $items,
            'total_items' => $total_rows,
            'pagination' => $pagination
        ];

        $view = new \Altum\View('category/index', (array) $this);

        $this->add_view_content('content', $view->run($data));

    }

    public function duplicate() {
        \Altum\Authentication::guard();

        /* Team checks */
        if(\Altum\Teams::is_delegated() && !\Altum\Teams::has_access('create.categories')) {
            Alerts::add_error(l('global.info_message.team_no_access'));
            redirect('dashboard');
        }

        if (empty($_POST)) {
            throw_404();
        }

        /* Check for the plan limit */
        $total_rows = db()->where('user_id', $this->user->user_id)->getValue('categories', 'COUNT(*)') ?? 0;
        if($this->user->plan_settings->categories_limit != -1 && $total_rows >= $this->user->plan_settings->categories_limit) {
            Alerts::add_error(l('global.info_message.plan_feature_limit') . (settings()->payment->is_enabled ? ' <a href="' . url('plan') . '" class="font-weight-bold text-reset">' . l('global.info_message.plan_upgrade') . '.</a>' : null));
            redirect('dashboard');
        }

        $category_id = (int) $_POST['category_id'];

        //ALTUMCODE:DEMO if(DEMO) if($this->user->user_id == 1) Alerts::add_error('Please create an account on the demo to test out this function.');

        if(!\Altum\Csrf::check()) {
            Alerts::add_error(l('global.error_message.invalid_csrf_token'));
            redirect('dashboard');
        }

        /* Verify the main resource */
        if(!$category = db()->where('category_id', $category_id)->where('user_id', $this->user->user_id)->getOne('categories')) {
            redirect('dashboard');
        }

        if(!Alerts::has_field_errors() && !Alerts::has_errors()) {

            /* Generate random url if not specified */
            $url = mb_strtolower(string_generate(settings()->stores->random_url_length ?? 7));
            while(db()->where('menu_id', $category->menu_id)->where('url', $url)->getValue('categories', 'category_id')) {
                $url = mb_strtolower(string_generate(settings()->stores->random_url_length ?? 7));
            }

            /* Insert to database */
            $category_id = db()->insert('categories', [
                'menu_id' => $category->menu_id,
                'store_id' => $category->store_id,
                'user_id' => $this->user->user_id,
                'url' => $url,
                'name' => string_truncate($category->name . ' - ' . l('global.duplicated'), 64, null),
                'description' => $category->description,
                'is_enabled' => $category->is_enabled,
                'datetime' => get_date(),
            ]);

            /* Get all items to duplicate */
            $items = db()->where('category_id', $category->category_id)->get('items');

            /* Duplicate all of them */
            foreach($items as $item) {
                $url = mb_strtolower(string_generate(settings()->stores->random_url_length ?? 7));

                /* Duplicate the file */
                $image = \Altum\Uploads::copy_uploaded_file($item->image, \Altum\Uploads::get_path('item_images'), \Altum\Uploads::get_path('item_images'));

                /* Insert to database */
                $item_id = db()->insert('items', [
                    'category_id' => $category_id,
                    'menu_id' => $item->menu_id,
                    'store_id' => $item->store_id,
                    'user_id' => $this->user->user_id,
                    'url' => $url,
                    'name' => $item->name,
                    'description' => $item->description,
                    'image' => $image,
                    'price' => $item->price,
                    'variants_is_enabled' => $item->variants_is_enabled,
                    'is_enabled' => $item->is_enabled,
                    'datetime' => get_date(),
                ]);

                (new \Altum\Models\Item())->duplicate($item, $item_id, $category_id, $item->menu_id, $item->store_id, $this->user->user_id);
            }

            /* Set a nice success message */
            Alerts::add_success(sprintf(l('global.success_message.create1'), '<strong>' . input_clean($category->name) . '</strong>'));

            /* Redirect */
            redirect('category/' . $category_id);

        }

        redirect('dashboard');
    }

    public function delete() {

        \Altum\Authentication::guard();

        /* Team checks */
        if(\Altum\Teams::is_delegated() && !\Altum\Teams::has_access('delete.categories')) {
            Alerts::add_error(l('global.info_message.team_no_access'));
            redirect('dashboard');
        }

        if (empty($_POST)) {
            throw_404();
        }

        $category_id = (int) $_POST['category_id'];

        //ALTUMCODE:DEMO if(DEMO) if($this->user->user_id == 1) Alerts::add_error('Please create an account on the demo to test out this function.');

        if(!\Altum\Csrf::check()) {
            Alerts::add_error(l('global.error_message.invalid_csrf_token'));
        }

        /* Make sure the store id is created by the logged in user */
        if(!$category = db()->where('user_id', $this->user->user_id)->where('category_id', $category_id)->getOne('categories', ['store_id', 'menu_id', 'category_id', 'name'])) {
            throw_404();
        }

        if(!Alerts::has_field_errors() && !Alerts::has_errors()) {

            /* Delete the menu */
            database()->query("DELETE FROM `categories` WHERE `category_id` = {$category->category_id}");

            /* Clear the cache */
            cache()->deleteItemsByTag('store_id=' . $category->store_id);

            /* Set a nice success message */
            Alerts::add_success(sprintf(l('global.success_message.delete1'), '<strong>' . $category->name . '</strong>'));

            redirect('menu/' . $category->menu_id);

        }

        redirect('menu/' . $category->menu_id);
    }

    public function order_ajax() {

        \Altum\Authentication::guard();

        /* Team checks */
        if(\Altum\Teams::is_delegated() && !\Altum\Teams::has_access('update.categories')) {
            die();
        }

        $_POST = json_decode(file_get_contents('php://input'), true);

        $store_id = (int) $_POST['store_id'];

        if(empty($_POST)) {
            die();
        }

        if(!isset($_POST['blocks']) || (isset($_POST['blocks']) && !is_array($_POST['blocks']))) {
            die();
        }

        if(!\Altum\Csrf::check('global_token')) {
            die();
        }

        if(!$store = db()->where('store_id', $store_id)->where('user_id', $this->user->user_id)->getOne('stores')) {
            die();
        }

        $case_query = '';
        $category_ids = [];

        foreach($_POST['blocks'] as $block) {
            if(!isset($block['category_id']) || !isset($block['order'])) {
                continue;
            }

            $block['category_id'] = (int) $block['category_id'];
            $block['order'] = (int) $block['order'];

            $case_query .= ' WHEN ' . $block['category_id'] . ' THEN ' . $block['order'];
            $category_ids[] = $block['category_id'];

            /* Clear the cache */
            cache()->deleteItemsByTag('category_id=' . $block['category_id']);
        }

        $in_query = implode(',', $category_ids);

        /* Update all blocks */
        db()->rawQuery("UPDATE `categorys` SET `order` = CASE `category_id` {$case_query} END WHERE `user_id` = {$this->user->user_id} AND `category_id` IN ({$in_query})");

        /* Clear the cache */
        cache()->deleteItemsByTag('store_id=' . $store_id);

        Response::json('', 'success');

    }

}
