<?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\Models\User;

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

class StoreRedirect extends Controller {

    public function index() {

        if(isset($_GET['store_id'])) {
            $store_id = isset($_GET['store_id']) ? (int) $_GET['store_id'] : null;

            if(!$store = db()->where('store_id', $store_id)->getOne('stores', ['store_id', 'domain_id', 'user_id', 'url'])) {
                throw_404();
            }

            $this->store_user = (new User())->get_user_by_user_id($store->user_id);

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

            header('Location: ' . $store->full_url);

            die();
        }

        else if(isset($_GET['menu_id'])) {
            $menu_id = isset($_GET['menu_id']) ? (int) $_GET['menu_id'] : null;

            if(!$menu = db()->where('menu_id', $menu_id)->getOne('menus', ['store_id', 'url'])) {
                throw_404();
            }

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

            $this->store_user = (new User())->get_user_by_user_id($store->user_id);

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

            header('Location: ' . $store->full_url . $menu->url);

            die();
        }

        else if(isset($_GET['category_id'])) {
            $category_id = isset($_GET['category_id']) ? (int) $_GET['category_id'] : null;

            if(!$category = db()->where('category_id', $category_id)->getOne('categories', ['menu_id', 'store_id', 'url'])) {
                throw_404();
            }

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

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

            $this->store_user = (new User())->get_user_by_user_id($store->user_id);

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

            header('Location: ' . $store->full_url . $menu->url . '/' . $category->url);

            die();
        }

        else if(isset($_GET['item_id'])) {
            $item_id = isset($_GET['item_id']) ? (int) $_GET['item_id'] : null;

            if(!$item = db()->where('item_id', $item_id)->getOne('items', ['category_id', 'menu_id', 'store_id', 'url'])) {
                throw_404();
            }

            $category = db()->where('category_id', $item->category_id)->getOne('categories', ['url']);

            $menu = db()->where('menu_id', $item->menu_id)->getOne('menus', ['url']);

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

            $this->store_user = (new User())->get_user_by_user_id($store->user_id);

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

            header('Location: ' . $store->full_url . $menu->url . '/' . $category->url . '/' . $item->url);

            die();
        } else {

            throw_404();

        }

    }
}
