<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use Illuminate\Http\Request;
 
class ProductsController extends Controller
{
    public function index(Request $request){         
          $objProductsController = new ProductsController();
          $objProductsCategory = $objProductsController->productsCategory($request->url);        
          return view('product',['objProductsCategory'=> $objProductsCategory]); 
    }
    

    public function variantsList(){        
        $response = Http::withHeaders([            
        'Authorization' => 'Bearer '.env('VITE_BEARER_TOKEN__GEN_API'),
        'Content-Type' => 'application/json',
        ])->get(env('VITE_SERVICES_API').'variants/list');
        $data = $response->json();            
        return $data;
    }


    public function productsAll(){        
        $response = Http::withHeaders([            
        'Authorization' => 'Bearer '.env('VITE_BEARER_TOKEN__GEN_API'),
        'Content-Type' => 'application/json',
        ])->get(env('VITE_SERVICES_API').'products/all');
        $data = $response->json();            
        return $data;
    }

    public function Allpackage(){    
        $response = Http::withHeaders([            
        'Authorization' => 'Bearer '.env('VITE_BEARER_TOKEN__GEN_API'),
        'Content-Type' => 'application/json',
        ])->get(env('VITE_SERVICES_API').'products/allpackage');
        $data = $response->json(); 
        return $data;
    } 

    public function packageId(Request $request){  
        $objProductsController = new ProductsController();
        $objProducts = $objProductsController->Allpackage();
        $data = collect($objProducts)->firstWhere('id',$request->id);        
        return $data;
    }

    public function productsAllIndex(Request $request){        
        if($request->search==""){
            $objProductsController = new ProductsController();
            $objProducts = $objProductsController->productsAll();
            return view('product',['objProducts'=> $objProducts]); 
        }else{
            $data ="<div class='sec-main__con2 sec-main__con2--nf'>";
            $data .="<h1 class='sec-main__tit'>No se encontraron resultados con el criterio de búsqueda: <strong>'".$request->search."'</strong></h1>";
            $data .="<p class='sec-main__des'>Revisa que esté bien escrito o intenta con otras palabras.</p><a class='sxs-btn sxs-btn--primary sxs-btn--style-primary sec-main__btn' href='/' data-discover='true'>";
            $data .="<div class='sxs-btn__con0'><span class='sxs-btn__wv'></span>";
            $data .="<div class='sxs-btn__con1'>";
            $data .="<div class='sxs-btn__con2'><span class=''><span class='sas-bs' style='--sas-idx: 0;'>V</span><span class='sas-bs' style='--sas-idx: 1;'>o</span><span class='sas-bs' style='--sas-idx: 2;'>l</span><span class='sas-bs' style='--sas-idx: 3;'>v</span><span class='sas-bs' style='--sas-idx: 4;'>e</span><span class='sas-bs' style='--sas-idx: 5;'>r</span><span class='sas-bs' style='--sas-idx: 6;'>&nbsp;</span></span><span class=''><span class='sas-bs' style='--sas-idx: 0;'>a</span><span class='sas-bs' style='--sas-idx: 1;'>l</span><span class='sas-bs' style='--sas-idx: 2;'>&nbsp;</span></span><span class=''><span class='sas-bs' style='--sas-idx: 0;'>i</span><span class='sas-bs' style='--sas-idx: 1;'>n</span><span class='sas-bs' style='--sas-idx: 2;'>i</span><span class='sas-bs' style='--sas-idx: 3;'>c</span><span class='sas-bs' style='--sas-idx: 4;'>i</span><span class='sas-bs' style='--sas-idx: 5;'>o</span></span></div><div class='sxs-btn__con2'><span class=''><span class='sas-bs' style='--sas-idx: 0;'>V</span><span class='sas-bs' style='--sas-idx: 1;'>o</span><span class='sas-bs' style='--sas-idx: 2;'>l</span><span class='sas-bs' style='--sas-idx: 3;'>v</span><span class='sas-bs' style='--sas-idx: 4;'>e</span><span class='sas-bs' style='--sas-idx: 5;'>r</span><span class='sas-bs' style='--sas-idx: 6;'>&nbsp;</span></span><span class=''><span class='sas-bs' style='--sas-idx: 0;'>a</span><span class='sas-bs' style='--sas-idx: 1;'>l</span><span class='sas-bs' style='--sas-idx: 2;'>&nbsp;</span></span><span class=''><span class='sas-bs' style='--sas-idx: 0;'>i</span><span class='sas-bs' style='--sas-idx: 1;'>n</span><span class='sas-bs' style='--sas-idx: 2;'>i</span><span class='sas-bs' style='--sas-idx: 3;'>c</span><span class='sas-bs' style='--sas-idx: 4;'>i</span><span class='sas-bs' style='--sas-idx: 5;'>o</span></span></div>";
            $data .="</div></div></a></div>";
            return view('product',['objProductsSearch'=> $data,'objProducts'=> ""]);    
        }        
    }

    public function productsId($id){        
        $objProductsController = new ProductsController();
        $objProducts = $objProductsController->productsAll();
        $data = collect($objProducts)->firstWhere('id',$id);        
        return $data;
    }

    public function productsURL($url){        
        $objProductsController = new ProductsController();
        $objProducts = $objProductsController->productsAll();
        $data = collect($objProducts)->firstWhere('url',$url);        
        return $data;
    }
 
    public function productsURLAll($url){
        $objProductsController = new ProductsController();
        $objProducts = collect($objProductsController->productsAll());
        $product = $objProducts->firstWhere('url', $url);
        if (!$product) {
            return collect([]);
        }
        $product = collect($product);
        $productId = $product->get('idproducto') ?? $product->get('id');
        $objVariants = collect($objProductsController->variantsList());    
        $idvariacion = $objVariants
            ->where('idproducto', $productId)
            ->pluck('idvariacion')
            ->first() ?? 0;
        $product->put('idvariacion', $idvariacion);
        return $product;
    }

    public function productsCategory($url){        
        $response = Http::withHeaders([            
        'Authorization' => 'Bearer '.env('VITE_BEARER_TOKEN__GEN_API'),
        'Content-Type' => 'application/json',
        ])->get(env('VITE_SERVICES_API').'products/category/'.$url);
        $data = $response->json();            
        return $data;
    }

    public function productDetail($url){        
        $response = Http::withHeaders([            
        'Authorization' => 'Bearer '.env('VITE_BEARER_TOKEN__GEN_API'),
        'Content-Type' => 'application/json',
        ])->get(env('VITE_SERVICES_API').'products/detail/'.$url);
        return $productDetail = $response->json();        
    }

    public function productsDetail($url){        
        $objProductsController = new ProductsController();
        $objProductDetail = $objProductsController->productDetail($url);                 
        return view('productdetail',['productDetail'=> $objProductDetail]);
    }
}