<?php

namespace App\Http\Controllers\Api;

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

class CustomerController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $customers = Customer::all();
        return response()->json($customers);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        try {
            $request->validate([
                'customer_name' => 'nullable|string|max:255',
            'mobile_number' => 'nullable|string|max:20',
               ]);

            // Check if customer exists by mobile number or name
            $existingCustomer = Customer::where(function ($query) use ($request) {
                if ($request->filled('mobile_number')) {
                    $query->orWhere('mobile_number', $request->mobile_number);
                }
                if ($request->filled('customer_name')) {
                    $query->orWhere('customer_name', $request->customer_name);
                }
            })->first();

            if ($existingCustomer) {
                return response()->json([
                    'message' => 'Customer already exists.',
                    'customer' => $existingCustomer
                ], 200);
            }

            $customer = Customer::create($request->all());
            return response()->json([
            'message' => 'Customer created successfully.',
            'customer' => $customer
        ], 201);
        } catch (ValidationException $e) {
            return response()->json(['errors' => $e->errors()], 422);
        } catch (\Exception $e) {
            return response()->json(['message' => 'Error creating customer', 'error' => $e->getMessage()], 500);
        }
    }

    /**
     * Display the specified resource.
     */
    public function show(Customer $customer)
    {
        return response()->json($customer);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, Customer $customer)
    {
        try {
            $request->validate([
                'customer_name' => 'sometimes|nullable|string|max:255',
                'mobile_number' => 'sometimes|nullable|string|max:20|unique:customers,mobile_number,' . $customer->customer_id . ',customer_id',
            ]);

            $customer->update($request->all());
            return response()->json($customer);
        } catch (ValidationException $e) {
            return response()->json(['errors' => $e->errors()], 422);
        } catch (\Exception $e) {
            return response()->json(['message' => 'Error updating customer', 'error' => $e->getMessage()], 500);
        }
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Customer $customer)
    {
        try {
            $customer->delete();
            return response()->json(['message' => 'Customer deleted successfully'], 204);
        } catch (\Exception $e) {
            return response()->json(['message' => 'Error deleting customer', 'error' => $e->getMessage()], 500);
        }
    }
}
