<?php

namespace Modules\Customer\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Modules\Admin\Repositories\FieldInterface;
use Modules\Admin\Services\AdminService;
use Modules\Customer\Repositories\CustomerInterface;
use Modules\User\Repositories\UserInterface;

class CustomerController extends Controller
{
    private $userRepository;
    private $customerRepository;

    private $adminService;
    private $fieldRepository;

    public function __construct(
        FieldInterface $fieldRepository,
        UserInterface $userRepository,
        AdminService $adminService,
        CustomerInterface $customerRepository) {
        $this->userRepository = $userRepository;
        $this->adminService = $adminService;
        $this->fieldRepository = $fieldRepository;
        $this->customerRepository = $customerRepository;
    }

    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $data['title'] = 'Customer List';
        $data['customers'] = $this->customerRepository->findAll();
        return view('customer::customer.index', $data);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        $data['title'] = 'Create Customer';
        $data['nationalityList'] = $this->fieldRepository->getDropdownByAlias('nationality');
        return view('customer::customer.create', $data);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request): RedirectResponse
    {
        $inputData = $request->all();
        $this->customerRepository->create($inputData);
        toastr()->success('Customer Created Succesfully');

        try {

            if ($request->hasFile('profile_picture')) {
                $inputData['profile_picture'] = uploadImage($request->profile_picture);
            }

            sendNotification(auth()->user(), [
                'msg' => 'Customer Created',
            ]);

        } catch (\Throwable $th) {
            echo $th->getMessage();
            toastr()->error($th->getMessage());
        }

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

    }

    /**
     * Show the specified resource.
     */
    public function show($id)
    {
        $data['title'] = 'Show Customer';
        $data['customer'] = $this->customerRepository->getCustomerById($id);
        return view('customer::customer.show', $data);
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit($id)
    {
        $data['title'] = 'Edit Customer';
        $data['customer'] = $this->customerRepository->getCustomerById($id);
        $data['departmentList'] = $this->adminService->pluckDepartments();
        $data['designationList'] = $this->adminService->pluckDesignations();
        $data['nationalityList'] = $this->fieldRepository->getDropdownByAlias('nationality');
        $data['genderList'] = $this->fieldRepository->getDropdownByAlias('gender');
        $data['casteList'] = $this->fieldRepository->getDropdownByAlias('caste');
        $data['cityList'] = $this->adminService->pluckCities();
        return view('customer::customer.edit', $data);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, $id): RedirectResponse
    {
        $inputData = $request->except(['_method', '_token']);
        try {
            if ($request->hasFile('profile_picture')) {
                $inputData['profile_picture'] = uploadImage($request->profile_picture);
            }
            $this->customerRepository->update($id, $inputData);

            sendNotification(auth()->user(), [
                'msg' => 'Customer Updated',
            ]);

            toastr()->success('Customer Updated Succesfully');
        } catch (\Throwable $th) {
            toastr()->error($th->getMessage());
        }
        return redirect()->route('customer.index');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy($id)
    {
        try {
            $CustomerModel = $this->customerRepository->getCustomerById($id);
            $CustomerModel->delete();

            toastr()->success('Customer Delete Succesfully');
        } catch (\Throwable $th) {
            toastr()->error($th->getMessage());
        }

        return response()->json(['status' => true, 'message' => 'Customer Delete Succesfully']);

    }
}