<?php
    namespace App\Http\Controllers\Admin;

    use App\Http\Controllers\Controller;
    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\DB;

    class AdminReviewController extends Controller
    {
        /**
         * Display all reviews with filters
         */
        public function index(Request $request)
        {
            $status = $request->get('status', 'all');
            
            // Build query
            $query = DB::table('reviews')
                ->leftJoin('products', 'reviews.product_id', '=', 'products.id')
                ->leftJoin('categories', 'products.category_id', '=', 'categories.id')
                ->select(
                    'reviews.*',
                    'products.name as product_name',
                    'products.sku as product_sku',
                    'categories.name as category_name'
                )
                ->orderBy('reviews.created_at', 'desc');
            
            // Filter by status
            if ($status !== 'all') {
                $query->where('reviews.status', $status);
            }
            
            // Paginate
            $reviews = $query->paginate(20)->withQueryString();
            
            // Get stats
            $stats = [
                'total' => DB::table('reviews')->count(),
                'pending' => DB::table('reviews')->where('status', 'pending')->count(),
                'approved' => DB::table('reviews')->where('status', 'approved')->count(),
                'rejected' => DB::table('reviews')->where('status', 'rejected')->count(),
            ];
            
        return view('account.reviews.reviews', compact('reviews', 'stats', 'status'));        }
        
        /**
         * Approve review
         */
        public function approve($id)
        {
            DB::table('reviews')
                ->where('id', $id)
                ->update([
                    'status' => 'approved',
                    'updated_at' => now()
                ]);
            
            // Update product rating cache
            $review = DB::table('reviews')->where('id', $id)->first();
            if ($review) {
                $this->updateProductRating($review->product_id);
            }
            
            return back()->with('success', 'Review approved successfully!');
        }
        
        /**
         * Reject review
         */
        public function reject($id)
        {
            DB::table('reviews')
                ->where('id', $id)
                ->update([
                    'status' => 'rejected',
                    'updated_at' => now()
                ]);
            
            // Update product rating cache
            $review = DB::table('reviews')->where('id', $id)->first();
            if ($review) {
                $this->updateProductRating($review->product_id);
            }
            
            return back()->with('success', 'Review rejected successfully!');
        }
        
        /**
         * Delete review
         */
        public function destroy($id)
        {
            // Get review before deleting
            $review = DB::table('reviews')->where('id', $id)->first();
            
            if ($review) {
                // Delete review images
                DB::table('review_images')->where('review_id', $id)->delete();
                
                // Delete review
                DB::table('reviews')->where('id', $id)->delete();
                
                // Update product rating cache
                $this->updateProductRating($review->product_id);
            }
            
            return back()->with('success', 'Review deleted successfully!');
        }
        
        /**
         * Update product rating cache
         */
        private function updateProductRating($productId)
        {
            $averageRating = round(DB::table('reviews')
                ->where('product_id', $productId)
                ->where('status', 'approved')
                ->avg('rating') ?? 0, 1);
            
            $totalReviews = DB::table('reviews')
                ->where('product_id', $productId)
                ->where('status', 'approved')
                ->count();
            
            DB::table('products')
                ->where('id', $productId)
                ->update([
                    'average_rating' => $averageRating,
                    'total_reviews' => $totalReviews,
                    'updated_at' => now()
                ]);
        }
    }