<?php

namespace modules\hse\controllers;

use Yii;
use yii\web\Controller;
use modules\hse\models\FatigueCalculator;
use modules\hse\models\FatigueCalculatorSearch;
use modules\hse\models\RiskCalculator;
use modules\hse\models\EmployeeDefaults;

/**
 * HSE Calculator Controller
 *
 * Provides actions for HSE fatigue and risk index calculations
 */
class CalculatorController extends Controller
{
    /**
     * Fatigue calculator example
     */
    public function actionFatigue()
    {
        // Initialize employee defaults model
        $employeeDefaultsModel = new EmployeeDefaults();
        $employeeDefaultsModel->loadDefaults();

        // Employee Default Settings (for display)
        $employeeDefaults = $employeeDefaultsModel->getDisplayValues();

        // Employee Default Settings (for calculator)
        $calculatorDefaults = $employeeDefaultsModel->getCalculatorValues();

        // Get selected user/employee filter
        $selectedUser = Yii::$app->request->get('user');
        $startDate = Yii::$app->request->get('start_date');
        $endDate = Yii::$app->request->get('end_date');
        
        $fullname = new \yii\db\Expression("CONCAT(json_value(adv, '$.firstname'), ' ', json_value(adv, '$.surname'))");
        // Get list of users/employees for dropdown
        $users = \modules\users\models\User::Employeesu();

        // Load data from FatigueCalculatorSearch with user filter
        $searchModel = new FatigueCalculatorSearch();
        $queryParams = Yii::$app->request->queryParams;
        if ($selectedUser) {
            $queryParams['FatigueCalculatorSearch']['user'] = $selectedUser;
        }
        if ($startDate) {
            $queryParams['FatigueCalculatorSearch']['start_date'] = $startDate;
        }
        if ($endDate) {
            $queryParams['FatigueCalculatorSearch']['end_date'] = $endDate;
        }
        $dataProvider = $searchModel->search($queryParams);
        
        // Get the data from the data provider
        $extendedPattern = [];
        foreach ($dataProvider->getModels() as $model) {
            $extendedPattern[] = $model;
        }

        // Initialize the calculator with employee defaults
        $calculator = new FatigueCalculator($calculatorDefaults);

        // Analyze the pattern
        $analysis = $calculator->analyzeShiftPattern($extendedPattern);

        return $this->render('fatigue', [
            'analysis' => $analysis,
            'employeeDefaults' => $employeeDefaults,
            'users' => $users,
            'selectedUser' => $selectedUser,
            'startDate' => $startDate,
            'endDate' => $endDate,
        ]);
    }

    /**
     * Risk calculator example
     */
    public function actionRisk()
    {
        // Initialize employee defaults model
        $employeeDefaultsModel = new EmployeeDefaults();
        $employeeDefaultsModel->loadDefaults();

        // Employee Default Settings (for display)
        $employeeDefaults = $employeeDefaultsModel->getDisplayValues();

        // Employee Default Settings (for calculator)
        $calculatorDefaults = $employeeDefaultsModel->getCalculatorValues();

        // Get selected user/employee filter
 // Get selected user/employee filter
        $selectedUser = Yii::$app->request->get('user');
        $startDate = Yii::$app->request->get('start_date');
        $endDate = Yii::$app->request->get('end_date');
        
        $fullname = new \yii\db\Expression("CONCAT(json_value(adv, '$.firstname'), ' ', json_value(adv, '$.surname'))");
        // Get list of users/employees for dropdown
        $users = \modules\users\models\User::Employeesu();

        // Load data from FatigueCalculatorSearch with user filter
        $searchModel = new FatigueCalculatorSearch();
        $queryParams = Yii::$app->request->queryParams;
        if ($selectedUser) {
            $queryParams['FatigueCalculatorSearch']['user'] = $selectedUser;
        }
        if ($startDate) {
            $queryParams['FatigueCalculatorSearch']['start_date'] = $startDate;
        }
        if ($endDate) {
            $queryParams['FatigueCalculatorSearch']['end_date'] = $endDate;
        }
        $dataProvider = $searchModel->search($queryParams);
        
        
        // Get the data from the data provider
        $extendedPattern = [];
        foreach ($dataProvider->getModels() as $model) {
            $extendedPattern[] = $model;
        }

        // Initialize the calculator with employee defaults
        $calculator = new RiskCalculator($calculatorDefaults);

        // Analyze the pattern
        $analysis = $calculator->analyzeShiftPattern($extendedPattern);

        return $this->render('risk', [
            'analysis' => $analysis,
            'employeeDefaults' => $employeeDefaults,
            'users' => $users,
            'selectedUser' => $selectedUser,
            'startDate' => $startDate,
            'endDate' => $endDate,
        ]);
    }

    public function actionIndex()
    {
        $searchModel = new FatigueCalculatorSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
}