<?php

namespace Modules\Vrc\Http\Controllers\Registers;

use Modules\Vrc\Entities\VrcIncident;

/** ISO 45001 incident / accident / near-miss reporting & investigation. */
class IncidentController extends AbstractRegisterController
{
    protected function schema(): array
    {
        $types = ['accident' => 'Accident', 'near_miss' => 'Near miss', 'illness' => 'Illness', 'property_damage' => 'Property damage', 'environmental' => 'Environmental'];
        $status = ['reported' => 'Reported', 'investigating' => 'Investigating', 'action' => 'Action', 'closed' => 'Closed'];
        $locations = $this->locations();

        return [
            'key' => 'incidents',
            'route' => 'vrc.safety.incidents',
            'model' => VrcIncident::class,
            'permView' => 'vrc.safety.view',
            'permManage' => 'vrc.safety.manage',
            'title' => 'Incident Reports',
            'singular' => 'Incident',
            'defaults' => ['status' => 'reported'],
            'beforeSave' => function (array $data, $request, $row) {
                if (! $row) {
                    $data['reported_by'] = (int) request()->session()->get('user.id');
                }
                if (($data['status'] ?? null) === 'closed' && (! $row || $row->status !== 'closed')) {
                    $data['closed_at'] = now();
                }
                return $data;
            },
            'columns' => [
                ['key' => 'number', 'label' => 'No.'],
                ['key' => 'type', 'label' => 'Type', 'map' => $types, 'badge' => ['accident' => 'danger', 'near_miss' => 'warning', 'illness' => 'warning', 'property_damage' => 'info', 'environmental' => 'info']],
                ['key' => 'occurred_at', 'label' => 'Occurred', 'type' => 'date'],
                ['key' => 'persons_involved', 'label' => 'Persons'],
                ['key' => 'status', 'label' => 'Status', 'map' => $status, 'badge' => ['reported' => 'danger', 'investigating' => 'warning', 'action' => 'info', 'closed' => 'success']],
            ],
            'fields' => [
                ['name' => 'number', 'label' => 'Incident no.', 'type' => 'text', 'col' => 4, 'max' => 60],
                ['name' => 'type', 'label' => 'Type', 'type' => 'select', 'options' => $types, 'required' => true, 'col' => 4, 'default' => 'near_miss'],
                ['name' => 'occurred_at', 'label' => 'Occurred at', 'type' => 'datetime', 'required' => true, 'col' => 4],
                ['name' => 'location_id', 'label' => 'Location', 'type' => 'select', 'options' => $locations, 'col' => 4],
                ['name' => 'persons_involved', 'label' => 'Persons involved', 'type' => 'text', 'col' => 8, 'max' => 191],
                ['name' => 'description', 'label' => 'Description', 'type' => 'textarea', 'required' => true, 'col' => 12, 'rows' => 2],
                ['name' => 'injury_details', 'label' => 'Injury details', 'type' => 'textarea', 'col' => 6, 'rows' => 2],
                ['name' => 'immediate_action', 'label' => 'Immediate action', 'type' => 'textarea', 'col' => 6, 'rows' => 2],
                ['name' => 'status', 'label' => 'Status', 'type' => 'select', 'options' => $status, 'col' => 4, 'default' => 'reported'],
                ['name' => 'root_cause', 'label' => 'Root cause', 'type' => 'textarea', 'col' => 8, 'rows' => 2],
                ['name' => 'investigation', 'label' => 'Investigation', 'type' => 'textarea', 'col' => 12, 'rows' => 2],
            ],
        ];
    }
}
