<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Pagination\Paginator;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use Illuminate\Support\Facades\Auth;
use Tymon\JWTAuth\Facades\JWTAuth;
use App\Models\User;
use Illuminate\Support\Facades\View;
use App\Models\Diamond;
use App\Models\SamayajewelryDiamond;
class DiamondNewController extends Controller
{
    public function index(Request $request)
    {
        echo "Welcome to my app.";
    }
    public function getFilteredDiamond(Request $request)
    {
        $query = SamayajewelryDiamond::query();

        // SHAPE
        if ($request->shape) {
            $query->where('shapecode', $request->shape);
        }

        // CARAT (exact + tolerance)
        // if ($request->carat) {
        //     $carat = (float)$request->carat;

        //     $query->whereBetween('weight', [
        //         $carat - 0.05,
        //         $carat + 0.05
        //     ]);
        // }
        if ($request->carat) {
                $query->where('weight', (float) $request->carat);
            }

        // FIRST MATCH
        $diamond = $query->orderBy('price1', 'asc')->first();

        return response()->json([
            "record" => $diamond
        ]);
    }
}