<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Item;
use App\Models\Review;
use Illuminate\Http\Request;

class ReviewsController extends Controller
{
    public function list(){
        $reviews=Review::with(['item','itemCampaign','customer','order.details'])->latest()->paginate(config('default_pagination'));
        return view('admin-views.reviews.list',compact('reviews'));
    }

    public function search(Request $request){
        $key = explode(' ', $request['search']);
        $foods=Item::withoutGlobalScopes()->where(function ($q) use ($key) {
            foreach ($key as $value) {
                $q->orWhere('name', 'like', "%{$value}%");
            }
        })->pluck('id')->toArray();
        $campaigns = \App\Models\ItemCampaign::withoutGlobalScopes()->where(function ($q) use ($key) {
            foreach ($key as $value) {
                $q->orWhere('title', 'like', "%{$value}%");
            }
        })->pluck('id')->toArray();
        $reviews=Review::with(['item','itemCampaign','customer','order.details'])->where(function ($query) use ($foods, $campaigns, $key, $request) {
            $hasCondition = false;

            if (Review::hasColumn('item_id')) {
                $query->whereIn('item_id',$foods);
                $hasCondition = true;
            }

            if (Review::hasColumn('item_campaign_id')) {
                if ($hasCondition) {
                    $query->orWhereIn('item_campaign_id',$campaigns);
                } else {
                    $query->whereIn('item_campaign_id',$campaigns);
                }
                $hasCondition = true;
            }

            if (Review::hasColumn('user_id')) {
                $method = $hasCondition ? 'orWhereHas' : 'whereHas';
                $query->{$method}('customer', function ($customerQuery) use ($key) {
                    foreach ($key as $value) {
                        $customerQuery->where('f_name', 'like', "%{$value}%")
                            ->orWhere('l_name', 'like', "%{$value}%");
                    }
                });
                $hasCondition = true;
            }

            if (Review::hasColumn('order_id')) {
                if ($hasCondition) {
                    $query->orWhere('order_id', 'like', "%{$request['search']}%");
                } else {
                    $query->where('order_id', 'like', "%{$request['search']}%");
                }
                $hasCondition = true;
            }

            if (! $hasCondition) {
                $query->whereRaw('1 = 0');
            }
        })->where(function ($q) use ($key) {
            $hasCondition = false;

            if (Review::hasColumn('review_id')) {
                foreach ($key as $index => $value) {
                    if ($index === 0) {
                        $q->where('review_id', 'like', "%{$value}%");
                    } else {
                        $q->orWhere('review_id', 'like', "%{$value}%");
                    }
                }
                $hasCondition = true;
            }

            if (Review::hasColumn('comment')) {
                foreach ($key as $value) {
                    if ($hasCondition) {
                        $q->orWhere('comment', 'like', "%{$value}%");
                    } else {
                        $q->where('comment', 'like', "%{$value}%");
                        $hasCondition = true;
                    }
                }
            }

            if (! $hasCondition) {
                $q->whereRaw('1 = 1');
            }
        })->get();
        return response()->json([
            'view'=>view('admin-views.reviews.partials._table',compact('reviews'))->render()
        ]);
    }
}
