<?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\User;

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

class UserController extends Controller
{
    public function get_signedup_customers_list($ServerName)
    {
        $customers_list = User::orderBy('created_at', 'desc')->where('ProcessStatus', 'Inactive')->get();
        return response()->json([
            'status' => true,
            'message' => 'Signed Up Customers List',
            'customers_list' => $customers_list
        ]);
    }


    public function get_suppliers_list($ServerName)
    {
        $suppliers_list = User::where('UserType', 'supplier')->latest()->get();
        return response()->json([
            'status' => true,
            'message' => 'Suppliers List',
            'suppliers_list' => $suppliers_list
        ]);
    }


    // public function addNewCustomer(Request $request)
    // {
    //     try {
    //         // Validate the request data
    //         $validator = Validator::make($request->all(), [
    //             'ExistingUser' => 'required',
    //             // 'CustomerNo' => 'required',
    //             // 'InvoiceNo' => 'required',
    //             'FirstName' => 'required',
    //             'SurName' => 'required',
    //             'HouseName' => 'required',
    //             'StreetNo' => 'required',
    //             'StreetName' => 'required',
    //             'Town' => 'required',
    //             'Area' => 'required',
    //             'County' => 'required',
    //             'PostCode' => 'required',
    //             'email' => 'required|email|unique:users,email',
    //             'Username' => 'required|unique:users,Username',
    //             'password' => 'required',
    //             'CustomerType' => '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 = User::create($request->all());

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





    // public function updateCustomer(Request $request, $id)
    // {
    //     try {
    //         // Validate the request data
    //         $validator = Validator::make($request->all(), [
    //             'ExistingUser' => 'required',
    //             // 'CustomerNo' => 'required',
    //             // 'InvoiceNo' => 'required',
    //             'FirstName' => 'required',
    //             'SurName' => 'required',
    //             'HouseName' => 'required',
    //             'StreetNo' => 'required',
    //             'StreetName' => 'required',
    //             'Town' => 'required',
    //             'Area' => 'required',
    //             'County' => 'required',
    //             'PostCode' => 'required',
    //             'email' => 'required|email|unique:users,email,' . $id,
    //             'Username' => 'required|unique:users,Username,' . $id,
    //             'password' => 'required',
    //             'CustomerType' => 'required',
    //         ]);

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

    //         // Validation passed, find the user
    //         $customer = User::findOrFail($id);

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

    //         return response()->json([
    //             'status' => true,
    //             'message' => 'Customer updated successfully',
    //             'data' => $customer,
    //         ]);
    //     } 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 updating user',
    //             'error' => $e->getMessage(),
    //         ], 200);
    //     }
    // }



    public function updateShowPriceStatus($id, $status)
    {
        try {
            $User = User::findOrFail($id);

            if ($status == 'Active') {
                $User->show_prices = 1;
            } else {
                $User->show_prices = 0;
            }

            $User->save();

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



    public function updateAccountStatus($id, $status)
    {

        try {
            $User = User::findOrFail($id);

            $User->ProcessStatus = $status;
            $User->save();

            $companySettings = CompanySetting::find(1);
            $send = $this->sendActivationEmail($User, $companySettings);


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

    private function sendActivationEmail($user, $companySettings)
    {

        $mail = new PHPMailer(true);

        try {

            $mail->isMail();

            //Recipients
            $mail->setFrom($companySettings->EmailAddress, $companySettings->name);
            $mail->addAddress($user->email);
            $mail->addBCC($companySettings->EmailAddress);

            //Content
            $mail->isHTML(true);
            //Set email format to HTML
            $mail->Subject = 'Account has been activated';

            $mail->Body = "
                <html>
                    <head>
                        <style>
                            table {
                                border-collapse: collapse;
                                width: 100%;
                            }

                            th, td {
                                border: 1px solid #dddddd;
                                text-align: left;
                                padding: 8px;
                            }

                            th {
                                background-color: #f2f2f2;
                            }
                        </style>
                    </head>
                    <body>
                        <p>Dear " . $user->FirstName . "</p>
                        <p>Your account has been activated, you can login.</p>
                        <p>Regards,<br>
                        " . $companySettings->Name . "<br>
                        " . $companySettings->Address . "<br>
                        </p>
                    </body>
                </html>";

            $send = $mail->send();
            return $send;
            // dd($send);

        } catch (Exception $e) {
            // Handle exception if the email fails to send
            // Log or notify as needed
            // dd($e);
            return $e;
        }
    }
}
