<?php

namespace App\Http\Controllers;

use App\Business;
use App\Currency;
use App\Notifications\TestEmailNotification;
use App\System;
use App\TaxRate;
use App\Unit;
use App\User;
use App\Utils\BusinessUtil;
use App\Utils\ModuleUtil;
use App\Utils\RestaurantUtil;
use Carbon\Carbon;
use DateTimeZone;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Spatie\Permission\Models\Permission;

class BusinessController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | BusinessController
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new business/business as well as their
    | validation and creation.
    |
    */

    /**
     * All Utils instance.
     *
     */
    protected $businessUtil;
    protected $restaurantUtil;
    protected $moduleUtil;
    protected $mailDrivers;

    /**
     * Constructor
     *
     * @param ProductUtils $product
     * @return void
     */
    public function __construct(BusinessUtil $businessUtil, RestaurantUtil $restaurantUtil, ModuleUtil $moduleUtil)
    {
        $this->businessUtil = $businessUtil;
        $this->moduleUtil = $moduleUtil;

        $this->theme_colors = [
            'blue' => 'Blue',
            'black' => 'Black',
            'purple' => 'Purple',
            'green' => 'Green',
            'red' => 'Red',
            'yellow' => 'Yellow',
            'blue-light' => 'Blue Light',
            'black-light' => 'Black Light',
            'purple-light' => 'Purple Light',
            'green-light' => 'Green Light',
            'red-light' => 'Red Light',
        ];

        $this->mailDrivers = [
            'smtp' => 'SMTP',
            // 'sendmail' => 'Sendmail',
            // 'mailgun' => 'Mailgun',
            // 'mandrill' => 'Mandrill',
            // 'ses' => 'SES',
            // 'sparkpost' => 'Sparkpost'
        ];
    }

    /**
     * Shows registration form
     *
     * @return \Illuminate\Http\Response
     */
    public function getRegister()
    {
        if (!config('constants.allow_registration')) {
            return redirect('/');
        }

        // No GET, o pacote vem pela URL: /business/register?package=1
        $package_id = request()->get('package', null);

        $is_installed_superadmin = $this->moduleUtil->isSuperadminInstalled();

        if ($is_installed_superadmin && empty($package_id)) {
            return redirect()->route('pricing')->with('status', [
                'success' => 0,
                'msg' => 'Selecione um plano antes de criar sua conta.'
            ]);
        }

        if ($is_installed_superadmin) {
            $package = \Modules\Superadmin\Entities\Package::active()
                ->notPrivate()
                ->find($package_id);

            if (empty($package)) {
                return redirect()->route('pricing')->with('status', [
                    'success' => 0,
                    'msg' => 'Plano inválido ou indisponível. Selecione um plano válido.'
                ]);
            }
        }

        $currencies = $this->businessUtil->allCurrencies();
        $timezone_list = $this->businessUtil->allTimeZones();

        $months = [];
        for ($i = 1; $i <= 12; $i++) {
            $months[$i] = __('business.months.' . $i);
        }

        $accounting_methods = $this->businessUtil->allAccountingMethods();

        $system_settings = System::getProperties([
            'superadmin_enable_register_tc',
            'superadmin_register_tc'
        ], true);

        return view('business.register', compact(
            'currencies',
            'timezone_list',
            'months',
            'accounting_methods',
            'package_id',
            'system_settings'
        ));
    }
    /**
     * Handles the registration of a new business and it's owner
     *
     * @return \Illuminate\Http\Response
     */
    public function postRegister(Request $request)
    {
        if (!config('constants.allow_registration')) {
            return redirect('/');
        }

        $is_installed_superadmin = $this->moduleUtil->isSuperadminInstalled();
        $package_id = $request->get('package_id', null);

        if ($is_installed_superadmin && empty($package_id)) {
            return redirect()->route('pricing')->with('status', [
                'success' => 0,
                'msg' => 'Selecione um plano antes de criar sua conta.'
            ]);
        }

        if ($is_installed_superadmin) {
            $package = \Modules\Superadmin\Entities\Package::active()
                ->notPrivate()
                ->find($package_id);

            if (empty($package)) {
                return redirect()->route('pricing')->with('status', [
                    'success' => 0,
                    'msg' => 'Plano inválido ou indisponível. Selecione um plano válido.'
                ]);
            }
        }
        try {
            $validator = $request->validate(
                [
                    'name' => 'required|max:255',
                    'currency_id' => 'required|numeric',
                    'country' => 'required|max:255',
                    'state' => 'required|max:255',
                    'city' => 'required|max:255',
                    'zip_code' => 'required|max:255',
                    'landmark' => 'required|max:255',
                    'time_zone' => 'required|max:255',
                    'surname' => 'max:10',
                    'email' => 'sometimes|nullable|email|unique:users|max:255',
                    'first_name' => 'required|max:255',
                    'username' => 'required|min:4|max:255|unique:users',
                    'password' => 'required|min:4|max:255',
                    'fy_start_month' => 'required',
                    'accounting_method' => 'required',
                ],
                [
                    'name.required' => __('validation.required', ['attribute' => __('business.business_name')]),
                    'name.currency_id' => __('validation.required', ['attribute' => __('business.currency')]),
                    'country.required' => __('validation.required', ['attribute' => __('business.country')]),
                    'state.required' => __('validation.required', ['attribute' => __('business.state')]),
                    'city.required' => __('validation.required', ['attribute' => __('business.city')]),
                    'zip_code.required' => __('validation.required', ['attribute' => __('business.zip_code')]),
                    'landmark.required' => __('validation.required', ['attribute' => __('business.landmark')]),
                    'time_zone.required' => __('validation.required', ['attribute' => __('business.time_zone')]),
                    'email.email' => __('validation.email', ['attribute' => __('business.email')]),
                    'email.email' => __('validation.unique', ['attribute' => __('business.email')]),
                    'first_name.required' => __('validation.required', ['attribute' =>
                        __('business.first_name')]),
                    'username.required' => __('validation.required', ['attribute' => __('business.username')]),
                    'username.min' => __('validation.min', ['attribute' => __('business.username')]),
                    'password.required' => __('validation.required', ['attribute' => __('business.username')]),
                    'password.min' => __('validation.min', ['attribute' => __('business.username')]),
                    'fy_start_month.required' => __('validation.required', ['attribute' => __('business.fy_start_month')]),
                    'accounting_method.required' => __('validation.required', ['attribute' => __('business.accounting_method')]),
                ]
            );

            if (!$this->verifyRecaptcha($request)) {
                return back()
                    ->withErrors([
                        'g-recaptcha-response' => 'Confirme que você não é um robô antes de continuar.'
                    ])
                    ->withInput();
            }

            DB::beginTransaction();

            //Create owner.
            $owner_details = $request->only(['surname', 'first_name', 'last_name', 'username', 'email', 'password', 'language']);

            $owner_details['language'] = empty($owner_details['language']) ? config('app.locale') : $owner_details['language'];

            $user = User::create_user($owner_details);

            $business_details = $request->only(['name', 'start_date', 'currency_id', 'time_zone']);
            $business_details['fy_start_month'] = 1;

            $business_location = $request->only(['name', 'country', 'state', 'city', 'zip_code', 'landmark', 'website', 'mobile', 'alternate_number']);

            //Create the business
            $business_details['owner_id'] = $user->id;
            if (!empty($business_details['start_date'])) {
                $business_details['start_date'] = Carbon::createFromFormat(config('constants.default_date_format'), $business_details['start_date'])->toDateString();
            }

            //upload logo
            $logo_name = $this->businessUtil->uploadFile($request, 'business_logo', 'business_logos', 'image');
            if (!empty($logo_name)) {
                $business_details['logo'] = $logo_name;
            }

            //default enabled modules
            $business_details['enabled_modules'] = ['purchases','add_sale','pos_sale','stock_transfers','stock_adjustment','expenses'];

            $business = $this->businessUtil->createNewBusiness($business_details);

            //Update user with business id
            $user->business_id = $business->id;

// ERP Sousait: o login só será liberado após pagamento + confirmação por token.
            $user->allow_login = 0;

            $user->save();

            $this->businessUtil->newBusinessDefaultResources($business->id, $user->id);
            $new_location = $this->businessUtil->addLocation($business->id, $business_location);

            //create new permission with the new location
            Permission::create(['name' => 'location.' . $new_location->id ]);

            DB::commit();

            //Module function to be called after after business is created
            if (config('app.env') != 'demo') {
                $this->moduleUtil->getModuleData('after_business_created', ['business' => $business]);
            }

            // ERP Sousait: não enviar e-mail ao Admsys no cadastro inicial.
// O e-mail administrativo será enviado somente após pagamento + confirmação por token do cliente.
            if (env('SEND_ADMIN_EMAIL_ON_REGISTER', false)) {
                try {
                    $this->sendNewRegistrationAdminEmail($business, $user, $request, $package_id);
                } catch (\Exception $mailException) {
                    \Log::warning(
                        'Falha ao enviar e-mail de novo registro para o administrador. ' .
                        'Business ID: ' . $business->id . ' - Erro: ' . $mailException->getMessage()
                    );
                }
            }

            // ERP Sousait: não enviar e-mail ao cliente no cadastro inicial.
// O e-mail do cliente será enviado somente após pagamento aprovado,
// contendo o link/token de confirmação.
            if (env('SEND_CUSTOMER_EMAIL_ON_REGISTER', false)) {
                try {
                    $this->sendNewRegistrationCustomerEmail($business, $user, $request, $package_id);
                } catch (\Exception $mailException) {
                    \Log::warning(
                        'Falha ao enviar e-mail de novo registro para o cliente. ' .
                        'Business ID: ' . $business->id . ' - User ID: ' . $user->id . ' - Erro: ' . $mailException->getMessage()
                    );
                }
            }

            //Process payment information if superadmin is installed & package information is present
            $is_installed_superadmin = $this->moduleUtil->isSuperadminInstalled();
            $package_id = $request->get('package_id', null);
            if ($is_installed_superadmin && !empty($package_id) && (config('app.env') != 'demo')) {
                $package = \Modules\Superadmin\Entities\Package::active()
                    ->notPrivate()
                    ->find($package_id);

                if (!empty($package)) {
                    Auth::login($user);
                    return redirect()->route('register-pay', ['package_id' => $package_id]);
                }
            }

            $output = ['success' => 1,
                'msg' => __('business.business_created_succesfully')
            ];

            return redirect('login')->with('status', $output);
        } catch (\Exception $e) {
            DB::rollBack();
            \Log::emergency("File:" . $e->getFile(). "Line:" . $e->getLine(). "Message:" . $e->getMessage());

            $output = ['success' => 0,
                'msg' => __('messages.something_went_wrong')
            ];

            return back()->with('status', $output)->withInput();
        }
    }

    private function verifyRecaptcha(Request $request)
    {
        if (!config('services.recaptcha.enabled')) {
            return true;
        }

        $secret = config('services.recaptcha.secret_key');
        $response = $request->input('g-recaptcha-response');

        if (empty($secret) || empty($response)) {
            \Log::warning('reCAPTCHA não informado no cadastro do ERP Sousait.');
            return false;
        }

        try {
            $client = new \GuzzleHttp\Client(['timeout' => 10]);

            $googleResponse = $client->post('https://www.google.com/recaptcha/api/siteverify', [
                'form_params' => [
                    'secret' => $secret,
                    'response' => $response,
                    'remoteip' => $request->ip(),
                ],
            ]);

            $body = json_decode((string) $googleResponse->getBody(), true);

            if (empty($body['success']) || $body['success'] !== true) {
                \Log::warning('Falha na validação do reCAPTCHA no cadastro do ERP Sousait.', [
                    'response' => $body,
                    'ip' => $request->ip(),
                    'email' => $request->get('email'),
                ]);

                return false;
            }

            return true;
        } catch (\Exception $e) {
            \Log::warning('Erro ao validar reCAPTCHA no cadastro do ERP Sousait: ' . $e->getMessage());
            return false;
        }
    }

    private function sendNewRegistrationAdminEmail($business, $user, Request $request, $package_id = null)
    {
        $adminEmail = env('ADMIN_REGISTRATION_EMAIL', 'admsys@sousait.net');

        $package = null;

        if (!empty($package_id) && $this->moduleUtil->isSuperadminInstalled()) {
            $package = \Modules\Superadmin\Entities\Package::find($package_id);
        }

        $html = $this->buildNewRegistrationAdminEmailHtml($business, $user, $request, $package);

        \Mail::send([], [], function ($message) use ($adminEmail, $html, $business) {
            $fromAddress = env('MAIL_FROM_ADDRESS', env('MAIL_USERNAME'));
            $fromName = env('MAIL_FROM_NAME', 'ERP Sousait');

            if (!empty($fromAddress)) {
                $message->from($fromAddress, $fromName);
            }

            $message->to($adminEmail)
                ->subject('Novo registro criado no ERP Sousait - ' . $business->name)
                ->setBody($html, 'text/html');
        });
    }

    private function buildNewRegistrationAdminEmailHtml($business, $user, Request $request, $package = null)
    {
        $value = function ($item) {
            return e(!empty($item) ? $item : 'Não informado');
        };

        $ownerName = trim(
            ($user->surname ? $user->surname . ' ' : '') .
            ($user->first_name ?? '') . ' ' .
            ($user->last_name ?? '')
        );

        $packageName = !empty($package) ? $package->name : 'Não informado';
        $packagePrice = !empty($package) && isset($package->price)
            ? 'R$ ' . number_format((float) $package->price, 2, ',', '.')
            : 'Não informado';

        $cnpj = $request->get('tax_number_1', $request->get('cnpj', 'Não informado'));
        $stateRegistration = $request->get('tax_number_2', $request->get('state_registration', 'Não informado'));

        $createdAt = date('d/m/Y H:i:s');

        return '
<!DOCTYPE html>
<html lang="pt-BR">
<head>
    <meta charset="UTF-8">
    <title>Novo registro criado - ERP Sousait</title>
</head>
<body style="margin:0; padding:0; background:#eef4f8; font-family:Arial, Helvetica, sans-serif; color:#1f2d3d;">
    <table width="100%" cellpadding="0" cellspacing="0" style="background:#eef4f8; padding:30px 0;">
        <tr>
            <td align="center">
                <table width="680" cellpadding="0" cellspacing="0" style="background:#ffffff; border-radius:14px; overflow:hidden; box-shadow:0 8px 24px rgba(15, 40, 70, 0.12);">
                    
                    <tr>
                        <td style="background:linear-gradient(135deg,#092033,#0d5f8f); padding:30px 36px; color:#ffffff;">
                            <div style="font-size:13px; text-transform:uppercase; letter-spacing:1.4px; color:#65d4ff; font-weight:bold;">
                                ERP Sousait
                            </div>
                            <h1 style="margin:10px 0 6px; font-size:26px; line-height:1.3;">
                                Novo registro criado no sistema
                            </h1>
                            <p style="margin:0; font-size:15px; color:#d8eef9;">
                                Uma nova empresa iniciou o cadastro no ERP Sousait.
                            </p>
                        </td>
                    </tr>

                    <tr>
                        <td style="padding:28px 36px;">
                            <p style="font-size:15px; line-height:1.6; margin:0 0 20px;">
                                Olá, foi criado um novo registro no sistema. Confira abaixo os dados principais do cadastro.
                            </p>

                            <table width="100%" cellpadding="0" cellspacing="0" style="border-collapse:collapse; margin-bottom:24px;">
                                <tr>
                                    <td colspan="2" style="background:#f2f8fc; color:#0d5f8f; padding:12px 14px; font-size:16px; font-weight:bold; border-radius:8px 8px 0 0;">
                                        Dados da empresa
                                    </td>
                                </tr>
                                ' . $this->emailInfoRow('Empresa', $value($business->name)) . '
                                ' . $this->emailInfoRow('ID da empresa', $value($business->id)) . '
                                ' . $this->emailInfoRow('CNPJ / Documento fiscal', $value($cnpj)) . '
                                ' . $this->emailInfoRow('Inscrição estadual', $value($stateRegistration)) . '
                                ' . $this->emailInfoRow('Cidade', $value($request->get('city'))) . '
                                ' . $this->emailInfoRow('Estado', $value($request->get('state'))) . '
                                ' . $this->emailInfoRow('País', $value($request->get('country'))) . '
                                ' . $this->emailInfoRow('CEP', $value($request->get('zip_code'))) . '
                                ' . $this->emailInfoRow('Endereço / Referência', $value($request->get('landmark'))) . '
                                ' . $this->emailInfoRow('Telefone / Celular', $value($request->get('mobile'))) . '
                                ' . $this->emailInfoRow('Website', $value($request->get('website'))) . '
                            </table>

                            <table width="100%" cellpadding="0" cellspacing="0" style="border-collapse:collapse; margin-bottom:24px;">
                                <tr>
                                    <td colspan="2" style="background:#f2f8fc; color:#0d5f8f; padding:12px 14px; font-size:16px; font-weight:bold; border-radius:8px 8px 0 0;">
                                        Dados do responsável
                                    </td>
                                </tr>
                                ' . $this->emailInfoRow('Nome', $value($ownerName)) . '
                                ' . $this->emailInfoRow('Usuário', $value($user->username)) . '
                                ' . $this->emailInfoRow('E-mail', $value($user->email)) . '
                                ' . $this->emailInfoRow('ID do usuário', $value($user->id)) . '
                            </table>

                            <table width="100%" cellpadding="0" cellspacing="0" style="border-collapse:collapse; margin-bottom:24px;">
                                <tr>
                                    <td colspan="2" style="background:#f2f8fc; color:#0d5f8f; padding:12px 14px; font-size:16px; font-weight:bold; border-radius:8px 8px 0 0;">
                                        Plano e auditoria
                                    </td>
                                </tr>
                                ' . $this->emailInfoRow('Plano selecionado', $value($packageName)) . '
                                ' . $this->emailInfoRow('Valor do plano', $value($packagePrice)) . '
                                ' . $this->emailInfoRow('Data do registro', $value($createdAt)) . '
                                ' . $this->emailInfoRow('Ambiente', $value(config('app.env'))) . '
                                ' . $this->emailInfoRow('URL do sistema', $value(url('/'))) . '
                            </table>

                            <div style="background:#fff7e6; border-left:4px solid #f5a623; padding:14px 16px; border-radius:8px; font-size:14px; line-height:1.5; color:#5c4600;">
                                <strong>Atenção:</strong> este e-mail não inclui senha do usuário por segurança. 
                                Para verificar pagamento, assinatura ou status do cadastro, acesse o painel administrativo do ERP Sousait.
                            </div>
                        </td>
                    </tr>

                    <tr>
                        <td style="background:#092033; padding:18px 36px; color:#aac7d8; font-size:12px; text-align:center;">
                            ERP Sousait • Gestão empresarial simples, clara e integrada<br>
                            Mensagem automática gerada pelo sistema.
                        </td>
                    </tr>

                </table>
            </td>
        </tr>
    </table>
</body>
</html>';
    }

    private function sendNewRegistrationCustomerEmail($business, $user, Request $request, $package_id = null)
    {
        if (empty($user->email)) {
            return;
        }

        $package = null;

        if (!empty($package_id) && $this->moduleUtil->isSuperadminInstalled()) {
            $package = \Modules\Superadmin\Entities\Package::find($package_id);
        }

        $html = $this->buildNewRegistrationCustomerEmailHtml($business, $user, $request, $package);

        \Mail::send([], [], function ($message) use ($user, $html, $business) {
            $fromAddress = env('MAIL_FROM_ADDRESS', env('MAIL_USERNAME'));
            $fromName = env('MAIL_FROM_NAME', 'ERP Sousait');

            if (!empty($fromAddress)) {
                $message->from($fromAddress, $fromName);
            }

            $message->to($user->email)
                ->subject('Bem-vindo ao ERP Sousait - cadastro iniciado')
                ->setBody($html, 'text/html');
        });
    }

    private function buildNewRegistrationCustomerEmailHtml($business, $user, Request $request, $package = null)
    {
        $value = function ($item) {
            return e(!empty($item) ? $item : 'Não informado');
        };

        $ownerName = trim(
            ($user->surname ? $user->surname . ' ' : '') .
            ($user->first_name ?? '') . ' ' .
            ($user->last_name ?? '')
        );

        $packageName = !empty($package) ? $package->name : 'Não informado';
        $packagePrice = !empty($package) && isset($package->price)
            ? 'R$ ' . number_format((float) $package->price, 2, ',', '.')
            : 'Não informado';

        $loginUrl = url('/login');
        $siteUrl = url('/');
        $createdAt = date('d/m/Y H:i:s');

        return '
<!DOCTYPE html>
<html lang="pt-BR">
<head>
    <meta charset="UTF-8">
    <title>Bem-vindo ao ERP Sousait</title>
</head>
<body style="margin:0; padding:0; background:#eef4f8; font-family:Arial, Helvetica, sans-serif; color:#1f2d3d;">
    <table width="100%" cellpadding="0" cellspacing="0" style="background:#eef4f8; padding:30px 0;">
        <tr>
            <td align="center">
                <table width="680" cellpadding="0" cellspacing="0" style="max-width:680px; background:#ffffff; border-radius:14px; overflow:hidden; box-shadow:0 8px 24px rgba(15, 40, 70, 0.12);">

                    <tr>
                        <td style="background:linear-gradient(135deg,#092033,#0d5f8f); padding:30px 36px; color:#ffffff;">
                            <div style="font-size:13px; text-transform:uppercase; letter-spacing:1.4px; color:#65d4ff; font-weight:bold;">
                                ERP Sousait
                            </div>
                            <h1 style="margin:10px 0 6px; font-size:26px; line-height:1.3;">
                                Cadastro iniciado com sucesso
                            </h1>
                            <p style="margin:0; font-size:15px; line-height:1.6; color:#d8eef9;">
                                Sua empresa já foi registrada na plataforma. Agora falta concluir a etapa de assinatura/pagamento do plano selecionado.
                            </p>
                        </td>
                    </tr>

                    <tr>
                        <td style="padding:28px 36px;">
                            <p style="font-size:15px; line-height:1.6; margin:0 0 20px;">
                                Olá, <strong>' . $value($ownerName) . '</strong>.
                            </p>

                            <p style="font-size:15px; line-height:1.6; margin:0 0 20px;">
                                Recebemos o cadastro da empresa <strong>' . $value($business->name) . '</strong> no <strong>ERP Sousait</strong>. Guarde este e-mail como confirmação inicial do registro.
                            </p>

                            <table width="100%" cellpadding="0" cellspacing="0" style="border-collapse:collapse; margin-bottom:24px;">
                                <tr>
                                    <td colspan="2" style="background:#f2f8fc; color:#0d5f8f; padding:12px 14px; font-size:16px; font-weight:bold; border-radius:8px 8px 0 0;">
                                        Dados do cadastro
                                    </td>
                                </tr>
                                ' . $this->emailInfoRow('Empresa', $value($business->name)) . '
                                ' . $this->emailInfoRow('Responsável', $value($ownerName)) . '
                                ' . $this->emailInfoRow('Usuário de acesso', $value($user->username)) . '
                                ' . $this->emailInfoRow('E-mail cadastrado', $value($user->email)) . '
                                ' . $this->emailInfoRow('Cidade/Estado', $value(trim($request->get('city') . ' / ' . $request->get('state'), ' /'))) . '
                                ' . $this->emailInfoRow('Data do cadastro', $value($createdAt)) . '
                            </table>

                            <table width="100%" cellpadding="0" cellspacing="0" style="border-collapse:collapse; margin-bottom:24px;">
                                <tr>
                                    <td colspan="2" style="background:#f2f8fc; color:#0d5f8f; padding:12px 14px; font-size:16px; font-weight:bold; border-radius:8px 8px 0 0;">
                                        Plano selecionado
                                    </td>
                                </tr>
                                ' . $this->emailInfoRow('Plano', $value($packageName)) . '
                                ' . $this->emailInfoRow('Valor', $value($packagePrice)) . '
                            </table>

                            <div style="background:#f7fbff; border-left:4px solid #0d5f8f; padding:16px 18px; border-radius:8px; font-size:14px; line-height:1.6; color:#36566f; margin-bottom:24px;">
                                <strong>Próximo passo:</strong> conclua o pagamento da assinatura para liberar o uso do ERP. Se você já finalizou o pagamento, aguarde a confirmação automática ou administrativa conforme o meio de pagamento utilizado.
                            </div>

                            <table cellpadding="0" cellspacing="0" style="margin:28px 0;">
                                <tr>
                                    <td>
                                        <a href="' . e($loginUrl) . '" style="display:inline-block; background:linear-gradient(135deg,#0d5f8f,#32c5ff); color:#ffffff; text-decoration:none; padding:14px 24px; border-radius:999px; font-size:14px; font-weight:bold;">
                                            Acessar ERP Sousait
                                        </a>
                                    </td>
                                </tr>
                            </table>

                            <div style="background:#fff7e6; border-left:4px solid #f5a623; padding:14px 16px; border-radius:8px; font-size:14px; line-height:1.5; color:#5c4600;">
                                <strong>Segurança:</strong> sua senha não é enviada por e-mail. Use a senha criada no cadastro. Caso esqueça, utilize a opção de recuperação de senha na tela de login.
                            </div>
                        </td>
                    </tr>

                    <tr>
                        <td style="background:#092033; padding:18px 36px; color:#aac7d8; font-size:12px; text-align:center; line-height:1.6;">
                            <strong style="color:#ffffff;">ERP Sousait</strong><br>
                            Gestão empresarial simples, clara e integrada.<br>
                            Site: ' . e($siteUrl) . '
                        </td>
                    </tr>

                </table>
            </td>
        </tr>
    </table>
</body>
</html>';
    }

    private function emailInfoRow($label, $value)
    {
        return '
    <tr>
        <td style="width:220px; padding:11px 14px; border-bottom:1px solid #e5edf3; font-size:13px; color:#607080; font-weight:bold; background:#fbfdff;">
            ' . $label . '
        </td>
        <td style="padding:11px 14px; border-bottom:1px solid #e5edf3; font-size:14px; color:#1f2d3d;">
            ' . $value . '
        </td>
    </tr>';
    }
    /**
     * Handles the validation username
     *
     * @return \Illuminate\Http\Response
     */
    public function postCheckUsername(Request $request)
    {
        $username = $request->input('username');

        if (!empty($request->input('username_ext'))) {
            $username .= $request->input('username_ext');
        }

        $count = User::where('username', $username)->count();

        if ($count == 0) {
            echo "true";
            exit;
        } else {
            echo "false";
            exit;
        }
    }

    /**
     * Shows business settings form
     *
     * @return \Illuminate\Http\Response
     */
    public function getBusinessSettings()
    {
        if (!auth()->user()->can('business_settings.access')) {
            abort(403, 'Unauthorized action.');
        }

        $timezones = DateTimeZone::listIdentifiers(DateTimeZone::ALL);
        $timezone_list = [];
        foreach ($timezones as $timezone) {
            $timezone_list[$timezone] = $timezone;
        }

        $business_id = request()->session()->get('user.business_id');
        $business = Business::where('id', $business_id)->first();

        $currencies = $this->businessUtil->allCurrencies();
        $tax_details = TaxRate::forBusinessDropdown($business_id);
        $tax_rates = $tax_details['tax_rates'];

        $months = [];
        for ($i=1; $i<=12; $i++) {
            $months[$i] = __('business.months.' . $i);
        }

        $accounting_methods = [
            'fifo' => __('business.fifo'),
            'lifo' => __('business.lifo')
        ];
        $commission_agent_dropdown = [
            '' => __('lang_v1.disable'),
            'logged_in_user' => __('lang_v1.logged_in_user'),
            'user' => __('lang_v1.select_from_users_list'),
            'cmsn_agnt' => __('lang_v1.select_from_commisssion_agents_list')
        ];

        $units_dropdown = Unit::forDropdown($business_id, true);

        $date_formats = Business::date_formats();

        $shortcuts = json_decode($business->keyboard_shortcuts, true);

        $pos_settings = empty($business->pos_settings) ? $this->businessUtil->defaultPosSettings() : json_decode($business->pos_settings, true);

        $email_settings = empty($business->email_settings) ? $this->businessUtil->defaultEmailSettings() : $business->email_settings;

        $sms_settings = empty($business->sms_settings) ? $this->businessUtil->defaultSmsSettings() : $business->sms_settings;

        $modules = $this->moduleUtil->availableModules();

        $theme_colors = $this->theme_colors;

        $mail_drivers = $this->mailDrivers;

        $allow_superadmin_email_settings = System::getProperty('allow_email_settings_to_businesses');

        $custom_labels = !empty($business->custom_labels) ? json_decode($business->custom_labels, true) : [];

        $common_settings = !empty($business->common_settings) ? $business->common_settings : [];

        $weighing_scale_setting = !empty($business->weighing_scale_setting) ? $business->weighing_scale_setting : [];

        return view('business.settings', compact('business', 'currencies', 'tax_rates', 'timezone_list', 'months', 'accounting_methods', 'commission_agent_dropdown', 'units_dropdown', 'date_formats', 'shortcuts', 'pos_settings', 'modules', 'theme_colors', 'email_settings', 'sms_settings', 'mail_drivers', 'allow_superadmin_email_settings', 'custom_labels', 'common_settings', 'weighing_scale_setting'));
    }

    /**
     * Updates business settings
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function postBusinessSettings(Request $request)
    {
        if (!auth()->user()->can('business_settings.access')) {
            abort(403, 'Unauthorized action.');
        }

        try {
            $notAllowed = $this->businessUtil->notAllowedInDemo();
            if (!empty($notAllowed)) {
                return $notAllowed;
            }

            $business_details = $request->only(['name', 'start_date', 'currency_id', 'tax_label_1', 'tax_number_1', 'tax_label_2', 'tax_number_2', 'default_profit_percent', 'default_sales_tax', 'default_sales_discount', 'sell_price_tax', 'sku_prefix', 'time_zone', 'fy_start_month', 'accounting_method', 'transaction_edit_days', 'sales_cmsn_agnt', 'item_addition_method', 'currency_symbol_placement', 'on_product_expiry',
                'stop_selling_before', 'default_unit', 'expiry_type', 'date_format',
                'time_format', 'ref_no_prefixes', 'theme_color', 'email_settings',
                'sms_settings', 'rp_name', 'amount_for_unit_rp',
                'min_order_total_for_rp', 'max_rp_per_order',
                'redeem_amount_per_unit_rp', 'min_order_total_for_redeem',
                'min_redeem_point', 'max_redeem_point', 'rp_expiry_period',
                'rp_expiry_type', 'custom_labels', 'weighing_scale_setting']);

            if (!empty($request->input('enable_rp')) &&  $request->input('enable_rp') == 1) {
                $business_details['enable_rp'] = 1;
            } else {
                $business_details['enable_rp'] = 0;
            }

            $business_details['amount_for_unit_rp'] = !empty($business_details['amount_for_unit_rp']) ? $this->businessUtil->num_uf($business_details['amount_for_unit_rp']) : 1;
            $business_details['min_order_total_for_rp'] = !empty($business_details['min_order_total_for_rp']) ? $this->businessUtil->num_uf($business_details['min_order_total_for_rp']) : 1;
            $business_details['redeem_amount_per_unit_rp'] = !empty($business_details['redeem_amount_per_unit_rp']) ? $this->businessUtil->num_uf($business_details['redeem_amount_per_unit_rp']) : 1;
            $business_details['min_order_total_for_redeem'] = !empty($business_details['min_order_total_for_redeem']) ? $this->businessUtil->num_uf($business_details['min_order_total_for_redeem']) : 1;

            $business_details['default_profit_percent'] = !empty($business_details['default_profit_percent']) ? $this->businessUtil->num_uf($business_details['default_profit_percent']) : 0;

            $business_details['default_sales_discount'] = !empty($business_details['default_sales_discount']) ? $this->businessUtil->num_uf($business_details['default_sales_discount']) : 0;

            if (!empty($business_details['start_date'])) {
                $business_details['start_date'] = $this->businessUtil->uf_date($business_details['start_date']);
            }

            if (!empty($request->input('enable_tooltip')) &&  $request->input('enable_tooltip') == 1) {
                $business_details['enable_tooltip'] = 1;
            } else {
                $business_details['enable_tooltip'] = 0;
            }

            $business_details['enable_product_expiry'] = !empty($request->input('enable_product_expiry')) &&  $request->input('enable_product_expiry') == 1 ? 1 : 0;
            if ($business_details['on_product_expiry'] == 'keep_selling') {
                $business_details['stop_selling_before'] = null;
            }

            $business_details['stock_expiry_alert_days'] = !empty($request->input('stock_expiry_alert_days')) ? $request->input('stock_expiry_alert_days') : 30;

            //Check for Purchase currency
            if (!empty($request->input('purchase_in_diff_currency')) &&  $request->input('purchase_in_diff_currency') == 1) {
                $business_details['purchase_in_diff_currency'] = 1;
                $business_details['purchase_currency_id'] = $request->input('purchase_currency_id');
                $business_details['p_exchange_rate'] = $request->input('p_exchange_rate');
            } else {
                $business_details['purchase_in_diff_currency'] = 0;
                $business_details['purchase_currency_id'] = null;
                $business_details['p_exchange_rate'] = 1;
            }

            //upload logo
            $logo_name = $this->businessUtil->uploadFile($request, 'business_logo', 'business_logos', 'image');
            if (!empty($logo_name)) {
                $business_details['logo'] = $logo_name;
            }

            $checkboxes = ['enable_editing_product_from_purchase',
                'enable_inline_tax',
                'enable_brand', 'enable_category', 'enable_sub_category', 'enable_price_tax', 'enable_purchase_status',
                'enable_lot_number', 'enable_racks', 'enable_row', 'enable_position', 'enable_sub_units'];
            foreach ($checkboxes as $value) {
                $business_details[$value] = !empty($request->input($value)) &&  $request->input($value) == 1 ? 1 : 0;
            }

            $business_id = request()->session()->get('user.business_id');
            $business = Business::where('id', $business_id)->first();

            //Update business settings
            if (!empty($business_details['logo'])) {
                $business->logo = $business_details['logo'];
            } else {
                unset($business_details['logo']);
            }

            //System settings
            $shortcuts = $request->input('shortcuts');
            $business_details['keyboard_shortcuts'] = json_encode($shortcuts);

            //pos_settings
            $pos_settings = $request->input('pos_settings');
            $default_pos_settings = $this->businessUtil->defaultPosSettings();
            foreach ($default_pos_settings as $key => $value) {
                if (!isset($pos_settings[$key])) {
                    $pos_settings[$key] = $value;
                }
            }
            $business_details['pos_settings'] = json_encode($pos_settings);

            $business_details['custom_labels'] = json_encode($business_details['custom_labels']);

            $business_details['common_settings'] = !empty($request->input('common_settings')) ? $request->input('common_settings') : [];

            //Enabled modules
            $enabled_modules = $request->input('enabled_modules');
            $business_details['enabled_modules'] = !empty($enabled_modules) ? $enabled_modules : null;

            $business->fill($business_details);
            $business->save();

            //update session data
            $request->session()->put('business', $business);

            //Update Currency details
            $currency = Currency::find($business->currency_id);
            $request->session()->put('currency', [
                'id' => $currency->id,
                'code' => $currency->code,
                'symbol' => $currency->symbol,
                'thousand_separator' => $currency->thousand_separator,
                'decimal_separator' => $currency->decimal_separator,
            ]);

            //update current financial year to session
            $financial_year = $this->businessUtil->getCurrentFinancialYear($business->id);
            $request->session()->put('financial_year', $financial_year);

            $output = ['success' => 1,
                'msg' => __('business.settings_updated_success')
            ];
        } catch (\Exception $e) {
            \Log::emergency("File:" . $e->getFile(). "Line:" . $e->getLine(). "Message:" . $e->getMessage());

            $output = ['success' => 0,
                'msg' => __('messages.something_went_wrong')
            ];
        }
        return redirect('business/settings')->with('status', $output);
    }

    /**
     * Handles the validation email
     *
     * @return \Illuminate\Http\Response
     */
    public function postCheckEmail(Request $request)
    {
        $email = $request->input('email');

        $query = User::where('email', $email);

        if (!empty($request->input('user_id'))) {
            $user_id = $request->input('user_id');
            $query->where('id', '!=', $user_id);
        }

        $exists = $query->exists();
        if (!$exists) {
            echo "true";
            exit;
        } else {
            echo "false";
            exit;
        }
    }

    public function getEcomSettings()
    {
        try {
            $api_token = request()->header('API-TOKEN');
            $api_settings = $this->moduleUtil->getApiSettings($api_token);

            $settings = Business::where('id', $api_settings->business_id)
                ->value('ecom_settings');

            $settings_array = !empty($settings) ? json_decode($settings, true) : [];

            if (!empty($settings_array['slides'])) {
                foreach ($settings_array['slides'] as $key => $value) {
                    $settings_array['slides'][$key]['image_url'] = !empty($value['image']) ? url('uploads/img/' . $value['image']) : '';
                }
            }
        } catch (\Exception $e) {
            \Log::emergency("File:" . $e->getFile(). "Line:" . $e->getLine(). "Message:" . $e->getMessage());

            return $this->respondWentWrong($e);
        }

        return $this->respond($settings_array);
    }

    /**
     * Handles the testing of email configuration
     *
     * @return \Illuminate\Http\Response
     */
    public function testEmailConfiguration(Request $request)
    {
        try {
            $email_settings = $request->input();

            $data['email_settings'] = $email_settings;
            \Notification::route('mail', $email_settings['mail_from_address'])
                ->notify(new TestEmailNotification($data));

            $output = [
                'success' => 1,
                'msg' => __('lang_v1.email_tested_successfully')
            ];
        } catch (\Exception $e) {
            \Log::emergency("File:" . $e->getFile(). "Line:" . $e->getLine(). "Message:" . $e->getMessage());
            $output = [
                'success' => 0,
                'msg' => $e->getMessage()
            ];
        }

        return $output;
    }

    /**
     * Handles the testing of sms configuration
     *
     * @return \Illuminate\Http\Response
     */
    public function testSmsConfiguration(Request $request)
    {
        try {
            $sms_settings = $request->input();

            $data = [
                'sms_settings' => $sms_settings,
                'mobile_number' => $sms_settings['test_number'],
                'sms_body' => 'This is a test SMS',
            ];
            if (!empty($sms_settings['test_number'])) {
                $response = $this->businessUtil->sendSms($data);
            } else {
                $response = __('lang_v1.test_number_is_required');
            }

            $output = [
                'success' => 1,
                'msg' => $response
            ];
        } catch (\Exception $e) {
            \Log::emergency("File:" . $e->getFile(). "Line:" . $e->getLine(). "Message:" . $e->getMessage());
            $output = [
                'success' => 0,
                'msg' => $e->getMessage()
            ];
        }

        return $output;
    }
}
