<?php

namespace Modules\Store\Controllers\Api;


use App\Services\Response\ResponseService;
use Exception;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Redirector;
use Illuminate\Http\RedirectResponse;
use App\Http\Controllers\Controller;
use Modules\Store\Enums\StoreStatusEnum;
use Modules\Store\Models\Store;
use Modules\Order\Models\Order;
use Modules\Store\Models\Resources\StoreResource;
use Modules\Complaint\Models\Resources\ComplaintResource;
use Modules\Store\Services\StoreService;
use Modules\Complaint\Services\ComplaintService;
use Modules\Complaint\Enums\ComplaintStatusEnum;
use Modules\Rate\Enums\RateStatusEnum;
use Modules\User\Enums\UserRoleEnum;
use Modules\Rate\Services\RateService;
use Modules\Category\Services\CategoryService;
use Modules\Rate\Models\Resources\RateResource;
use Modules\Category\Models\Resources\CategoryResource;
use Modules\User\Services\AccountService;
use Modules\Store\Requests\Api\FavoriteRequest;
use Modules\Store\Requests\Api\ComplaintRequest;
use Modules\Store\Requests\Api\RateRequest;
use Modules\Store\Requests\Api\SwitchStatusRequest;
use Modules\Store\Requests\Api\EditDateRequest;


class StoreController extends Controller
{
    
    private StoreService $storeService;
    private ComplaintService $complaintService;
    private RateService $rateService;
    private AccountService $accountService ;

    public function __construct(AccountService $accountService , StoreService $storeService , ComplaintService $complaintService,RateService $rateService , CategoryService $categoryService)
    {
        $this->storeService = $storeService;
        $this->complaintService = $complaintService;
        $this->rateService = $rateService;
        $this->categoryService = $categoryService;
        $this->accountService = $accountService;
    }

    /**
     * Display a listing of the Stores.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :JsonResponse
    {
        $account = $this->accountService->getAccount();
        $request->merge(['city_id'=>$account['user']->address->city_id,'status_active'=>1]);
        $stores = $this->storeService->get($request);
        $stores = StoreResource::collection($stores);
        $response = [
            'status'=>200,
            'message'=>'',
            'data'=> $stores
        ];
        return ResponseService::response($response);
    }

    public function show(Request $request,$id) :JsonResponse
    {
        $store = Store::findOrFail($id);
        $store = $this->storeService->getItem($store);
        $store = new StoreResource($store);
        $response = [
            'status'=>200,
            'message'=>'',
            'data'=> $store
        ];
        return ResponseService::response($response);
    }
    
    public function complaints(Request $request,$id)
    {
        $request->merge(['store_id'=>$id,'type'=>ComplaintStatusEnum::USER->value]);
        $complaints = $this->complaintService->get($request);
        $complaints = ComplaintResource::collection($complaints);
        $response = [
            'status'=>200,
            'message'=>'',
            'data'=> $complaints
        ];
        return ResponseService::response($response);
    }
    
    public function complaint(ComplaintRequest $request)
    {
        $order = Order::find($request->order_id);
        $request->merge([
            'status'=> \Auth::user()->role == UserRoleEnum::USER->value ? ComplaintStatusEnum::USER->value : ComplaintStatusEnum::STORE->value,
            'user_id'=>$order->user_id,
            'store_id'=>$order->store_id,
        ]);
        $this->complaintService->save($request,null);
        // $request->request->remove('description');
        // $request->request->remove('reason_id');
        // $request->request->remove('order_id');
        // $request->request->remove('status');
        // $request->request->remove('user_id');
        $request->merge(['store_id'=>$order->store_id,'type'=>ComplaintStatusEnum::USER->value]);

        $complaints = $this->complaintService->get($request);

        $complaints = ComplaintResource::collection($complaints);
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> []
        ];
        return ResponseService::response($response);
    }
    
    public function rates(Request $request,$id)
    {
        $request->merge(['store_id'=>$id,'type'=>RateStatusEnum::USER->value]);
        $rates = $this->rateService->get($request);
        $rates = RateResource::collection($rates);
        $response = [
            'status'=>200,
            'message'=>'',
            'data'=> $rates
        ];
        return ResponseService::response($response);
    }
    
    public function rate(RateRequest $request)
    {
        $order = Order::find($request->order_id);
        $request->merge([
            'status'=> \Auth::user()->role == UserRoleEnum::USER->value ? RateStatusEnum::USER->value : RateStatusEnum::STORE->value,
            'user_id'=>$order->user_id,
            'store_id'=>$order->store_id,
        ]);
        $this->rateService->save($request,null);
        // $request->request->remove('comment');
        // $request->request->remove('rate');
        // $request->request->remove('reason_id');
        // $request->request->remove('order_id');
        // $request->request->remove('status');
        // $request->request->remove('user_id');
        $request->merge(['store_id'=>$order->store_id,'type'=>RateStatusEnum::USER->value]);
        $rates = $this->rateService->get($request);
        $rates = RateResource::collection($rates);
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> $rates
        ];
        return ResponseService::response($response);
    }
    
    public function categories(Request $request,$id)
    {
        $request->merge(['store_id'=>$id]);
        $categories = $this->categoryService->get($request);
        $categories = CategoryResource::collection($categories);
        $response = [
            'status'=>200,
            'message'=>'',
            'data'=> $categories
        ];
        return ResponseService::response($response);
    }
    
    public function switchStatus(SwitchStatusRequest $request)
    {
        $store = Store::find(auth()->user()->store_id);
        $store = $this->storeService->getItem($store);
        $this->storeService->save($request,$store);
        
        $store = $this->storeService->getItem($store);
        $store = new StoreResource($store);
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> $store
        ];
        return ResponseService::response($response);
    }
    
    public function editDate(EditDateRequest $request,$id)
    {
        $account = $this->accountService->getAccount();
        
        $store = $this->storeService->updateWorkTime($request,$id);
        
        $store = Store::find(auth()->user()->store_id);
        $store = $this->storeService->getItem($store);
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> new StoreResource($store)
        ];
        return ResponseService::response($response);
    }
    
    public function favorite(FavoriteRequest $request)
    {
        $account = $this->accountService->getAccount();
        $request->merge(['user_id'=>$account['user']->id,'status'=>$account['status']]);
        $store_ids = $this->storeService->favorite($request);
        $request->merge(['ids'=>$store_ids,'user_id'=>null,'status'=>null]);
        $store = $this->storeService->get($request);
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> StoreResource::collection($store)
        ];
        return ResponseService::response($response);
    }
    public function favorites(Request $request)
    {
        $account = $this->accountService->getAccount();
        $request->merge(['user_id'=>$account['user']->id,'status'=>$account['status']]);
        $store_ids = $this->storeService->favorites($request);
        $request->merge(['ids'=>$store_ids,'user_id'=>null,'status'=>null]);
        $stores = $this->storeService->get($request);

        $response = [
            'status'=>200,
            'message'=>'',
            'data'=> StoreResource::collection($stores)
        ];
        return ResponseService::response($response);
    }
    
}