<?php

namespace Espo\Modules\SmartAssistant\Services;

use Espo\ORM\EntityManager;
use Espo\Core\Utils\Log;

class AssistantRuleContextProvider
{
    private EntityManager $entityManager;
    private Log $log;

    public function __construct(EntityManager $entityManager, Log $log)
    {
        $this->entityManager = $entityManager;
        $this->log = $log;
    }

    public function getRulesForMode(string $mode): array
    {
        $entries = $this->entityManager->getRDBRepository('AssistantRule')
            ->where([
                'isActive' => true,
                'deleted' => false,
                'scope' => ['global', $mode],
            ])
            ->order('createdAt', 'ASC')
            ->find();

        $rules = [];
        foreach ($entries as $entry) {
            $rules[] = [
                'name' => $entry->get('name'),
                'rule' => $entry->get('rule'),
                'scope' => $entry->get('scope'),
            ];
        }

        return $rules;
    }
}
