<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB; // Import the DB facade

class ReviewController extends Controller
{
    // Get all reviews
    public function index()
    {
        $reviews = DB::table('review')->get(); // Fetch all reviews from the 'review' table
        return response()->json($reviews);
    }
public function indexAdmin()
{
    // Fetch all reviews with associated user names
    $reviews = DB::table('review')
        ->select(
            'review.id',
            'review.userID',
            'review.customerID',
            'review.star',
            'review.feedback',
            'review.orderID',
            'review.created_at',
            'review.updated_at',
            'user.name as userName', // User name, which can be vendor or driver
            'user.role as userRole', // User role
            'customer.name as customerName' // Customer name
        )
        ->leftJoin('users as user', 'review.userID', '=', 'user.user_id') // Join with users table for vendors or drivers
        ->leftJoin('users as customer', 'review.customerID', '=', 'customer.user_id') // Join with users table for customers
        ->get();

    // Return the view with reviews data
    return view('components.ratings-and-review', compact('reviews'));
}

    // Get reviews by user ID
    public function getByUserId($userID)
    {
        $reviews = DB::table('review')->where('userID', $userID)->get(); // Fetch reviews by user ID
        return response()->json($reviews);
    }

    // Get reviews by customer ID
    public function getByCustomerId($customerID)
    {
        $reviews = DB::table('review')->where('customerID', $customerID)->get(); // Fetch reviews by customer ID
        return response()->json($reviews);
    }

    // Create or update a review
public function store(Request $request)
{
    $request->validate([
        'userID' => 'required|string',
        'customerID' => 'required|string',
        'star' => 'required|numeric|between:1,5', // Accept numeric values between 1 and 5, including decimals
        'feedback' => 'nullable|string',
        'orderID' => 'nullable|string', // Ensure orderID is required
    ]);

    // Check if a review already exists with the same orderID, userID, and customerID
    $existingReview = DB::table('review')
        ->where('orderID', $request->orderID)
        ->where('userID', $request->userID)
        ->where('customerID', $request->customerID)
        ->first();

    if ($existingReview) {
        // Update the existing review
        DB::table('review')->where('id', $existingReview->id)->update($request->only(['star', 'feedback', 'orderID']));
        $updatedReview = DB::table('review')->where('id', $existingReview->id)->first();
        return response()->json($updatedReview);
    } else {
        // Insert the new review into the 'review' table
        $reviewId = DB::table('review')->insertGetId($request->only(['userID', 'customerID', 'star', 'feedback', 'orderID']));

        // Fetch the created review to return it
        $newReview = DB::table('review')->where('id', $reviewId)->first();
        return response()->json($newReview, 201);
    }
}

    // Update a review (if needed)
    public function update(Request $request, $id)
    {
        $request->validate([
            'star' => 'sometimes|required|integer|min:1|max:5',
            'feedback' => 'sometimes|nullable|string',
            'orderID' => 'sometimes|nullable|string',
        ]);

        // Update the review in the 'review' table
        DB::table('review')->where('id', $id)->update($request->only(['star', 'feedback', 'orderID']));

        // Fetch the updated review to return it
        $review = DB::table('review')->where('id', $id)->first();
        return response()->json($review);
    }

    // Delete a review
    public function destroy($id)
    {
        DB::table('review')->where('id', $id)->delete(); // Delete the review by ID
        return response()->json(null, 204); // Return a 204 No Content response
    }
}
