<?php

namespace App\Service\Admin;

use App\Enum\BankProvider;
use App\Enum\PaymentProvider;
use App\Models\Admin;
use App\Models\SystemSetting;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Auth;

use App\Traits\JsonResponseTrait;

class SystemSettingsService
{
    use JsonResponseTrait;
    /**
     * Get the authenticated admin's profile details.
     *
     * @return array
     */
    public function getSystemSettings()
    {

        $settings = SystemSetting::whereId(1)->first();
        if (!$settings) {
            return $this->errorResponse(404, message: "System settings not found",);
        }
        return $this->successDataResponse(data: $settings, message: "System settings fetched successful");
    }

    public function updateSystemSettings(array $data)
    {
        if (empty($data)) {
            return $this->errorResponse(
                statusCode: 400,
                message: "No data provided for updating system settings"
            );
        }

        // Update the settings
        $settings = SystemSetting::whereId(1)->first();
        if (!$settings) {
            return $this->errorResponse(statusCode: 404, message: "System settings not found");
        }
        $settings->update($data);
        $newSettings = $settings->fresh();

        return $this->successDataResponse(data: $newSettings , message: "System settings updated successfully");
    }

    public function updateReferalPoints(array $data)
    {
        // Update the settings
        if (empty($data)) {
            return $this->errorResponse(
                statusCode: 400,
                message: "No data provided for updating system settings"
            );
        }
        $settings = SystemSetting::whereId(1)->first();
        if (!$settings) {
            return $this->errorResponse(statusCode: 404, message: "System settings not found");
        }
        $settings->update($data);
        $settings->fresh();

        return $this->successDataResponse(data: $settings, message: "Referal points updated successfully");
    }

    public function updateBankProvider(int $bankProvider)
    {
        //1 Monnify 2 kora 3 boni 4 SH 5 palmpay
        // Update the settings
        if (empty($bankProvider)) {
            return $this->errorResponse(
                statusCode: 400,
                message: "No data provided for updating system settings"
            );
        }

        // Validate if the provided value exists in the enum
        if (!BankProvider::tryFrom($bankProvider)) {
            return $this->errorResponse(
                statusCode: 400,
                message: "Invalid payment provider"
            );
        }

        $settings = SystemSetting::whereId(1)->first();
        if (!$settings) {
            return $this->errorResponse(statusCode: 404, message: "System settings not found");
        }
        $settings->update(
            [
                'activebanksystem' => $bankProvider
            ]
        );
        $settings->fresh();

        return $this->successDataResponse(
            data: $settings,
            message: "Bank provider updated successfully to " . BankProvider::getName($bankProvider)
        );
    }

    public function updatePaymentProvider(int $paymentProvider)
    {
        //1 Monnify 2 kora 3 boni 4 SH 5 palmpay
        // Update the settings
        if (empty($paymentProvider)) {
            return $this->errorResponse(
                statusCode: 400,
                message: "No data provided for updating system settings"
            );
        }

        // Validate if the provided value exists in the enum
        if (!PaymentProvider::tryFrom($paymentProvider)) {
            return $this->errorResponse(
                statusCode: 400,
                message: "Invalid payment provider"
            );
        }

        
        $settings = SystemSetting::whereId(1)->first();
        if (!$settings) {
            return $this->errorResponse(statusCode: 404, message: "System settings not found");
        }
        $settings->update(
            [
                'activepaysystem' => $paymentProvider
            ]
        );
        $settings->fresh();

        return $this->successDataResponse(
            data: $settings,
            message: "Payment provider updated successfully to " . PaymentProvider::getName($paymentProvider)
        );
    }
}
