<?php

namespace Yas\FrontTheme\Http\Controllers\Customer;

use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Storage;
use Webkul\Core\Repositories\SubscribersListRepository;
use Webkul\Customer\Repositories\CustomerRepository;
use Webkul\Product\Repositories\ProductReviewRepository;
use Webkul\Sales\Models\Order;
use Yas\FrontTheme\Http\Controllers\Controller;
use Yas\FrontTheme\Http\Requests\Customer\ProfileRequest;
use Yas\FrontTheme\Http\Requests\ConsignmentFormRequest;
use Yas\FrontTheme\Repositories\ConsignmentEnquiryRepository;
use Yas\FrontTheme\Repositories\ConsignmentImagesRepository;
use Webkul\Theme\Repositories\ThemeCustomizationRepository;

class CustomerController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct(
        protected CustomerRepository $customerRepository,
        protected ProductReviewRepository $productReviewRepository,
        protected SubscribersListRepository $subscriptionRepository,
        protected ConsignmentEnquiryRepository $consignmentEnquiryRepository,
        protected ConsignmentImagesRepository $consignmentImagesRepository,
        protected ThemeCustomizationRepository $themeCustomizationRepository,
    ) {}

    /**
     * Using const variable for status
     */
    const STATUS_APPROVED = 'approved';

    const STATUS_PENDING = 'pending';

    /**
     * Taking the customer to profile details page.
     *
     * @return \Illuminate\View\View
     */
    public function index()
    {
        $customer = $this->customerRepository->find(auth()->guard('customer')->user()->id);
        return view('yas_theme::customers.account.profile.index', compact('customer'));
    }

    /**
     * For loading the edit form page.
     *
     * @return \Illuminate\View\View
     */
    public function edit()
    {
        $customer = $this->customerRepository->find(auth()->guard('customer')->user()->id);

        return view('yas_theme::customers.account.profile.edit', compact('customer'));
    }

    /**
     * Edit function for editing customer profile.
     *
     * @return \Illuminate\Http\Response
     */
    public function update(ProfileRequest $profileRequest)
    {
    //    / dd(request()->all());
        $isPasswordChanged = false;

        $data = $profileRequest->validated();

        if (empty($data['date_of_birth'])) {
            unset($data['date_of_birth']);
        }

        if (
            core()->getCurrentChannel()->theme === 'default'
            && ! isset($data['image'])
        ) {
            $data['image']['image_0'] = '';
        }

        $data['subscribed_to_news_letter'] = isset($data['subscribed_to_news_letter']);

        if (! empty($data['current_password'])) {
            if (Hash::check($data['current_password'], auth()->guard('customer')->user()->password)) {
                $isPasswordChanged = true;

                $data['password'] = bcrypt($data['new_password']);
            } else {
                session()->flash('warning', trans('yas_theme::app.customers.account.profile.index.unmatched'));

                return redirect()->back();
            }
        } else {
            unset($data['new_password']);
        }

        Event::dispatch('customer.update.before');

        if ($customer = $this->customerRepository->update($data, auth()->guard('customer')->user()->id)) {
            if ($isPasswordChanged) {
                Event::dispatch('customer.password.update.after', $customer);
            }

            Event::dispatch('customer.update.after', $customer);

            if ($data['subscribed_to_news_letter']) {
                $subscription = $this->subscriptionRepository->findOneWhere(['email' => $data['email']]);

                if ($subscription) {
                    $this->subscriptionRepository->update([
                        'customer_id'   => $customer->id,
                        'is_subscribed' => 1,
                    ], $subscription->id);
                } else {
                    $this->subscriptionRepository->create([
                        'email'         => $data['email'],
                        'customer_id'   => $customer->id,
                        'channel_id'    => core()->getCurrentChannel()->id,
                        'is_subscribed' => 1,
                        'token'         => $token = uniqid(),
                    ]);
                }
            } else {
                $subscription = $this->subscriptionRepository->findOneWhere(['email' => $data['email']]);

                if ($subscription) {
                    $this->subscriptionRepository->update([
                        'customer_id'   => $customer->id,
                        'is_subscribed' => 0,
                    ], $subscription->id);
                }
            }

            if (request()->hasFile('image')) {
                $this->customerRepository->uploadImages($data, $customer);
            } else {
                if (isset($data['image'])) {
                    if (! empty($data['image'])) {
                        Storage::delete((string) $customer->image);
                    }

                    $customer->image = null;

                    $customer->save();
                }
            }

            session()->flash('success', trans('yas_theme::app.customers.account.profile.index.edit-success'));

            return redirect()->route('yas_theme.customers.account.profile.index');
        }

        session()->flash('success', trans('yas_theme::app.customer.account.profile.edit-fail'));

        return redirect()->back('yas_theme.customers.account.profile.edit');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy()
    {
        $this->validate(request(), [
            'password' => 'required',
        ]);

        $customerRepository = $this->customerRepository->findorFail(auth()->guard('customer')->user()->id);

        try {
            if (Hash::check(request()->input('password'), $customerRepository->password)) {
                if ($customerRepository->orders->whereIn('status', [Order::STATUS_PENDING, Order::STATUS_PROCESSING])->first()) {
                    session()->flash('error', trans('yas_theme::app.customers.account.profile.index.order-pending'));

                    return redirect()->route('yas_theme.customers.account.profile.index');
                }

                $this->customerRepository->delete(auth()->guard('customer')->user()->id);

                session()->flash('success', trans('yas_theme::app.customers.account.profile.index.delete-success'));

                return redirect()->route('yas_theme.customer.session.index');
            }

            session()->flash('error', trans('yas_theme::app.customers.account.profile.index.wrong-password'));

            return redirect()->back();
        } catch (\Exception $e) {
            session()->flash('error', trans('yas_theme::app.customers.account.profile.index.delete-failed'));

            return redirect()->route('yas_theme.customers.account.profile.index');
        }
    }

    /**
     * Load the view for the customer account panel, showing approved reviews.
     *
     * @return \Illuminate\View\View
     */
    public function reviews()
    {
        $reviews = $this->productReviewRepository->getCustomerReview();

        return view('yas_theme::customers.account.reviews.index', compact('reviews'));
    }

    /**
     * Taking the customer to account details page.
     *
     * @return \Illuminate\View\View
     */
    public function account()
    {
        return view('yas_theme::customers.account.index');
    }

        /**
     * Summary of consignment_form.
     *
     * @return \Illuminate\View\View
     */
    public function consignmnetForm()
    {
        $uniqueNo = date('YmdHis');
        $customer = $this->customerRepository->find(auth()->guard('customer')->user()->id);

        $consignmentEnquiryNo = $uniqueNo.$customer->id;

        $consignmentAddressText = $this->themeCustomizationRepository->findWhere([
            'name'=>'consignment-form-address'
        ]);

        $consignmentFooterText = $this->themeCustomizationRepository->findWhere([
            'name'=>'consignment-form-footer'
        ]);

        return view('yas_theme::customers.consignment-form',compact('consignmentAddressText','consignmentEnquiryNo','consignmentFooterText'));
    }

    /**
     * Summary of store.
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function consignmnetFormSubmit(ConsignmentFormRequest $consignmentRequest)
    {
        try {
            $params = $consignmentRequest->all();
            
            $customer = $this->customerRepository->find(auth()->guard('customer')->user()->id);

            $fields = $consignmentRequest->only([
                'consignment_no',
                'name',
                'email',
                'contact',
                'item_type',
                'item_description',
                'brand',
                'model',
                'material',
                'color',
                'hardware',
                'stamp',
                'condition',
                'price',
                'b','db','ss','c','l','k','or','cr','sr','cc','ac','rc','nric',
                'consignment_date'
            ]);

            $fields['item_type'] = isset($fields['item_type']) ? $fields['item_type']: 'simple';
            $fields['customer_id'] = $customer->id;
            $fields['status']   = self::STATUS_PENDING;

            //dd($fields);

           $consignmentEnquiry = $this->consignmentEnquiryRepository->create($fields);

            if (request()->hasFile('images')) {
                
                $this->consignmentImagesRepository->uploadImages($params, $consignmentEnquiry->id, 'images');
            }
            session()->flash('success', trans('yas_theme::app.home.consignment.thanks-for-consignmnet'));
        } catch (\Exception $e) {
            session()->flash('error', $e->getMessage());

            report($e);
        }

        return back();
    }

    

    
}
