<?php

namespace Autogedal\TbiBank\Model;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;

class ApprovalConfig
{
    public const ORDER_STATE = 'pending';
    public const WAITING_STATUS = 'asteptare_plata_tbi';
    public const PATH = 'tbi_settings/approval_settings/';

    public function __construct(private ScopeConfigInterface $scopeConfig)
    {
    }

    public function isRequired($storeId): bool
    {
        return $this->scopeConfig->isSetFlag(self::PATH . 'require_approval_before_awb', ScopeInterface::SCOPE_STORE, $storeId);
    }

    public function getApprovedStatus($storeId): string
    {
        return (string) ($this->scopeConfig->getValue(self::PATH . 'approved_status', ScopeInterface::SCOPE_STORE, $storeId) ?: 'new');
    }

    public function getCallbackApprovedStatus(): string
    {
        return trim((string) $this->scopeConfig->getValue(self::PATH . 'callback_approved_status'));
    }

    public function pollingEnabled(): bool
    {
        return $this->scopeConfig->isSetFlag(self::PATH . 'polling_enabled');
    }

    public function deferOrderEmail($storeId): bool
    {
        return $this->scopeConfig->isSetFlag(self::PATH . 'defer_order_email_until_approval', ScopeInterface::SCOPE_STORE, $storeId);
    }

    public function getRemoteApprovedStatuses(): array
    {
        $value = (string) $this->scopeConfig->getValue(self::PATH . 'remote_approved_statuses');
        return array_values(array_filter(array_map('trim', preg_split('/\R/', $value))));
    }

    public function getBatchSize(): int
    {
        return max(1, min(100, (int) ($this->scopeConfig->getValue(self::PATH . 'polling_batch_size') ?: 50)));
    }
}
