<?php
namespace Modules\PrintSuppliers\Traits;
use Illuminate\Support\Facades\DB;

trait ProductFulfill {

    public function getFulfillSkuAttrs($productSkuId = null)
    {
        $returnValue = [];

        if (!$productSkuId || !is_numeric($productSkuId)) {
            $productSkuId = request('sku_id');
        }

        $productAttrs = $this->getProductAttrs($productSkuId);

        $deepestCategoryId = null;

        if ($productAttrs['categories'] && gettype($productAttrs['categories']) == 'array' && count($productAttrs['categories'])) {
            $deepestCategory = array_pop($productAttrs['categories']);
            foreach ($productAttrs['categories'] as $category) {
                if ($category->depth > $deepestCategory->depth) $deepestCategory = $category;
            }
            $deepestCategoryId = $deepestCategory->id;
        }

        $returnValue[] = $deepestCategoryId;

        foreach ([['Color', 'Couleur'], ['Size', 'Taille'], ['Type'], ['Style']] as $listKey) {
            $valueAttr = null;
            foreach ($listKey as $key) {
                if (isset($productAttrs['attrs'][$key])) {
                    $valueAttr = $productAttrs['attrs'][$key];
                    break;
                }
            }
            $returnValue[] = $valueAttr;
        }

        return $returnValue;
    }

    public function getProductAttrs($productSkuId = null)
    {
        $returnValue = null;

        if (!$productSkuId || !is_numeric($productSkuId)) {
            $productSkuId = request('sku_id');
        }

        if ($productSkuId) {

            $productSkuValues = DB::table('product_sku_value')
                ->select([
                    'product_sku_value.sku_id',
                    'product_variant.name as variant',
                    'product_variant_option.name as option',
                    'product_sku_value.product_id'
                ])
                ->where('sku_id', $productSkuId)
                ->join('product_variant', 'product_variant.id', '=', 'product_sku_value.variant_id')
                ->join('product_variant_option', 'product_variant_option.id', '=', 'product_sku_value.variant_option_id')
                ->get();

            $productId = null;
            $productAttrs = [];

            foreach ($productSkuValues as $sku) {
                if (!$productId) $productId = $sku->product_id;
                $productAttrs[$sku->variant] = $sku->option;
            }

            if (!count($productAttrs)) { // query sang bang old_product_sku_value
                return $this->getOldProductAttrs($productSkuId);
            }

            $categories = DB::table('product_n_category')
                ->select([
                    'category.id', 'category.name',
                    'category._lft', 'category._rgt'
                ])
                ->where('product_id', $productId)
                ->join('category', 'category.id', 'product_n_category.category_id')
                ->get();

            foreach ($categories as &$category) {
                $depth = DB::table('category')->where('_lft', '<', $category->_lft)
                    ->where('_rgt', '>', $category->_rgt)->count();
                $category->depth = $depth + 1;
            }

            $returnValue = [
                'product_id' => $productId,
                'attrs' => $productAttrs,
                'categories' => $categories->toArray()
            ];

        }

        return $returnValue;
    }

    public function getOldProductAttrs($productSkuId)
    {
        $returnValue = null;

        if ($productSkuId) {

            $productSkuValues = DB::table('old_product_sku_value')
                ->select([
                    'old_product_sku_value.sku_id',
                    'product_variant.name as variant',
                    'product_variant_option.name as option',
                    'old_product_sku_value.product_id'
                ])
                ->where('sku_id', $productSkuId)
                ->join('product_variant', 'product_variant.id', '=', 'old_product_sku_value.variant_id')
                ->join('product_variant_option', 'product_variant_option.id', '=', 'old_product_sku_value.variant_option_id')
                ->get();

            $productId = null;
            $productAttrs = [];

            foreach ($productSkuValues as $sku) {
                if (!$productId) $productId = $sku->product_id;
                $productAttrs[$sku->variant] = $sku->option;
            }

            $categories = DB::table('product_n_category')
                ->select([
                    'category.id', 'category.name',
                    'category._lft', 'category._rgt'
                ])
                ->where('product_id', $productId)
                ->join('category', 'category.id', 'product_n_category.category_id')
                ->get();

            foreach ($categories as &$category) {
                $depth = DB::table('category')->where('_lft', '<', $category->_lft)
                    ->where('_rgt', '>', $category->_rgt)->count();
                $category->depth = $depth + 1;
            }

            $returnValue = [
                'product_id' => $productId,
                'attrs' => $productAttrs,
                'categories' => $categories->toArray()
            ];

        }

        return $returnValue;
    }

}
