<?php

namespace Modules\Vrc\Http\Controllers\Registers;

use App\User;
use Modules\Vrc\Entities\VrcHazard;

/** ISO 45001 hazard identification & risk assessment. */
class HazardController extends AbstractRegisterController
{
    protected function schema(): array
    {
        $scale = [1 => '1', 2 => '2', 3 => '3', 4 => '4', 5 => '5'];
        $levels = ['low' => 'Low', 'medium' => 'Medium', 'high' => 'High', 'extreme' => 'Extreme'];
        $locations = $this->locations();
        $owners = User::forDropdown($this->businessId(), false);

        return [
            'key' => 'hazards',
            'route' => 'vrc.safety.hazards',
            'model' => VrcHazard::class,
            'permView' => 'vrc.safety.view',
            'permManage' => 'vrc.safety.manage',
            'title' => 'Hazards & Risk Assessment',
            'singular' => 'Hazard',
            'defaults' => ['status' => 'open'],
            'beforeSave' => function (array $data, $request, $row) {
                [$score, $band] = VrcHazard::level((int) ($data['likelihood'] ?? 1), (int) ($data['severity'] ?? 1));
                $data['risk_score'] = $score;
                $data['risk_level'] = $band;
                return $data;
            },
            'columns' => [
                ['key' => 'hazard', 'label' => 'Hazard'],
                ['key' => 'area', 'label' => 'Area'],
                ['key' => 'risk_score', 'label' => 'Score'],
                ['key' => 'risk_level', 'label' => 'Risk', 'map' => $levels, 'badge' => ['low' => 'success', 'medium' => 'warning', 'high' => 'danger', 'extreme' => 'danger']],
                ['key' => 'review_due', 'label' => 'Review due', 'type' => 'date'],
            ],
            'fields' => [
                ['name' => 'hazard', 'label' => 'Hazard', 'type' => 'text', 'required' => true, 'col' => 8, 'max' => 191],
                ['name' => 'area', 'label' => 'Area', 'type' => 'text', 'col' => 4, 'max' => 120],
                ['name' => 'risk_description', 'label' => 'Risk description', 'type' => 'textarea', 'col' => 12, 'rows' => 2],
                ['name' => 'likelihood', 'label' => 'Likelihood (1-5)', 'type' => 'select', 'options' => $scale, 'required' => true, 'col' => 3, 'default' => 1],
                ['name' => 'severity', 'label' => 'Severity (1-5)', 'type' => 'select', 'options' => $scale, 'required' => true, 'col' => 3, 'default' => 1],
                ['name' => 'location_id', 'label' => 'Location', 'type' => 'select', 'options' => $locations, 'col' => 3],
                ['name' => 'owner_id', 'label' => 'Owner', 'type' => 'select', 'options' => $owners, 'col' => 3],
                ['name' => 'controls', 'label' => 'Controls', 'type' => 'textarea', 'col' => 12, 'rows' => 2],
                ['name' => 'residual_level', 'label' => 'Residual risk', 'type' => 'select', 'options' => $levels, 'col' => 4],
                ['name' => 'review_due', 'label' => 'Review due', 'type' => 'date', 'col' => 4],
            ],
        ];
    }
}
