<?php

namespace humhub\modules\suggestedfollowing\models;

use humhub\modules\suggestedFollowing\Module;
use Yii;
use yii\base\Model;

/**
 * Settings form model for the Suggested Following module.
 *
 * @package humhub\modules\suggestedfollowing\models
 */
class ModuleSettings extends Model
{
    public int $maxWidgetSuggestions;

    public function init(): void
    {
        parent::init();

        /** @var Module $module */
        $module = Yii::$app->getModule('suggestedfollowing');

        $this->maxWidgetSuggestions = (int) $module->settings->get('maxWidgetSuggestions', $module->maxWidgetSuggestions);
    }

    public function rules(): array
    {
        return [
            [['maxWidgetSuggestions'], 'required'],
            [['maxWidgetSuggestions'], 'integer', 'min' => 1],
        ];
    }

    public function attributeLabels(): array
    {
        return [
            'maxWidgetSuggestions' => Yii::t('SuggestedfollowingModule.base', 'Max suggestions in dashboard widget'),
        ];
    }

    public function save(): bool
    {
        if (!$this->validate()) {
            return false;
        }

        /** @var Module $module */
        $module = Yii::$app->getModule('suggestedfollowing');

        $module->settings->set('maxWidgetSuggestions', $this->maxWidgetSuggestions);

        return true;
    }
}
