<?php

namespace App\Http\Controllers;

use Exception;
use Illuminate\Support\Facades\Storage;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\Mail\ContactMail;
use App\Mail\WelcomeEmail;
use Illuminate\Support\Facades\Http;

class MiscController extends Controller
{

    //Contact From Submission
    public function handleFormSubmission(Request $request)
    {
        try {
            $name = $request->fullName;
            $companyName = $request->companyName;
            $email = $request->email;
            $phone = $request->phone;
            $message = $request->message;
            $requestType = $request->requestType;
            $dateTime = $request->dateTime;
            $employeeHeadcount = $request->employeeHeadcount;
            $country = $request->country;
            $findUs = $request->findUs;
            $meta = $request->meta;

            //Send Email
            $mailData = [
                "email" => env('APP_ENV') == 'develop' ? 'antonyodu@gmail.com' : 'info@payraty.com',
                "companyName" => $companyName,
                "contactEmail" => $email,
                "name" => $name,
                "phone" => $phone,
                "message" => $message,
                "requestType" => $requestType,
                "dateTime" => $dateTime,
                "employeeHeadcount" => $employeeHeadcount,
                "country" => $country,
                "findUs" => $findUs,
                "meta" => $meta
            ];
            try {
                Mail::to($mailData["email"])->send(new ContactMail($mailData));
            } catch (Exception $e) {
                return response()->json([
                    'status' => 'failed',
                    'message' => $e->getMessage(),
                ], 500);
            }

            return response()->json([
                'status' => 'success',
                'message' => 'Mail sent successfully',
                'data' => $mailData
            ], 200);
        } catch (\Exception $e) {
            return response()->json([
                'status' => 'failed',
                'message' => $e->getMessage(),
            ], 500);
        }
    }

    //list All Banks
    public function listAllBanksOld(Request $request)
    {
        try {
            $secret = env('PAYSTACK_SECRET');
            $baseUrl = env("PAYSTACK_URL");
            $url = "{$baseUrl}/bank";
            $authorization = "Bearer {$secret}";
            $response = Http::withHeaders([
                'Authorization' => $authorization,
            ])->get($url);
            $responseData = $response->json();

            return response()->json([
                'status' => 'success',
                'message' => 'Banks List',
                'data' => $responseData['data']
            ], 200);
        } catch (\Exception $e) {
            return response()->json([
                'status' => 'failed',
                'message' => $e->getMessage(),
            ], 500);
        }
    }

    public function listAllBanks(Request $request)
    {
        try {

            $loginRemita = $this->remitaLogin();
            if (isset($loginRemita['accessToken'])) {
                $accessToken = $loginRemita['accessToken'];
                $baseUrl = "https://api-gateway.remita.net/api/v1";
                $url = "{$baseUrl}/integration/banks";

                $response = Http::withHeaders([
                    'Authorization' => "Bearer {$accessToken}",
                    'Content-Type' => 'application/json'
                ])->get($url);

                $responseData = $response->json();

                return response()->json([
                    'status' => 'success',
                    'message' => $responseData['message'] ?? 'Banks List',
                    'data' => collect($responseData['data']['banks'] ?? [])->map(function ($bank) {
                        return [
                            'code' => $bank['bankCode'],
                            'name' => $bank['bankName']
                        ];
                    })->values()
                ], 200);
            }

        } catch (\Exception $e) {
            return response()->json([
                'status' => 'failed',
                'message' => $e->getMessage(),
                'remita' => $loginRemita
            ], 500);
        }
    }


    public function testMail(Request $request)
    {
        try {
            $mailData = [
                "name" => "Boss Inc International",
                "email" => $request->email
            ];

            try {
                Mail::to($mailData["email"])->send(new WelcomeEmail($mailData));
            } catch (Exception $e) {
                return response()->json([
                    'status' => 'failed',
                    'message' => $e->getMessage(),
                ], 500);
            }
            return response()->json([
                'status' => 'success',
                'message' => 'Mail Sent',
                'data' => []
            ], 200);
        } catch (\Exception $e) {
            return response()->json([
                'status' => 'failed',
                'message' => $e->getMessage(),
            ], 500);
        }
    }
}
