<?php

declare(strict_types=1);

namespace App\Http\Controllers\Payment;

use App\Enums\UserActions;
use App\Http\Controllers\Helpers\Permissions\UserPermissions;
use App\Http\Controllers\Helpers\ResponseHelper;
use App\Http\Controllers\Helpers\QrphDecoder;
use App\Models\SystemSetting;
use Hypervel\Http\Request;
use Hypervel\Support\Facades\Auth;

class QRPHController
{
    private function checkAdmin(): bool
    {
        return UserPermissions::isActionPermitted(Auth::user()->acct_type, UserActions::ManageQrphPaymentCode);
    }

    /**
     * Get the static QR PH code stored in system settings.
     */
    public function getQrCode()
    {
        $code  = SystemSetting::getValue('qrph_payment_code');
        $image = SystemSetting::getValue('qrph_payment_image_hashkey');

        return response()->json([
            'success' => true,
            'data'    => [
                'qrph_code'           => $code,
                'qrph_image_hashkey'  => $image,
                'has_qr'              => !empty($code),
            ],
        ]);
    }

    /**
     * Update the static QR PH code (admin only).
     */
    public function setQrCode(Request $request)
    {
        if (!$this->checkAdmin()) return ResponseHelper::returnUnauthorized();

        $code = $request->input('qrph_code');
        if (empty($code)) return ResponseHelper::returnError('QR PH code is required', 422);

        try {
            $decoded = QrphDecoder::decode($code);
        } catch (\Throwable $e) {
            return ResponseHelper::returnError('Invalid QR PH code: ' . $e->getMessage(), 422);
        }

        SystemSetting::setValue('qrph_payment_code', $code);

        if ($hashkey = $request->input('image_hashkey')) {
            SystemSetting::setValue('qrph_payment_image_hashkey', $hashkey);
        }

        return response()->json([
            'success'  => true,
            'data'     => $decoded,
            'message'  => 'QR PH code updated',
        ]);
    }

    /**
     * Decode a QR PH string (admin utility).
     */
    public function decode(Request $request)
    {
        if (!$this->checkAdmin()) return ResponseHelper::returnUnauthorized();

        $code = $request->input('code');
        if (empty($code)) return ResponseHelper::returnError('QR PH code is required', 422);

        try {
            $decoded = QrphDecoder::decode($code);
            return response()->json(['success' => true, 'data' => $decoded]);
        } catch (\Throwable $e) {
            return ResponseHelper::returnError('Decode error: ' . $e->getMessage(), 422);
        }
    }

    /**
     * Remove QR PH code from system settings.
     */
    public function removeQrCode()
    {
        if (!$this->checkAdmin()) return ResponseHelper::returnUnauthorized();

        SystemSetting::setValue('qrph_payment_code', null);
        SystemSetting::setValue('qrph_payment_image_hashkey', null);

        return response()->json(['success' => true, 'message' => 'QR PH code removed']);
    }
}
