<?php
declare(strict_types=1);

use App\Core\Database;

return function (Database $db): void {
    $ts = date('Y-m-d H:i:s');

    $definitions = [
        // ── 1. Purchase Requisition Approval ──
        [
            'workflow_code'  => 'procurement_pr_approval',
            'name_ar'        => 'اعتماد طلبات الشراء',
            'name_en'        => 'Purchase Requisition Approval',
            'description_ar' => 'دورة عمل طلب واعتماد طلبات الشراء الداخلية',
            'definition_json' => json_encode([
                'states' => [
                    'draft'            => ['label_ar' => 'مسودة',               'label_en' => 'Draft',            'type' => 'initial'],
                    'submitted'        => ['label_ar' => 'تم التقديم',          'label_en' => 'Submitted',        'type' => 'intermediate'],
                    'approved'         => ['label_ar' => 'معتمد',               'label_en' => 'Approved',         'type' => 'intermediate'],
                    'rejected'         => ['label_ar' => 'مرفوض',               'label_en' => 'Rejected',         'type' => 'terminal'],
                    'converted'        => ['label_ar' => 'تم التحويل لأمر شراء', 'label_en' => 'Converted to PO', 'type' => 'terminal'],
                    'cancelled'        => ['label_ar' => 'ملغى',                'label_en' => 'Cancelled',        'type' => 'terminal'],
                ],
                'transitions' => [
                    ['name' => 'submit',     'from' => 'draft',     'to' => 'submitted', 'guards' => [['type' => 'permission', 'value' => 'procurement.pr.create']],  'actions' => [['type' => 'event', 'value' => 'procurement.requisition_submitted']]],
                    ['name' => 'approve',    'from' => 'submitted', 'to' => 'approved',  'guards' => [['type' => 'permission', 'value' => 'procurement.pr.approve']], 'actions' => [['type' => 'event', 'value' => 'procurement.requisition_approved']]],
                    ['name' => 'reject',     'from' => 'submitted', 'to' => 'rejected',  'guards' => [['type' => 'permission', 'value' => 'procurement.pr.approve']], 'actions' => [['type' => 'event', 'value' => 'procurement.requisition_rejected']]],
                    ['name' => 'convert',    'from' => 'approved',  'to' => 'converted', 'guards' => [['type' => 'permission', 'value' => 'procurement.pr.convert']], 'actions' => [['type' => 'event', 'value' => 'procurement.requisition_converted']]],
                    ['name' => 'cancel',     'from' => 'draft',     'to' => 'cancelled', 'guards' => [['type' => 'condition', 'value' => 'is_creator']],              'actions' => [['type' => 'event', 'value' => 'procurement.requisition_cancelled']]],
                    ['name' => 'cancel_sub', 'from' => 'submitted', 'to' => 'cancelled', 'guards' => [['type' => 'permission', 'value' => 'procurement.pr.approve']], 'actions' => [['type' => 'event', 'value' => 'procurement.requisition_cancelled']]],
                ],
            ], JSON_UNESCAPED_UNICODE),
        ],

        // ── 2. Return to Vendor Approval ──
        [
            'workflow_code'  => 'procurement_rtv_approval',
            'name_ar'        => 'اعتماد مرتجعات الموردين',
            'name_en'        => 'Return to Vendor Approval',
            'description_ar' => 'دورة عمل طلب واعتماد وشحن مرتجعات الموردين',
            'definition_json' => json_encode([
                'states' => [
                    'draft'     => ['label_ar' => 'مسودة',         'label_en' => 'Draft',     'type' => 'initial'],
                    'submitted' => ['label_ar' => 'تم التقديم',    'label_en' => 'Submitted', 'type' => 'intermediate'],
                    'approved'  => ['label_ar' => 'معتمد',         'label_en' => 'Approved',  'type' => 'intermediate'],
                    'shipped'   => ['label_ar' => 'تم الشحن',      'label_en' => 'Shipped',   'type' => 'intermediate'],
                    'completed' => ['label_ar' => 'مكتمل',         'label_en' => 'Completed', 'type' => 'terminal'],
                    'cancelled' => ['label_ar' => 'ملغى',          'label_en' => 'Cancelled', 'type' => 'terminal'],
                ],
                'transitions' => [
                    ['name' => 'submit',   'from' => 'draft',     'to' => 'submitted', 'guards' => [['type' => 'permission', 'value' => 'procurement.rtv.create']],  'actions' => [['type' => 'event', 'value' => 'procurement.rtv_submitted']]],
                    ['name' => 'approve',  'from' => 'submitted', 'to' => 'approved',  'guards' => [['type' => 'permission', 'value' => 'procurement.rtv.approve']], 'actions' => [['type' => 'event', 'value' => 'procurement.rtv_approved']]],
                    ['name' => 'reject',   'from' => 'submitted', 'to' => 'cancelled', 'guards' => [['type' => 'permission', 'value' => 'procurement.rtv.approve']], 'actions' => [['type' => 'event', 'value' => 'procurement.rtv_rejected']]],
                    ['name' => 'ship',     'from' => 'approved',  'to' => 'shipped',   'guards' => [['type' => 'permission', 'value' => 'procurement.rtv.manage']],  'actions' => [['type' => 'event', 'value' => 'procurement.rtv_shipped']]],
                    ['name' => 'complete', 'from' => 'shipped',   'to' => 'completed', 'guards' => [['type' => 'permission', 'value' => 'procurement.rtv.manage']],  'actions' => [['type' => 'event', 'value' => 'procurement.rtv_completed']]],
                    ['name' => 'cancel',   'from' => 'draft',     'to' => 'cancelled', 'guards' => [['type' => 'condition', 'value' => 'is_creator']],               'actions' => [['type' => 'event', 'value' => 'procurement.rtv_cancelled']]],
                ],
            ], JSON_UNESCAPED_UNICODE),
        ],
    ];

    foreach ($definitions as $def) {
        $existing = $db->selectOne(
            "SELECT id FROM workflow_definitions WHERE workflow_code = ?",
            [$def['workflow_code']]
        );
        if ($existing) {
            continue;
        }
        $db->insert('workflow_definitions', [
            'workflow_code'   => $def['workflow_code'],
            'name_ar'         => $def['name_ar'],
            'name_en'         => $def['name_en'],
            'description_ar'  => $def['description_ar'],
            'definition_json' => $def['definition_json'],
            'version'         => 1,
            'is_active'       => 1,
            'created_at'      => $ts,
            'updated_at'      => $ts,
        ]);
    }
};
