<?php

namespace App\Http\Controllers\Web;

use App\Models\Room;
use App\Models\Order;
use App\Models\Product;
use App\Models\Group;
use App\Models\GroupProduct;
use App\Models\TableNumber;
use App\Services\Integration\Integration\Connection;
use App\Services\Interfaces\CategoryRepositoryInterface;
use App\Services\Interfaces\ProductRepositoryInterface;
use App\Services\Interfaces\RoomRepositoryInterface;
use App\Http\Controllers\Controller;
use App\Models\OrderProduct;
use App\Services\Interfaces\GroupRepositoryInterface;
use App\Services\Interfaces\TableNumberRepositoryInterface;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;
use Illuminate\View\View;

class CronController extends Controller
{
    private CategoryRepositoryInterface $categoryRepository ;
    private GroupRepositoryInterface $groupRepository ;
    private ProductRepositoryInterface $productRepository ;
    private TableNumberRepositoryInterface $tableNumberRepository ;
    private Connection $connection ;


    public function __construct(
        CategoryRepositoryInterface $categoryRepository,
        GroupRepositoryInterface $groupRepository,
        ProductRepositoryInterface $productRepository,
        TableNumberRepositoryInterface $tableNumberRepository,
        Connection $connection,
    )
    {
        $this->categoryRepository = $categoryRepository;
        $this->groupRepository = $groupRepository;
        $this->productRepository = $productRepository;
        $this->tableNumberRepository = $tableNumberRepository;
        $this->connection = $connection;
    }

    public function cron()
    {
        // $this->postOrder();
        //$this->getCategories();
        
        $this->getGroups();
        $this->getProducts();
        $this->getTables();
    }
    
    private function postOrder()
    {
        $url = Config::get('app.baseUrl').'orders';
        $order = Order::find(48);
        $products = [];
        foreach($order->products as $row)
        {
            $product_item = 
            [
                "product_id" => $row->product_id, 
                "quantity" => $row->count, 
                "discount_quantity" => 0, 
                "unit_price" => $row->product_cost, 
                "discount_amount" => 0, 
                "total_price" => $row->total, 
                "tax_exclusive_discount_amount" => 0, 
                "tax_exclusive_unit_price" => 0, 
                "tax_exclusive_total_price" => 0, 
            ];
            array_push($products,$product_item);
        }
        
        $data = 
        [
            "type" => 1, 
            "source" => 2, 
            "guests" => 2, 
            "status" => 1, 
            "business_date" => date('Y-m-d'), 
            "subtotal_price" => 0, 
            "discount_amount" => $order->payment->discount, 
            "rounding_amount" => 0, 
            "total_price" => $order->payment->total, 
            "tax_exclusive_discount_amount" => 0, 
            "branch_id" => "9c8948f0-8a1b-4ea7-8fef-b698ef3cb555", 
            "products" => $products, 
            "due_at" => date('Y-m-d H:i:s')
        ];
        $this->connection->create($url,$data);
    }
    
    public function getCategories($page = 1)
    {
        $url = Config::get('app.baseUrl').'categories';
        $data = $this->connection->get($url,['page'=>$page]);
        if ($data->successful())
        {
            $data = json_decode($data->body());
            foreach ($data->data as $row)
            {
                $checkItem = $this->categoryRepository->checkItem($row->id);
                if ($checkItem)
                {
                    $this->categoryRepository->update($row,$row->id);
                }
                else
                {
                    $this->categoryRepository->create($row);
                }
            }
            if(!is_null($data->links->next))
            {
                $page = last(explode('?page=',$data->links->next));
                
                $this->getCategories($page);
            }
        }
    }
    
    public function getGroups($page = 1)
    {
        if($page == 1)
        {
            Group::query()->delete();
        }
        $url = Config::get('app.baseUrl').'groups';
        // dump($url);
        $data = $this->connection->get($url,['include'=> 'subgroups','filter[is_deleted]'=>false,'page'=>$page]);
        if ($data->successful())
        {
            $data = json_decode($data->body());
            // dd($data);
            foreach ($data->data ?? [] as $row)
            {
                $checkItem = $this->groupRepository->checkItem($row->id);
                // dump($row);
                if ($checkItem)
                {
                    $this->groupRepository->restore($row,$row->id);
                    // Reset parent_foodics_id 
                    $row->parent_foodics_id = null;
                    $this->groupRepository->update($row,$row->id);
                }
                else
                {
                    $this->groupRepository->create($row);
                }
                
            }

            // SET THE PARENT FOODECIS ID
            foreach ($data->data ?? [] as $row)
            {
                foreach ($row->subgroups ?? [] as $subGroup) {
                    $checkItem = $this->groupRepository->checkItem($subGroup->id);
                    if ($checkItem)
                    {
                        $this->groupRepository->restore($subGroup,$subGroup->id);
                        $this->groupRepository->restore($row,$row->id);
                        $this->groupRepository->updateParentGroup($subGroup->id, $row->id);
                    }
                }
            }
            
            if(!is_null($data->links->next))
            {
                $page = last(explode('?page=',$data->links->next));
                
                $this->getGroups($page);
            }
        }
    }

    public function getProducts($page = 1)
    {
        if($page == 1)
        {
            Product::query()->delete();
            GroupProduct::query()->delete();
        }
        $url = Config::get('app.baseUrl').'products';
        $data = $this->connection->get($url,['include'=> 'groups','page'=>$page]);
        // dd($data);
        if ($data->successful())
        {
            $data = json_decode($data->body());
            
            foreach ($data->data as $row)
            {
                $checkItem = $this->productRepository->checkItem($row->id);
                if ($checkItem)
                {
                    $this->productRepository->restore($row,$row->id);
                    $this->productRepository->update($row,$row->id);
                }
                else
                {
                    $this->productRepository->create($row);
                }
            }
            if(!is_null($data->links->next))
            {
                $page = last(explode('?page=',$data->links->next));
                
                $this->getProducts($page);
            }
        }
    }

    public function getTables($page = 1)
    {
        if($page == 1)
        {
            TableNumber::query()->delete();
        }
        $url = Config::get('app.baseUrl').'tables';
        $data = $this->connection->get($url,['page'=>$page]);
        if ($data->successful())
        {
            $data = json_decode($data->body());

            foreach ($data->data as $row)
            {
                $checkItem = $this->tableNumberRepository->checkItem($row->id);
                if ($checkItem)
                {
                    $this->tableNumberRepository->restore($row,$row->id);
                    $this->tableNumberRepository->update($row,$row->id);
                }
                else
                {
                    $this->tableNumberRepository->create($row);
                }
            }
            if(!is_null($data->links->next))
            {
                $page = last(explode('?page=',$data->links->next));
                
                $this->getTables($page);
            }
        }
    }



}
