<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\auth_user_request;
use App\Http\Requests\create_cart_request;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use App\Http\Requests\create_user_request;
use App\Models\Category;
use App\Models\Product;
use App\Models\Cart;

class UserController extends Controller
{
    public function loginuser(auth_user_request $request)
    {
        if (Auth::attempt(['login' => $request->login,  'password' => $request->password])) {
            return redirect()->route('index');;
        } else {
            return redirect()->route('login')->withErrors([
                'password' => 'Incorrect password',
            ]);
        }
    }

    public function registeruser(create_user_request $request)
    {
        $user = User::create([
            'fullname' => $request->fullname,
            'login' => $request->login,
            'phone' => $request->phone,
            'email' => $request->email,
            'password' => $request->password,
        ]);
        Auth::login($user);
        return redirect()->route('index');
    }
    public function catalog()
    {
        $categories = Category::with('products')->orderBy('created_at', 'desc')->get();
        $products = Product::all();
        // $user = Auth::user();
        // $aps = Apply::all();
        // return view('account', ['aps' => $aps, 'user' => $user]);
        return view('index', ['categories' => $categories]);
    }

    public function catalogAPI()
    {
        $categories = Category::with('products')->select('categories.*')->leftJoin('products', 'categories.id', '=', 'products.category_id')->groupBy('categories.id')->orderByRaw('MAX(products.created_at) desc')->get();
        return ['categories' => $categories];
    }

    public function addcart(Product $product)
    {
        $cart = Cart::where('user_id', Auth::id())->where('product_id', $product->id)->first();

        if ($cart && $product->count != 0) {
            $cart->count += 1;
            $cart->save();
            $product->count -= 1;
            $product->save();
        } else {
            Cart::create([
                'product_id' => $product->id,
                'user_id' => Auth::id(),
                'counts' => 1,
                'price' => $product->price,
            ]);
        }

        return redirect()->route('lk');
    }
    public function removecart(Product $product)
    {
        $cart = Cart::where('user_id', Auth::id())->where('product_id', $product->id)->first();
        if($cart->count != 0){
            $cart->count -= 1;
            $cart->save();
            $product->count += 1;
            $product->save();
        }
        
        return redirect()->route('lk');
    }
    public function lk()
    {
        $carts = Cart::where('user_id', Auth::id())->get();
        $total = $carts->sum(function ($cart) {
            return $cart->price * $cart->count;
        });
        // $user = Auth::user();
        // $aps = Apply::all();
        // return view('account', ['aps' => $aps, 'user' => $user]);
        return view('lk', ['carts' => $carts, 'total' => $total]);
    }
    public function lkAPI()
    {
        $carts = Cart::where('user_id', 1)->get();
        // $user = Auth::user();
        // $aps = Apply::all();
        // return view('account', ['aps' => $aps, 'user' => $user]);
        return ['carts' => $carts];
    }

    public function register()
    {
        return view('register');
    }

    public function login()
    {
        return view('login');
    }

    public function logout()
    {
        Auth::Logout();
        return redirect("/");
    }

    public function categories(Request $request)
    {
        $categories = Category::all();
        if ($request->is('apply')) {
            return view('apply', ['categories' => $categories]);
        } elseif ($request->is('/')) {
            return view('index', ['categories' => $categories]);
        }
    }

    public function auser()
    {
        return Auth::user();
    }
}
