<?php

namespace App\Http\Controllers;

use Inertia\Inertia;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;

class PimController extends Controller
{
    
    public function products_index(){
        $products = Http::get(env('PIM_ADDRESS').'/api/public/products/index/'.auth()->user()->token)->json();
        $categories = Http::get(env('PIM_ADDRESS').'/api/public/categories/index/'.auth()->user()->token)->json();
        return Inertia::render('Products/Index', ['products' => $products['data'], 'links' => $products['links'], 'categories' => $categories]); 
    }
    

//Products ----------------------------------------------------------------

    public function products_store(Request $request, $category_id){
        $response = $this->send_request("POST", "products", "create", $category_id, Arr::except($request->all(), ['user_token']) + [
            'slug' => Str::slug($request->title, '_', 'fa'),   
        ]);
        return response($response->json(), $response->status());
    }
    
    public function products_update(Request $request, $product_id){
        $response = $this->send_request("POST", "products", "update", $product_id, Arr::except($request->all(), ['user_token']) + [
            'slug' => Str::slug($request->title, '_', 'fa'),   
        ]);
        return response($response->json(), $response->status());
    }

    public function products_delete($product_id){
        
        $response = $this->send_request("DELETE", "products", "delete", $product_id, []);
        return response($response->json(), $response->status());
    }

//Categories ----------------------------------------------------------------

    public function categories_store(Request $request){
        $response = $this->send_request("POST", "categories", "create", "", Arr::except($request->all(), ['user_token']) + [
            'slug' => Str::slug($request->title, '_', 'fa'),   
        ]);
        return response($response->json(), $response->status());
    }
    
    public function categories_update(Request $request, $category_id){
        $response = $this->send_request("POST", "categories", "update", $category_id, Arr::except($request->all(), ['user_token']) + [
            'slug' => Str::slug($request->title, '_', 'fa'),   
        ]);
        return response($response->json(), $response->status());
    }

    public function categories_delete($category_id){
        
        $response = $this->send_request("DELETE", "categories", "delete", $category_id, []);
        return response($response->json(), $response->status());
    }

//Properties ----------------------------------------------------------------

    public function properties_store(Request $request, $category_id){
        $response = $this->send_request("POST", "properties", "create", $category_id, Arr::except($request->all(), ['user_token']) + [
            'slug' => Str::slug($request->title, '_', 'fa'),   
        ]);
        return response($response->json(), $response->status());
    }
    
    public function properties_update(Request $request, $property_id){
        $response = $this->send_request("POST", "properties", "update", $property_id, Arr::except($request->all(), ['user_token']) + [
            'slug' => Str::slug($request->title, '_', 'fa'),   
        ]);
        return response($response->json(), $response->status());
    }

    public function properties_delete($property_id){
        $response = $this->send_request("DELETE", "properties", "delete", $property_id, []);
        return response($response->json(), $response->status());
    }

    public function properties_attach_category($property_id, $category_id){
        $response = $this->send_request("POST", "properties", $property_id."/attachCategory", $category_id, []);
        return response($response->json(), $response->status());
    }

    public function properties_detach_category($property_id, $category_id){
        $response = $this->send_request("POST", "properties", $property_id."/detachCategory", $category_id, []);
        return response($response->json(), $response->status());
    }

//Order ----------------------------------------------------------------

    public function orders_index(Request $request) {
        $response = $this->send_request("GET", "orders", "index", "", []);
        return response($response->json(), $response->status());
    }
    public function orders_show(Request $request, $order_id) {
        $response = $this->send_request("GET", "orders", "show", $order_id, []);
        return response($response->json(), $response->status());
    }
    public function orders_setStatus(Request $request, $order_id) {
        $response = $this->send_request("POST", "orders", "setStatus", $order_id, Arr::except($request->all(), ['user_token']));
        return response($response->json(), $response->status());
    }

//Helper ----------------------------------------------------------------

    public function send_request($method, $model, $action, $id, $data){
        
        $request = Http::withHeaders([
            'Authorization' => env('secret'),
            'Accept' => 'application/json' 
        ])->withOptions(['verify' => false]);

        $response = null;
        if($method == "POST"){
            $response = $request->post(env('PIM_ADDRESS').'/api/admin/'.$model.'/'.$action.($id != "" ? '/' : '').$id, [
                'user_token' => auth()->user()->token,
            ] + $data);
           
        }
        else if($method == "DELETE"){
            $response = $request->delete(env('PIM_ADDRESS').'/api/admin/'.$model.'/'.$action.($id != "" ? '/' : '').$id, [
                'user_token' => auth()->user()->token,
            ] + $data);
        }
        else if($method == "GET"){
            $response = $request->get(env('PIM_ADDRESS').'/api/admin/'.$model.'/'.$action.($id != "" ? '/' : '').$id, [
                'user_token' => auth()->user()->token,
            ] + $data);
        }
        return $response;
    }
}