<?php 
namespace modules\hse\models;

use Yii;

class EmployeeDefaults extends \common\models\DynamicModel
{
    public $form = 'EmployeeDefaults';
    public $viewLayout = '/users/employee-defaults/_view.php';
    public $formLayout = '/users/employee-defaults/_form.php';
    public $printlayout = 'Portrait';
    public $formname = 'HSE Fatigue & Risk Calculator - Employee Default Settings';

    public static function localAttributes()
    {
        return [
            'workload_pace' => [
                'rules' => [
                    ['string']
                ],
                'label' => Yii::t('app', "What is the pace of the work?"),
                'input' => [
                    'input' => 'dropdownlist',
                    'fieldclass' => null,
                    'fieldlist' => [
                        'Very undemanding, plenty of spare capacity.' => 'Very undemanding, plenty of spare capacity.',
                        'Moderately undemanding, some spare capacity.' => 'Moderately undemanding, some spare capacity.',
                        'Moderate to demanding, little spare capacity.' => 'Moderate to demanding, little spare capacity.',
                        'Very demanding, no spare capacity.' => 'Very demanding, no spare capacity.',
                    ],
                    'options' => ['label' => false, 'prompt' => 'Please Select'],
                    'width' => 'col-12',
                ],
                'search' => false,
            ],
            'continuous_attention' => [
                'rules' => [
                    ['string']
                ],
                'label' => Yii::t('app', "Does the work require continuous attention?"),
                'input' => [
                    'input' => 'dropdownlist',
                    'fieldclass' => null,
                    'fieldlist' => [
                        'Rarely' => 'Rarely',
                        'Some of the time' => 'Some of the time',
                        'Frequent' => 'Frequent',
                        'Continuous' => 'Continuous',
                    ],
                    'options' => ['label' => false, 'prompt' => 'Please Select'],
                    'width' => 'col-12',
                ],
                'search' => false,
            ],
            'rest_break_frequency' => [
                'rules' => [
                    ['string']
                ],
                'label' => Yii::t('app', "How often do employees typically get a break?"),
                'input' => [
                    'input' => 'textInput',
                    'fieldclass' => null,
                    'fieldlist' => null,
                    'options' => ['label' => false, 'placeholder' => 'e.g., 3h'],
                    'width' => 'col-12',
                ],
                'search' => false,
            ],
            'rest_break_frequency_hours' => [
                'rules' => [
                    ['number']
                ],
                'label' => Yii::t('app', "Rest break frequency (hours)"),
                'input' => [
                    'input' => 'textInput',
                    'fieldclass' => null,
                    'fieldlist' => null,
                    'options' => ['label' => false, 'type' => 'number', 'step' => '0.5', 'placeholder' => 'e.g., 3.0'],
                    'width' => 'col-12',
                ],
                'search' => false,
            ],
            'rest_break_average_length' => [
                'rules' => [
                    ['string']
                ],
                'label' => Yii::t('app', "What is the typical average length of these breaks that are provided or taken?"),
                'input' => [
                    'input' => 'textInput',
                    'fieldclass' => null,
                    'fieldlist' => null,
                    'options' => ['label' => false, 'placeholder' => 'e.g., 25m'],
                    'width' => 'col-12',
                ],
                'search' => false,
            ],
            'rest_break_average_length_minutes' => [
                'rules' => [
                    ['integer']
                ],
                'label' => Yii::t('app', "Rest break average length (minutes)"),
                'input' => [
                    'input' => 'textInput',
                    'fieldclass' => null,
                    'fieldlist' => null,
                    'options' => ['label' => false, 'type' => 'number', 'placeholder' => 'e.g., 25'],
                    'width' => 'col-12',
                ],
                'search' => false,
            ],
            'longest_continuous_work_period' => [
                'rules' => [
                    ['string']
                ],
                'label' => Yii::t('app', "What is typically the longest period of continuous work before a break?"),
                'input' => [
                    'input' => 'textInput',
                    'fieldclass' => null,
                    'fieldlist' => null,
                    'options' => ['label' => false, 'placeholder' => 'e.g., 2h'],
                    'width' => 'col-12',
                ],
                'search' => false,
            ],
            'longest_continuous_work_hours' => [
                'rules' => [
                    ['number']
                ],
                'label' => Yii::t('app', "Longest continuous work period (hours)"),
                'input' => [
                    'input' => 'textInput',
                    'fieldclass' => null,
                    'fieldlist' => null,
                    'options' => ['label' => false, 'type' => 'number', 'step' => '0.5', 'placeholder' => 'e.g., 2.0'],
                    'width' => 'col-12',
                ],
                'search' => false,
            ],
            'break_after_longest_period' => [
                'rules' => [
                    ['string']
                ],
                'label' => Yii::t('app', "What is typically the length of the break taken after this longest period of continuous work?"),
                'input' => [
                    'input' => 'textInput',
                    'fieldclass' => null,
                    'fieldlist' => null,
                    'options' => ['label' => false, 'placeholder' => 'e.g., 45m'],
                    'width' => 'col-12',
                ],
                'search' => false,
            ],
            'break_after_longest_period_minutes' => [
                'rules' => [
                    ['integer']
                ],
                'label' => Yii::t('app', "Break after longest period (minutes)"),
                'input' => [
                    'input' => 'textInput',
                    'fieldclass' => null,
                    'fieldlist' => null,
                    'options' => ['label' => false, 'type' => 'number', 'placeholder' => 'e.g., 45'],
                    'width' => 'col-12',
                ],
                'search' => false,
            ],
            'typical_commuting_time' => [
                'rules' => [
                    ['string']
                ],
                'label' => Yii::t('app', "What is the typical commuting time of employees to OR from work?"),
                'input' => [
                    'input' => 'textInput',
                    'fieldclass' => null,
                    'fieldlist' => null,
                    'options' => ['label' => false, 'placeholder' => 'e.g., 20m'],
                    'width' => 'col-12',
                ],
                'search' => false,
            ],
            'typical_commuting_time_minutes' => [
                'rules' => [
                    ['integer']
                ],
                'label' => Yii::t('app', "Typical commuting time (minutes)"),
                'input' => [
                    'input' => 'textInput',
                    'fieldclass' => null,
                    'fieldlist' => null,
                    'options' => ['label' => false, 'type' => 'number', 'placeholder' => 'e.g., 20'],
                    'width' => 'col-12',
                ],
                'search' => false,
            ],
            'commuting_multiplier' => [
                'rules' => [
                    ['integer']
                ],
                'label' => Yii::t('app', "Commuting multiplier"),
                'hint' => Yii::t('app', "1 = one-way trip, 2 = return trip (to and from work)"),
                'input' => [
                    'input' => 'radioList',
                    'fieldclass' => null,
                    'fieldlist' => [
                        1 => 'One-way (×1)',
                        2 => 'Return trip (×2)',
                    ],
                    'options' => ['label' => false],
                    'width' => 'col-12',
                ],
                'search' => false,
            ],
        ];
    }

