<?php

namespace App\Http\Controllers\Evo;

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

use Illuminate\Support\Facades\Validator;
use App\Models\CompanySetting;
use App\Models\Customer;

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

use Illuminate\Database\Eloquent\ModelNotFoundException;

class EvoCustomerController extends Controller
{
    public function addNewCustomer(Request $request)
    {
        try {
            // Validate the request data
            $validator = Validator::make($request->all(), [
                // 'ExistingUser' => 'required',
                'CustomerNo' => 'required|string|unique:customers,CustomerNo',
                'FirstName' => 'required',
                'SurName' => 'required',
                'HouseName' => 'nullable|string|max:255',
                'StreetNo' => 'nullable|string|max:255',
                'StreetName' => 'nullable|string|max:255',
                'Town' => 'nullable|string|max:255',
                'Area' => 'nullable|string|max:255',
                'County' => 'nullable|string|max:255',
                'PostCode' => 'nullable|string|max:255',
                'EmailAddress' => 'nullable|string|email|max:255',
                'CustomerType' => 'nullable|string|max:255',
                'ContactNo' => 'nullable|string|max:255',
                'ShowPrice' => 'nullable',
            ], [

                'CustomerNo.required' => "The Customer Number field is required.",
                'FirstName.required' => "The First Name field is required.",
                'SurName.required' => "The Surname field is required.",
                // 'HouseName.required' => "The House Name field is required.",
                // 'StreetNo.required' => "The Street Number field is required.",
                // 'StreetName.required' => "The Street Name field is required.",
                // 'Town.required' => "The Town field is required.",
                // 'Area.required' => "The Area field is required.",
                // 'County.required' => "The County field is required.",
                // 'PostCode.required' => "The Post Code field is required.",
                // 'EmailAddress.required' => "The Email field is required.",
                // 'EmailAddress.email' => "The Email must be a valid email address.",
                // 'CustomerType.required' => "The Customer Type field is required.",
                // 'ContactNo.required_if' => "The Contact Number field is required.",
            ]);

            // Check if validation fails
            if ($validator->fails()) {
                return response()->json([
                    'status' => false,
                    'message' => 'Validation Error',
                    'errors' => $validator->errors(),
                ], 422);
            }

            // Validation passed, create the user
            $user = Customer::create($request->all());

            return response()->json([
                'status' => true,
                'message' => 'Customer added successfully',
                'data' => $user,
            ], 201);
        } catch (ModelNotFoundException $e) {
            // Handle the case where the model (User) is not found
            return response()->json([
                'status' => false,
                'message' => 'Customer not found',
            ], 404);
        } catch (\Exception $e) {
            // Handle other exceptions
            return response()->json([
                'status' => false,
                'message' => 'Error adding Customer',
                'error' => $e->getMessage(),
            ], 500);
        }
    }




    public function updateCustomer(Request $request, $CustomerNo)
    {
        try {
            // Find the customer by CustomerNo
            $customer = Customer::where('CustomerNo', $CustomerNo)->firstOrFail();

            // dd($customer);

            // Validate the request data
            $validator = Validator::make($request->all(), [
                'CustomerNo' => 'required|string|unique:customers,CustomerNo,' . $customer->id,
                'FirstName' => 'required|string|max:255',
                'SurName' => 'required|string|max:255',
                'HouseName' => 'nullable|string|max:255',
                'StreetNo' => 'nullable|string|max:255',
                'StreetName' => 'nullable|string|max:255',
                'Town' => 'nullable|string|max:255',
                'Area' => 'nullable|string|max:255',
                'County' => 'nullable|string|max:255',
                'PostCode' => 'nullable|string|max:255',
                'EmailAddress' => 'nullable|string|email|max:255',
                'CustomerType' => 'nullable|string|max:255',
                'ContactNo' => 'nullable|string|max:255',
                'ShowPrice' => 'nullable',
            ], [
                'CustomerNo.required' => "The Customer Number field is required.",
                'FirstName.required' => "The First Name field is required.",
                'SurName.required' => "The Surname field is required.",
            ]);

            // Check if validation fails
            if ($validator->fails()) {
                return response()->json([
                    'status' => false,
                    'message' => 'Validation Error',
                    'errors' => $validator->errors(),
                ], 422);
            }

            // Update the customer
            $customer->update($request->all());

            return response()->json([
                'status' => true,
                'message' => 'Customer updated successfully',
                'data' => $customer,
            ]);
        } catch (ModelNotFoundException $e) {
            // Handle the case where the model (Customer) is not found
            return response()->json([
                'status' => false,
                'message' => 'Customer not found',
            ], 404);
        } catch (\Exception $e) {
            // Handle other exceptions
            return response()->json([
                'status' => false,
                'message' => 'Error updating customer',
                'error' => $e->getMessage(),
            ], 500);
        }
    }
}