<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use App\Models\Customer;
use Illuminate\Http\Request;

class CustomerController extends Controller
{
    public function index(Request $request)
    {
        $q = Customer::where('user_id', auth()->id());

        if ($request->filled('search')) {
            $s = $request->search;
            $q->where(function ($qq) use ($s) {
                $qq->where('name', 'like', "%$s%")
                   ->orWhere('phone', 'like', "%$s%");
            });
        }

        if ($request->filled('status')) {
            $q->where('status', $request->status);
        }

        $customers = $q->latest()->paginate(15)->withQueryString();

        return view('user.customers.index', compact('customers'));
    }

    public function create()
    {
        return view('user.customers.create');
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'name' => ['required', 'string', 'max:255'],
            'phone' => ['required', 'string', 'max:50'],
            'address' => ['nullable', 'string', 'max:2000'],
            'opening_balance' => ['nullable', 'numeric', 'min:0'],
            'status' => ['required', 'in:active,inactive'],
        ], [
            'name.required' => __('Customer_Controller.validation.name_required'),
            'name.string' => __('Customer_Controller.validation.name_string'),
            'name.max' => __('Customer_Controller.validation.name_max'),

            'phone.required' => __('Customer_Controller.validation.phone_required'),
            'phone.string' => __('Customer_Controller.validation.phone_string'),
            'phone.max' => __('Customer_Controller.validation.phone_max'),

            'address.string' => __('Customer_Controller.validation.address_string'),
            'address.max' => __('Customer_Controller.validation.address_max'),

            'opening_balance.numeric' => __('Customer_Controller.validation.opening_balance_numeric'),
            'opening_balance.min' => __('Customer_Controller.validation.opening_balance_min'),

            'status.required' => __('Customer_Controller.validation.status_required'),
            'status.in' => __('Customer_Controller.validation.status_in'),
        ]);

        $data['user_id'] = auth()->id();
        $data['opening_balance'] = $data['opening_balance'] ?? 0;

        Customer::create($data);

        return redirect()
            ->route('user.customers.index')
            ->with('ok', __('Customer_Controller.messages.customer_added_successfully'));
    }

    public function edit($id)
    {
        $customer = Customer::where('user_id', auth()->id())
            ->findOrFail($id);

        return view('user.customers.edit', compact('customer'));
    }

    public function update(Request $request, $id)
    {
        $customer = Customer::where('user_id', auth()->id())
            ->findOrFail($id);

        $data = $request->validate([
            'name' => ['required', 'string', 'max:255'],
            'phone' => ['required', 'string', 'max:50'],
            'address' => ['nullable', 'string', 'max:2000'],
            'opening_balance' => ['nullable', 'numeric', 'min:0'],
            'status' => ['required', 'in:active,inactive'],
        ], [
            'name.required' => __('Customer_Controller.validation.name_required'),
            'name.string' => __('Customer_Controller.validation.name_string'),
            'name.max' => __('Customer_Controller.validation.name_max'),

            'phone.required' => __('Customer_Controller.validation.phone_required'),
            'phone.string' => __('Customer_Controller.validation.phone_string'),
            'phone.max' => __('Customer_Controller.validation.phone_max'),

            'address.string' => __('Customer_Controller.validation.address_string'),
            'address.max' => __('Customer_Controller.validation.address_max'),

            'opening_balance.numeric' => __('Customer_Controller.validation.opening_balance_numeric'),
            'opening_balance.min' => __('Customer_Controller.validation.opening_balance_min'),

            'status.required' => __('Customer_Controller.validation.status_required'),
            'status.in' => __('Customer_Controller.validation.status_in'),
        ]);

        $data['opening_balance'] = $data['opening_balance'] ?? 0;

        $customer->update($data);

        return redirect()
            ->route('user.customers.index')
            ->with('ok', __('Customer_Controller.messages.customer_updated_successfully'));
    }

    public function destroy($id)
    {
        $customer = Customer::where('user_id', auth()->id())
            ->findOrFail($id);

        $customer->delete();

        return redirect()
            ->route('user.customers.index')
            ->with('ok', __('Customer_Controller.messages.customer_deleted_successfully'));
    }
}