<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Customer;
use App\Models\Order;
use Illuminate\Support\Facades\DB;
use Stichoza\GoogleTranslate\GoogleTranslate;

class CustomerController extends Controller
{
    public function search(Request $request){
      $search = trim($request->search);

       if ($search == '') {
           return response()->json([]);
       }

       $customers = Customer::select(
               'id',
               'name',
               'phone',
               'phone2',
               'address',
               'opening_balance'
           )
           ->where(function ($query) use ($search) {

               $query->where('name', 'LIKE', "%{$search}%")
                     ->orWhere('phone', 'LIKE', "%{$search}%")
                     ->orWhere('phone2', 'LIKE', "%{$search}%");

           })
           ->orderBy('name')
           ->limit(10)
           ->get();

       return response()->json($customers);
   }

   public function customer_list(Request $request)  {

    $customers = Customer::query();

    if ($request->filled('search')) {
        $search = trim($request->search);

        $customers->where(function ($query) use ($search) {
            $query->where('name', 'like', "%{$search}%")
                  ->orWhere('phone', 'like', "%{$search}%");
        });
    }
    
    if ($request->filled('status')) {
        $customers->where('status', $request->status);
    }

    $customers = $customers->orderBy('id', 'desc')
        ->paginate(10);

    return view('Admin.partials.customer_list', compact('customers'));

   }

   public function add_customer(){
    return View('Admin.partials.add_customer');
   }

   public function add_new_customer(Request $request){

       DB::beginTransaction();
         //========
        // Customer
        // ===========================
        $translator = new GoogleTranslate(
          target: 'ur',
          source: 'auto',
          options: [
              'verify' => false,
          ]
           );

            $customer = Customer::create([

                    'name' => $request->name,
                    'urdu_name'  => $translator->translate($request->name),
                    'phone' => $request->phone,
                    'phone2' => $request->phone2,
                    'email' => $request->email,
                    'address' => $request->address,
                    'urdu_address'=> $translator->translate($request->address),
                    'customer_type' => $request->customer_type,

                ]);

     return back()->with('success', 'Customer Added Successfully.');

   }

    public function customer_update($id){
     $customers=Customer::find($id);
      return View('Admin.partials.update_customer',compact('customers'));


    }

   public function update_customer(Request $request,$id){
        $customer = Customer::findOrFail($id);

        $request->validate([
           'name'                => 'required|string|max:255',
           'customer_name_urdu'  => 'required|string|max:255',
           'phone'               => 'required|string|max:20',
           'phone2'              => 'nullable|string|max:20',
           'address'             => 'required|string|max:1000',
           'address_urdu'        => 'required|string|max:1000',
           'status'              => 'required|in:0,1',
           'customer_type'       => 'required|in:regular,walkin,wholesale',
        ]);

        $customer->update([
        'name'           => $request->name,
        'urdu_name'      => $request->customer_name_urdu,
        'phone'          => $request->phone,
        'phone2'         => $request->phone2,
        'address'        => $request->address,
        'status'         => $request->status,
        'customer_type'  => $request->customer_type,
        'urdu_address'   => $request->address_urdu,
    ]);
         return back()->with('success', 'Customer Updated Successfully.');
   }

    public function customer_records($id){

       $customer = Customer::findOrFail($id);

       $orders = Order::where('customer_id', $id)
                     ->latest()
                     ->get();

       return view('Admin.partials.customer_bills', compact('customer', 'orders'));

    }
    public function order_detail($id){
       $order = Order::with([
          'customer',
          'details.product'
      ])->findOrFail($id);

      return view('Admin.partials.customer_order_detail', compact('order'));
    }
}
