<?php

namespace App\Http\Services;

use App\Models\PromoCode;
use App\Models\StoreOrder;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\View;

use App\Models\User;

class EmailService
{

    protected $fromEmailAddress;
    protected $emailFromName;
    protected $secret;
    public $appUrl;
    public $appInternalName;
    public $stringMappingConfig;

    public function __construct()
    {
        $this->secret = 'Superman2022@';
        $this->fromEmailAddress = config('app.fromEmailAddress');
        $this->emailFromName = config('app.emailFromName');
        $this->appUrl = config('app.url');
        $this->appInternalName = config('app.internalName');
        $this->stringMappingConfig = config('constants.' . $this->appInternalName);
    }

    protected function callJava($endPoint, $data, $sessionKey)
    {
        $data['secret'] = $this->secret;
        $url = config('app.backendUrl') . $endPoint;
        $response =   Http::asForm()
            ->withHeaders([
                'sessionKey' => $sessionKey
            ])
            ->post($url, $data)
            ->body();

        return json_decode($response, true);
    }


    //Emails

    public function sendUserPasswordResetEmail(User $user)
    {
        if(!$user->getEmail()) return;
        $appInternalName = $this->appInternalName;
        $stringMappingConfig = $this->stringMappingConfig;
        $appUrl = $this->appUrl;
        $emailFromName = $this->emailFromName;
        $html = (string) view('emails.templates.user-reset-password', compact('user', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig'));
        $plainText = (string) view('emails.templates.user-reset-password-txt', compact('user', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig'));

        $params = [
            'fromEmailAddress' => $this->fromEmailAddress,
            'fromName' => $this->emailFromName,
            'toEmailAddress' => $user->getEmail(),
            'subject' => 'Reset Password',
            'contentHtml' => $html,
            'contentText' => $plainText,
            'entityType' => 'USER',
            'entityUid' => $user->uid,
        ];

        $response = $this->callJava('/api/email/send', $params, null);
    }

    public function sendOrderInvoice(StoreOrder $storeOrder)
    {
        $user = $storeOrder->user;
        if(!$user) return;
        if(!@$user->getEmail()) return;
        $appInternalName = $this->appInternalName;
        $stringMappingConfig = $this->stringMappingConfig;
        $appUrl = $this->appUrl;
        $emailFromName = $this->emailFromName;
        $html = (string) view('emails.templates.invoice', compact('user', 'storeOrder', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig'));
        $plainText = (string) view('emails.templates.invoice-txt', compact('user', 'storeOrder', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig'));

        $params = [
            'fromEmailAddress' => $this->fromEmailAddress,
            'fromName' => $this->emailFromName,
            'toEmailAddress' => $user->getEmail(),
            'subject' => 'Invoice from ' . $stringMappingConfig['name'] . ' #' . $storeOrder->iid ?? $storeOrder->id,
            'contentHtml' => $html,
            'contentText' => $plainText,
            'entityType' => 'USER',
            'entityUid' => $user->uid,
        ];

        $response = $this->callJava('/api/email/send', $params, null);
    }

    public function notifyUserOnFailedTransaction(User $user)
    {
        if(!@$user->getEmail()) return;
        $appInternalName = $this->appInternalName;
        $stringMappingConfig = $this->stringMappingConfig;
        $appUrl = $this->appUrl;
        $emailFromName = $this->emailFromName;
        $html = (string) view('emails.templates.user-failed-order-charge', compact('user', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig'));
        $plainText = (string) view('emails.templates.user-failed-order-charge-txt', compact('user', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig'));

        $params = [
            'fromEmailAddress' => $this->fromEmailAddress,
            'fromName' => $this->emailFromName,
            'toEmailAddress' => $user->getEmail(),
            'subject' => 'Failed to process card payment',
            'contentHtml' => $html,
            'contentText' => $plainText,
            'entityType' => 'USER',
            'entityUid' => $user->uid,
        ];

        $response = $this->callJava('/api/email/send', $params, null);
    }

    public function notifyUserOnShippedOrder(User $user, $shippingDetails)
    {
        if(!@$user->getEmail()) return;
        $appInternalName = $this->appInternalName;
        $stringMappingConfig = $this->stringMappingConfig;
        $appUrl = $this->appUrl;
        $emailFromName = $this->emailFromName;
        $html = (string) view('emails.templates.user-order-shipped', compact('user', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig', 'shippingDetails'));
        $plainText = (string) view('emails.templates.user-order-shipped-txt', compact('user', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig', 'shippingDetails'));

        $params = [
            'fromEmailAddress' => $this->fromEmailAddress,
            'fromName' => $this->emailFromName,
            'toEmailAddress' => $user->getEmail(),
            'subject' => 'Your order is on the way',
            'contentHtml' => $html,
            'contentText' => $plainText,
            'entityType' => 'USER',
            'entityUid' => $user->uid,
        ];

        $response = $this->callJava('/api/email/send', $params, null);
    }
}
