<?php

namespace App\Http\Controllers;
use App\Models\Banner;
use App\Models\Product;
use App\Models\Category;
use App\Models\PostTag;
use App\Models\PostCategory;
use App\Models\Post;
use App\Models\Colors;
use App\Models\Size;
use App\Models\Cart;
use App\Models\Brand;
use App\Models\Order;
use App\User;
use Auth;
use Session;
use Newsletter;
use DB;
use Hash;
use Illuminate\Support\Str;
use Illuminate\Http\Request;

class reactApiController extends Controller
{
    //
    public function fetchProducts(){
        $items = array();
        $products=Product::where('status','active')->orderBy('id','DESC')->get();
        foreach($products as $product){
            $data = array(
                'id' => $product['id'],
                'title' => $product['title'],
                'stock' => $product['stock'],
                'photo' => $product['photo']
            );
            array_push($items, $data);
        }
        return response()->json($items);
    }
    public function fetchBanners(){
        $banners=Banner::where('status','active')->limit(3)->orderBy('id','DESC')->get();
        return response()->json($banners);
    }
    public function createUser(Request $request){
        $response = json_decode($request->getContent(), true);
        $checkUser = User::where('account', $response['account'])->exists();
        if(($checkUser)){
            return "User Already Exist";
        }else{
            try {
                $data = array(
                    "name"=> 'Unknown',
                    "email"=> '',
                    "account"=> $response['account'],
                    "role"=> 'user',
                    "status"=> 'active'
                );
                $store = DB::table('users')->insert($data);
                return true;
            } catch (\Throwable $th) {
                //throw $th;
                return false;
            }
        }
    }
    public function fetchProductById($id){
        $productDetails= Product::getProductById($id);

        $colorsID = explode(',', $productDetails->color);
        $colors = Colors::whereIn('id', $colorsID)->get();

        $sizeID = explode(',', $productDetails->size);
        $sizes = Size::whereIn('id', $sizeID)->get();

        $images = explode(',', $productDetails->photo);
        

        $productData = [
            'product'=> $productDetails,
            'sizes'=>json_encode($sizes),
            'colors'=>json_encode($colors),
            'images'=>json_encode($images)
        ];
        return response()->json($productData);
    }
    public function deleteOrderifFailed(Request $request){
        $response = json_decode($request->getContent(), true);
        try {
            DB::table('orders')->where('id', $response['data'])->delete(); 
            DB::table('orderproducts')->where('order_id', $response['data'])->delete(); 
            DB::table('ordernft')->where('order_id', $response['data'])->delete(); 
        } catch (\Throwable $th) {
            return $th;
        }
    }
    public function confirmOrder(Request $request){
        $response = json_decode($request->getContent(), true);
        $data = array(
            "country" => $response['values']['countryvalue'],
            "address" => $response['values']['address'], //address
            "province" => $response['values']['province'],
            "city" => $response['values']['city'],
            "houseNo" => $response['values']['houseNo'],
            "iossNumber" => $response['values']['iossNumber'],
            "account" => $response['account'],
            "first_name" => $response['values']['fullName'],
            "phone" => $response['values']['phone'],
            "email" => $response['values']['email']
        );

        try {

            $store = DB::table('orders')->insertGetId($data);
            if(!empty($response['basket']) && !empty($store)){
                foreach($response['basket'] as $basket){
                    $orderDetails = array(
                        "order_id" => $store,
                        "product_id" => $basket['id'],
                        "color" => $basket['color'],
                        "image" => $basket['image'],
                        "size" => $basket['size'],
                        "title" => $basket['title'],
                    );
                    $storeDetails = DB::table('orderproducts')->insert($orderDetails);
                    if($storeDetails){
                        $currentProductStock = DB::table('products')->where('id', $basket['id'])->first();
                        $newStock = $currentProductStock->stock - 1;
                        $updateStock = DB::table('products')->where('id', $basket['id'])->update(array('stock' => $newStock));
                    }
                }
                if(!empty($response['selectedNFT'])){
                        $nfts = array(
                            'order_id' => $store,
                            'token_id' => $response['selectedNFT']['0']['id'],
                            'image' => $response['selectedNFT']['0']['image']
                        );
                        $nftDetails = DB::table('ordernft')->insert($nfts);
                }
            
                return response()->json(array('order_id'=>$store), '200');
            }

        } catch (\Throwable $th) {
            return $th;
        }

    }

    public function awardPoints(Request $request){
        $checkPromo = DB::table('promo_code')->where('promo_code', $request->promo_code)->exists();
        if($checkPromo){
            $updatePoints = DB::table('promo_code')->where('promo_code', $request->promo_code)->update(array('points'=>10));
            if($updatePoints){
                $data = array(
                    'account'=>$request->account,
                    'promo_code'=>$request->promo_code
                );
                $insert = DB::table('promo_used')->insert($data);
                if($insert){
                    return 1;
                }
            }
        }
    }

}
