<?php

namespace Modules\Vrc\Http\Controllers\Registers;

use Modules\Vrc\Entities\VrcWasteDisposal;
use Modules\Vrc\Entities\VrcWasteStream;

/** ISO 14001 hazardous-waste disposal log (manifests + certificates). */
class WasteDisposalController extends AbstractRegisterController
{
    protected function schema(): array
    {
        $streams = VrcWasteStream::where('business_id', $this->businessId())->where('is_active', true)
            ->pluck('name', 'id')->toArray();
        $locations = $this->locations();

        return [
            'key' => 'waste_disposals',
            'route' => 'vrc.env.waste-disposals',
            'model' => VrcWasteDisposal::class,
            'permView' => 'vrc.environment.view',
            'permManage' => 'vrc.environment.manage',
            'title' => 'Waste Disposal Log',
            'singular' => 'Disposal Record',
            'beforeSave' => function (array $data, $request, $row) {
                if (! $row) {
                    $data['recorded_by'] = (int) request()->session()->get('user.id');
                }
                return $data;
            },
            'columns' => [
                ['key' => 'disposed_on', 'label' => 'Date', 'type' => 'date'],
                ['key' => 'waste_stream_id', 'label' => 'Waste stream', 'map' => $streams],
                ['key' => 'quantity', 'label' => 'Qty'],
                ['key' => 'unit', 'label' => 'Unit'],
                ['key' => 'disposal_vendor', 'label' => 'Vendor'],
                ['key' => 'manifest_no', 'label' => 'Manifest'],
            ],
            'fields' => [
                ['name' => 'waste_stream_id', 'label' => 'Waste stream', 'type' => 'select', 'options' => $streams, 'required' => true, 'col' => 6],
                ['name' => 'disposed_on', 'label' => 'Disposed on', 'type' => 'date', 'required' => true, 'col' => 3],
                ['name' => 'location_id', 'label' => 'Location', 'type' => 'select', 'options' => $locations, 'col' => 3],
                ['name' => 'quantity', 'label' => 'Quantity', 'type' => 'number', 'required' => true, 'col' => 3, 'step' => '0.001', 'min' => 0],
                ['name' => 'unit', 'label' => 'Unit', 'type' => 'text', 'col' => 3, 'default' => 'L', 'max' => 12],
                ['name' => 'disposal_vendor', 'label' => 'Disposal vendor', 'type' => 'text', 'col' => 6, 'max' => 191],
                ['name' => 'manifest_no', 'label' => 'Manifest no.', 'type' => 'text', 'col' => 6, 'max' => 120],
                ['name' => 'certificate_path', 'label' => 'Certificate URL / path', 'type' => 'text', 'col' => 6, 'max' => 255],
                ['name' => 'notes', 'label' => 'Notes', 'type' => 'textarea', 'col' => 12],
            ],
        ];
    }
}
