<?php

namespace App\Http\Controllers\Vendor;

use App\Models\Review;
use Illuminate\Http\Request;
use App\CentralLogics\Helpers;
use App\Exports\ReviewsExport;
use App\Http\Controllers\Controller;
use Brian2694\Toastr\Facades\Toastr;
use Maatwebsite\Excel\Facades\Excel;

class ReviewController extends Controller
{
    public function index(Request $request)
    {
        $key = explode(' ', $request['search']);
        $storeId = Helpers::get_store_id();
        $reviews = Review::with(['item','itemCampaign','customer','order.details'])
        ->where(function ($query) use ($storeId) {
            $hasCondition = false;

            if (Review::hasColumn('store_id')) {
                $query->where('store_id', $storeId);
                $hasCondition = true;
            }

            if (Review::hasColumn('item_id')) {
                $method = $hasCondition ? 'orWhereHas' : 'whereHas';
                $query->{$method}('item', function ($itemQuery) use ($storeId) {
                    $itemQuery->withoutGlobalScopes()->where('store_id', $storeId);
                });
                $hasCondition = true;
            }

            if (Review::hasColumn('item_campaign_id')) {
                $method = $hasCondition ? 'orWhereHas' : 'whereHas';
                $query->{$method}('itemCampaign', function ($campaignQuery) use ($storeId) {
                    $campaignQuery->withoutGlobalScopes()->where('store_id', $storeId);
                });
                $hasCondition = true;
            }

            if (Review::hasColumn('order_id')) {
                $method = $hasCondition ? 'orWhereHas' : 'whereHas';
                $query->{$method}('order', function ($orderQuery) use ($storeId) {
                    $orderQuery->where('store_id', $storeId);
                });
                $hasCondition = true;
            }

            if (! $hasCondition) {
                $query->whereRaw('1 = 0');
            }
        })
        ->when($request['search'], function($query) use($key, $request){
            $query->where(function ($searchQuery) use ($key, $request) {
                $hasCondition = false;

                if (Review::hasColumn('item_id')) {
                    $searchQuery->whereHas('item', function($itemQuery) use($key){
                        $itemQuery->withoutGlobalScopes();
                        foreach ($key as $value) {
                            $itemQuery->where('name', 'like', "%{$value}%");
                        }
                    });
                    $hasCondition = true;
                }

                if (Review::hasColumn('item_campaign_id')) {
                    $method = $hasCondition ? 'orWhereHas' : 'whereHas';
                    $searchQuery->{$method}('itemCampaign', function($campaignQuery) use($key){
                        $campaignQuery->withoutGlobalScopes();
                        foreach ($key as $value) {
                            $campaignQuery->where('title', 'like', "%{$value}%");
                        }
                    });
                    $hasCondition = true;
                }

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

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

                if (Review::hasColumn('order_id')) {
                    if ($hasCondition) {
                        $searchQuery->orWhere('order_id', 'like', "%{$request['search']}%");
                    } else {
                        $searchQuery->where('order_id', 'like', "%{$request['search']}%");
                    }
                }
            });
        })
        ->latest()->paginate(config('default_pagination'));
        return view('vendor-views.review.index', compact('reviews'));
    }
    public function reviewsExport(Request $request)
    {
        $key = explode(' ', $request['search']);
        $storeId = Helpers::get_store_id();
        $reviews = Review::with(['item','itemCampaign','customer','order.details'])
        ->where(function ($query) use ($storeId) {
            $hasCondition = false;

            if (Review::hasColumn('store_id')) {
                $query->where('store_id', $storeId);
                $hasCondition = true;
            }

            if (Review::hasColumn('item_id')) {
                $method = $hasCondition ? 'orWhereHas' : 'whereHas';
                $query->{$method}('item', function ($itemQuery) use ($storeId) {
                    $itemQuery->withoutGlobalScopes()->where('store_id', $storeId);
                });
                $hasCondition = true;
            }

            if (Review::hasColumn('item_campaign_id')) {
                $method = $hasCondition ? 'orWhereHas' : 'whereHas';
                $query->{$method}('itemCampaign', function ($campaignQuery) use ($storeId) {
                    $campaignQuery->withoutGlobalScopes()->where('store_id', $storeId);
                });
                $hasCondition = true;
            }

            if (Review::hasColumn('order_id')) {
                $method = $hasCondition ? 'orWhereHas' : 'whereHas';
                $query->{$method}('order', function ($orderQuery) use ($storeId) {
                    $orderQuery->where('store_id', $storeId);
                });
                $hasCondition = true;
            }

            if (! $hasCondition) {
                $query->whereRaw('1 = 0');
            }
        })
        ->when($request['search'], function($query) use($key, $request){
            $query->where(function ($searchQuery) use ($key, $request) {
                $hasCondition = false;

                if (Review::hasColumn('item_id')) {
                    $searchQuery->whereHas('item', function($itemQuery) use($key){
                        $itemQuery->withoutGlobalScopes();
                        foreach ($key as $value) {
                            $itemQuery->where('name', 'like', "%{$value}%");
                        }
                    });
                    $hasCondition = true;
                }

                if (Review::hasColumn('item_campaign_id')) {
                    $method = $hasCondition ? 'orWhereHas' : 'whereHas';
                    $searchQuery->{$method}('itemCampaign', function($campaignQuery) use($key){
                        $campaignQuery->withoutGlobalScopes();
                        foreach ($key as $value) {
                            $campaignQuery->where('title', 'like', "%{$value}%");
                        }
                    });
                    $hasCondition = true;
                }

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

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

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

        $data = [
            'data'=>$reviews,
            'search'=>$request->search??null,
        ];

        if ($request->export_type == 'excel') {
            return Excel::download(new ReviewsExport($data), 'ReviewsExport.xlsx');
        } else if ($request->export_type == 'csv') {
            return Excel::download(new ReviewsExport($data), 'ReviewsExport.csv');
        }
    }

    public function update_reply(Request $request, $id)
    {
        $request->validate([
            'reply' => 'required|max:65000',
        ]);

        $review = Review::findOrFail($id);
        $review->reply = $request->reply;
        $review->replied_at = now();
        $review->store_id = Helpers::get_store_id();
        $review->save();

        Toastr::success(translate('messages.review_reply_updated'));
        return to_route('vendor.reviews');
    }
}
