<?php

namespace App\Http\Controllers\Auto;

use App\Http\Controllers\Controller;
use Yajny\Models\Store;
use Yajny\Models\Category;
use Yajny\Models\CategoryCoupon;
use Yajny\Models\Brand;
use Yajny\Models\Tag;
use Illuminate\Support\Facades\DB;


class RecommendStores extends Controller
{

    public function brands()
    {
        foreach(DB::table('brands')->select('id')->get() as $brand)
        {
            DB::statement("UPDATE `brands` SET recom_store = (SELECT JSON_ARRAYAGG(store_id) FROM (SELECT  (store_id), SUM(clicks)   FROM `coupons` c  WHERE JSON_OVERLAPS(brands,'[{$brand->id}]') GROUP BY store_id ORDER BY  2  DESC LIMIT 5) st) WHERE id = ".$brand->id);
        }
    }


    public function cats()
    {
        foreach(DB::table('categories_coupon')->select('id')->get() as $cat)
        {
            DB::statement("UPDATE `categories_coupon` SET recom_store = (SELECT JSON_ARRAYAGG(store_id) FROM (SELECT  (store_id), SUM(clicks)   FROM `coupons` c  WHERE JSON_OVERLAPS(cats,'[{$cat->id}]') GROUP BY store_id ORDER BY  2  DESC LIMIT 5) st) WHERE id = ".$cat->id);
        }
    }



    public function tags()
    {
        foreach(DB::table('tags')->select('id')->get() as $tag)
        {
            DB::statement("UPDATE `tags` SET recom_store = (SELECT JSON_ARRAYAGG(store_id) FROM (SELECT  (store_id), SUM(clicks)   FROM `coupons` c  WHERE JSON_OVERLAPS(tags,'[{$tag->id}]') GROUP BY store_id ORDER BY  2  DESC LIMIT 5) st) WHERE id = ".$tag->id);
        }
    }

}
