<?php

namespace App\Http\Controllers\Evo;

use App\Models\AppOrder;
use App\Models\AppOrderItem;
use App\Models\Product;
use App\Models\CompanySetting;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Config;
// use PHPMailer\PHPMailer\PHPMailer;
// use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

class EvoOrderController extends Controller
{
    public function getOrdersByCustomerNo($customerNo)
    {
        // $customerNo = auth()->user()->CustomerNo;
        $companySettings = Config::get('company_settings');

        try {
            $orders = AppOrder::where('CustomerNo', $customerNo)->latest()->get();

            if ($orders->isEmpty()) {
                return response()->json(['status' => false, 'message' => 'No orders found for CustomerNo ' . $customerNo], 200);
            }

            return response()->json([
                'status' => true,
                'companySettings' => $companySettings,
                'data' => $orders],  200);
        } catch (ModelNotFoundException $e) {
            return response()->json(['status' => false, 'message' => 'Customer not found'], 200);
        }
    }


    public function updateOrderStatus($orderId, $newStatus)
    {


        try {
            // Find the order by ID
            $order = AppOrder::findOrFail($orderId);

            // Update the ProcessStatus field
            $order->ProcessStatus = $newStatus;

            // Save the changes to the database
            $order->save();

            return response()->json([
                'status' => true,
                'message' => 'Process Status updated successfully',
                'data' => $order
            ], 200);
        } catch (\Exception $e) {
            return response()->json([
                'status' => false,
                'message' => 'Failed to update Process Status',
                'error' => $e->getMessage()
            ], 200);
        }

    }


}