    /**
     * Get display-friendly values from the model
     * @return array
     */
    public function getDisplayValues()
    {
        return [
            'workload_pace' => $this->workload_pace ?? 'Moderately undemanding, some spare capacity.',
            'continuous_attention' => $this->continuous_attention ?? 'Some of the time',
            'rest_break_frequency' => $this->rest_break_frequency ?? '3h',
            'rest_break_average_length' => $this->rest_break_average_length ?? '25m',
            'longest_continuous_work_period' => $this->longest_continuous_work_period ?? '2h',
            'break_after_longest_period' => $this->break_after_longest_period ?? '45m',
            'typical_commuting_time' => $this->typical_commuting_time ?? '20m',
        ];
    }

    /**
     * Get calculator-friendly values (numeric)
     * @return array
     */
    public function getCalculatorValues()
    {
        return [
            'workload_pace' => $this->workload_pace ?? 'Moderately undemanding, some spare capacity.',
            'continuous_attention' => $this->continuous_attention ?? 'Some of the time',
            'rest_break_frequency_hours' => $this->rest_break_frequency_hours ?? 3.0,
            'rest_break_average_length_minutes' => $this->rest_break_average_length_minutes ?? 25,
            'longest_continuous_work_hours' => $this->longest_continuous_work_hours ?? 2.0,
            'break_after_longest_period_minutes' => $this->break_after_longest_period_minutes ?? 45,
            'typical_commuting_time_minutes' => $this->typical_commuting_time_minutes ?? 20,
            'commuting_multiplier' => $this->commuting_multiplier ?? 2,
        ];
    }

    /**
     * Load default values
     */
    public function loadDefaults()
    {
        $this->workload_pace = 'Moderately undemanding, some spare capacity.';
        $this->continuous_attention = 'Some of the time';
        $this->rest_break_frequency = '3h';
        $this->rest_break_frequency_hours = 3.0;
        $this->rest_break_average_length = '25m';
        $this->rest_break_average_length_minutes = 25;
        $this->longest_continuous_work_period = '2h';
        $this->longest_continuous_work_hours = 2.0;
        $this->break_after_longest_period = '45m';
        $this->break_after_longest_period_minutes = 45;
        $this->typical_commuting_time = '20m';
        $this->typical_commuting_time_minutes = 20;
        $this->commuting_multiplier = 2;
    }


    public static function loadData($data)
    {
        $M = new self();
        if (!empty($data)){
            if(is_object($data)) {
                $data = (array)$data;
            }
            foreach ($data as $key => $value) {
                if ($M->hasAttribute($key)) {
                    $M->$key = $value;
                }
            }
        }
        return $M;
    }
}
