<?php

namespace Modules\Vrc\Http\Controllers\Registers;

use App\User;
use Modules\Vrc\Entities\VrcCapa;
use Modules\Vrc\Entities\VrcNcr;

/** ISO 9001 corrective / preventive actions (the continuous-improvement loop). */
class CapaController extends AbstractRegisterController
{
    protected function schema(): array
    {
        $type = ['corrective' => 'Corrective', 'preventive' => 'Preventive'];
        $status = ['open' => 'Open', 'in_progress' => 'In progress', 'verifying' => 'Verifying', 'closed' => 'Closed'];
        $ncrs = VrcNcr::where('business_id', $this->businessId())->orderByDesc('id')->limit(200)
            ->get(['id', 'title'])->mapWithKeys(fn ($n) => [$n->id => '#' . $n->id . ' ' . $n->title])->toArray();
        $owners = User::forDropdown($this->businessId(), false);

        return [
            'key' => 'capas',
            'route' => 'vrc.quality.capas',
            'model' => VrcCapa::class,
            'permView' => 'vrc.quality.view',
            'permManage' => 'vrc.quality.manage',
            'title' => 'Corrective / Preventive Actions',
            'singular' => 'CAPA',
            'defaults' => ['status' => 'open'],
            'beforeSave' => function (array $data, $request, $row) {
                if (($data['status'] ?? null) === 'closed' && (! $row || $row->status !== 'closed')) {
                    $data['closed_at'] = now();
                }
                return $data;
            },
            'columns' => [
                ['key' => 'number', 'label' => 'No.'],
                ['key' => 'title', 'label' => 'Title'],
                ['key' => 'type', 'label' => 'Type', 'map' => $type, 'badge' => ['corrective' => 'warning', 'preventive' => 'info']],
                ['key' => 'due_date', 'label' => 'Due', 'type' => 'date'],
                ['key' => 'status', 'label' => 'Status', 'map' => $status, 'badge' => ['open' => 'danger', 'in_progress' => 'warning', 'verifying' => 'info', 'closed' => 'success']],
            ],
            'fields' => [
                ['name' => 'number', 'label' => 'CAPA no.', 'type' => 'text', 'col' => 4, 'max' => 60],
                ['name' => 'title', 'label' => 'Title', 'type' => 'text', 'required' => true, 'col' => 8, 'max' => 191],
                ['name' => 'type', 'label' => 'Type', 'type' => 'select', 'options' => $type, 'col' => 4, 'default' => 'corrective'],
                ['name' => 'ncr_id', 'label' => 'Related NCR', 'type' => 'select', 'options' => $ncrs, 'col' => 4],
                ['name' => 'owner_id', 'label' => 'Owner', 'type' => 'select', 'options' => $owners, 'col' => 4],
                ['name' => 'due_date', 'label' => 'Due date', 'type' => 'date', 'col' => 4],
                ['name' => 'status', 'label' => 'Status', 'type' => 'select', 'options' => $status, 'col' => 4, 'default' => 'open'],
                ['name' => 'root_cause', 'label' => 'Root cause', 'type' => 'textarea', 'col' => 12, 'rows' => 2],
                ['name' => 'action_plan', 'label' => 'Action plan', 'type' => 'textarea', 'col' => 12, 'rows' => 2],
                ['name' => 'effectiveness', 'label' => 'Effectiveness review', 'type' => 'textarea', 'col' => 12, 'rows' => 2],
            ],
        ];
    }
}
