<?php

/*
|--------------------------------------------------------------------------
| Tenant clone topology (Test Instances)
|--------------------------------------------------------------------------
|
| The schema map used by App\Services\Tenancy\TestInstanceService to copy every
| tenant-scoped row of a live tenant into its isolated test instance, remapping
| primary keys so both environments coexist in the same tables.
|
| Rules encoded here:
|  - Only columns listed as `fks` are remapped; everything else (global
|    references like city_id / tracking_mode_id / grn_status_id / permission_id)
|    is copied verbatim on purpose.
|  - Tables listed in `excluded` are operational, device- or audit-related rows
|    that must NOT leak into a test environment (e.g. device tokens would push
|    POS notifications to live devices) or are polymorphic heaps better
|    regenerated per-environment (audit trails).
|  - Tables flagged `tenant_scoped => false` are global children anchored to an
|    owned table through `anchor`/`anchor_col` (product_attributes has no
|    tenant_id but belongs to a tenant's products).
|  - `pivots` are tables without tenant_id whose isolation is inherited from
|    their tenant-scoped parents; their rows are copied/erased by membership in
|    the mapped id sets.
|  - `polymorphic` maps morph-type columns whose `*_id` may reference one of
|    several cloned tables; the id is remapped only when the type resolves to a
|    cloned table.
|  - `json_remap` lists JSON columns containing arrays of ids of another cloned
|    table (remapped in place).
|
| Tables are topologically ordered at runtime from these `fks`; any remaining
| cycle edge (e.g. users ↔ roles via created_by) is resolved in a second pass
| after every id map exists.
*/

return [

    'tables' => [
        // --- Lookups (no fks) ---
        'statuses' => ['fks' => []],
        'payment_methods' => ['fks' => []],
        'movement_types' => ['fks' => []],
        'return_reasons' => ['fks' => []],
        'damage_reasons' => ['fks' => []],
        'adjustment_reasons' => ['fks' => []],
        'stock_transfer_reasons' => ['fks' => []],
        'cash_movement_reasons' => ['fks' => []],
        'payment_terms' => ['fks' => []],
        'ledger_entry_types' => ['fks' => []],
        'supplier_address_types' => ['fks' => []],
        'discount_types' => ['fks' => []],
        'voucher_types' => ['fks' => []],
        'loyalty_transaction_types' => ['fks' => []],
        'refund_methods' => ['fks' => []],
        'unit_of_measures' => ['fks' => []],
        'document_sequences' => ['fks' => []],
        'app_settings' => ['fks' => []],

        // --- Users / roles / branches (cycle: users.role_id ↔ roles.created_by) ---
        'roles' => ['fks' => ['created_by' => 'users', 'updated_by' => 'users']],
        'users' => [
            'fks' => ['role_id' => 'roles', 'created_by' => 'users', 'updated_by' => 'users'],
            'json_remap' => ['dismissed_cheque_alert_ids' => 'invoices'],
        ],
        'warehouses' => [
            'fks' => ['manager_user_id' => 'users', 'created_by' => 'users', 'updated_by' => 'users', 'deleted_by' => 'users'],
        ],

        // --- Catalog ---
        'main_categories' => ['fks' => ['created_by' => 'users', 'updated_by' => 'users', 'deleted_by' => 'users']],
        'sub_categories' => [
            'fks' => [
                'main_category_id' => 'main_categories',
                'default_base_unit_id' => 'unit_of_measures',
                'created_by' => 'users',
                'updated_by' => 'users',
                'deleted_by' => 'users',
            ],
        ],
        'products' => [
            'fks' => [
                'main_category_id' => 'main_categories',
                'sub_category_id' => 'sub_categories',
                'base_unit_id' => 'unit_of_measures',
                'created_by' => 'users',
                'updated_by' => 'users',
                'deleted_by' => 'users',
            ],
        ],
        'product_units' => ['fks' => ['product_id' => 'products', 'unit_id' => 'unit_of_measures']],
        'product_attributes' => [
            'tenant_scoped' => false,
            'anchor' => 'products',
            'anchor_col' => 'product_id',
            'fks' => ['product_id' => 'products'],
        ],
        'product_bundles' => ['fks' => ['branch_id' => 'warehouses']],
        'bundle_items' => [
            'fks' => ['bundle_id' => 'product_bundles', 'product_id' => 'products', 'unit_id' => 'unit_of_measures', 'created_by' => 'users'],
        ],

        // --- Customers / people ---
        'customers' => [
            'fks' => [
                'assigned_rep_id' => 'sales_reps',
                'assigned_user_id' => 'users',
                'created_by' => 'users',
                'updated_by' => 'users',
                'deleted_by' => 'users',
            ],
        ],
        'customer_contacts' => ['fks' => ['customer_id' => 'customers']],
        'customer_credit_balances' => ['fks' => ['customer_id' => 'customers']],
        'customer_credit_ledger' => [
            'fks' => ['customer_id' => 'customers', 'created_by' => 'users'],
        ],
        'delivery_addresses' => ['fks' => ['customer_id' => 'customers']],
        'sales_reps' => [
            'fks' => [
                'user_id' => 'users',
                'branch_id' => 'warehouses',
                'created_by' => 'users',
                'updated_by' => 'users',
                'deleted_by' => 'users',
            ],
        ],
        'tills' => ['fks' => ['branch_id' => 'warehouses', 'created_by' => 'users', 'updated_by' => 'users', 'deleted_by' => 'users']],

        // --- POS / sales ---
        'pos_sessions' => ['fks' => ['branch_id' => 'warehouses', 'till_id' => 'tills', 'status_id' => 'statuses', 'opened_by' => 'users', 'closed_by' => 'users']],
        'cash_movements' => ['fks' => ['pos_session_id' => 'pos_sessions', 'reason_id' => 'cash_movement_reasons', 'created_by' => 'users']],
        'invoices' => [
            'fks' => [
                'branch_id' => 'warehouses',
                'customer_id' => 'customers',
                'delivery_address_id' => 'delivery_addresses',
                'till_id' => 'tills',
                'pos_session_id' => 'pos_sessions',
                'sales_rep_id' => 'sales_reps',
                'order_status_id' => 'statuses',
                'payment_status_id' => 'statuses',
                'amended_from_invoice_id' => 'invoices',
                'created_by' => 'users',
                'updated_by' => 'users',
                'deleted_by' => 'users',
            ],
        ],
        'invoice_items' => [
            'fks' => [
                'invoice_id' => 'invoices',
                'product_id' => 'products',
                'unit_id' => 'unit_of_measures',
                'stock_batch_id' => 'stock_batches',
                'product_serial_id' => 'product_serials',
                'product_bundle_id' => 'product_bundles',
                'deal_id' => 'deals',
            ],
        ],
        'transactions' => [
            'fks' => ['customer_id' => 'customers', 'branch_id' => 'warehouses', 'payment_method_id' => 'payment_methods', 'status_id' => 'statuses', 'created_by' => 'users', 'updated_by' => 'users'],
        ],
        'transaction_claims' => ['fks' => ['transaction_id' => 'transactions', 'invoice_id' => 'invoices', 'claimed_by' => 'users']],
        'held_sales' => ['fks' => ['branch_id' => 'warehouses', 'till_id' => 'tills', 'pos_session_id' => 'pos_sessions', 'customer_id' => 'customers', 'created_by' => 'users']],
        'held_sale_items' => [
            'fks' => ['held_sale_id' => 'held_sales', 'product_id' => 'products', 'product_bundle_id' => 'product_bundles', 'stock_batch_id' => 'stock_batches'],
            'json_remap' => ['selected_serial_ids' => 'product_serials'],
        ],
        'sales_orders' => [
            'fks' => [
                'branch_id' => 'warehouses',
                'customer_id' => 'customers',
                'delivery_address_id' => 'delivery_addresses',
                'sales_rep_id' => 'sales_reps',
                'status_id' => 'statuses',
                'payment_status_id' => 'statuses',
                'converted_invoice_id' => 'invoices',
                'created_by' => 'users',
                'updated_by' => 'users',
                'deleted_by' => 'users',
            ],
        ],
        'sales_order_items' => ['fks' => ['sales_order_id' => 'sales_orders', 'product_id' => 'products', 'unit_id' => 'unit_of_measures']],
        'sales_order_payments' => ['fks' => ['sales_order_id' => 'sales_orders', 'credit_note_id' => 'credit_notes', 'created_by' => 'users']],

        // --- Returns / credit / damages ---
        'returns' => ['fks' => ['customer_id' => 'customers', 'branch_id' => 'warehouses', 'status_id' => 'statuses', 'refund_method_id' => 'refund_methods', 'created_by' => 'users', 'updated_by' => 'users', 'deleted_by' => 'users']],
        'return_items' => [
            'fks' => [
                'return_id' => 'returns',
                'invoice_id' => 'invoices',
                'invoice_item_id' => 'invoice_items',
                'product_id' => 'products',
                'unit_id' => 'unit_of_measures',
                'return_reason_id' => 'return_reasons',
                'stock_batch_id' => 'stock_batches',
                'product_serial_id' => 'product_serials',
            ],
        ],
        'credit_notes' => ['fks' => ['customer_id' => 'customers', 'return_id' => 'returns', 'claimed_invoice_id' => 'invoices', 'status_id' => 'statuses', 'created_by' => 'users', 'updated_by' => 'users', 'deleted_by' => 'users']],
        'damages' => [
            'fks' => [
                'branch_id' => 'warehouses',
                'product_id' => 'products',
                'unit_id' => 'unit_of_measures',
                'damage_reason_id' => 'damage_reasons',
                'stock_batch_id' => 'stock_batches',
                'product_serial_id' => 'product_serials',
                'recorded_by' => 'users',
                'created_by' => 'users',
                'updated_by' => 'users',
                'deleted_by' => 'users',
            ],
        ],

        // --- Purchasing / GRN ---
        'suppliers' => ['fks' => ['payment_terms_id' => 'payment_terms', 'status_id' => 'statuses', 'blacklisted_by' => 'users', 'created_by' => 'users', 'updated_by' => 'users', 'deleted_by' => 'users']],
        'supplier_contacts' => ['fks' => ['supplier_id' => 'suppliers', 'created_by' => 'users']],
        'supplier_addresses' => ['fks' => ['supplier_id' => 'suppliers', 'address_type_id' => 'supplier_address_types']],
        'supplier_bank_accounts' => ['fks' => ['supplier_id' => 'suppliers']],
        'supplier_ledgers' => ['fks' => ['supplier_id' => 'suppliers', 'ledger_entry_type_id' => 'ledger_entry_types', 'created_by' => 'users']],
        'supplier_documents' => ['fks' => ['supplier_id' => 'suppliers', 'created_by' => 'users', 'updated_by' => 'users', 'deleted_by' => 'users']],
        'purchase_orders' => ['fks' => ['supplier_id' => 'suppliers', 'branch_id' => 'warehouses', 'status_id' => 'statuses', 'created_by' => 'users', 'updated_by' => 'users', 'deleted_by' => 'users']],
        'purchase_order_items' => ['fks' => ['purchase_order_id' => 'purchase_orders', 'product_id' => 'products', 'unit_id' => 'unit_of_measures']],
        'grns' => ['fks' => ['branch_id' => 'warehouses', 'supplier_document_id' => 'supplier_documents', 'supplier_id' => 'suppliers', 'purchase_order_id' => 'purchase_orders', 'confirmed_by' => 'users', 'created_by' => 'users', 'updated_by' => 'users', 'deleted_by' => 'users']],
        'grn_items' => ['fks' => ['grn_id' => 'grns', 'product_id' => 'products', 'unit_id' => 'unit_of_measures', 'stock_batch_id' => 'stock_batches']],

        // --- Inventory / stock ---
        'branch_stock' => ['fks' => ['branch_id' => 'warehouses', 'product_id' => 'products']],
        'stock_batches' => ['fks' => ['branch_id' => 'warehouses', 'product_id' => 'products', 'status_id' => 'statuses', 'created_by' => 'users', 'updated_by' => 'users', 'deleted_by' => 'users']],
        'product_serials' => ['fks' => ['product_id' => 'products', 'stock_batch_id' => 'stock_batches', 'branch_id' => 'warehouses', 'current_customer_id' => 'customers', 'status_id' => 'statuses', 'created_by' => 'users', 'updated_by' => 'users', 'deleted_by' => 'users']],
        'serial_ownership_history' => ['fks' => ['product_serial_id' => 'product_serials', 'customer_id' => 'customers', 'invoice_id' => 'invoices', 'return_id' => 'returns', 'created_by' => 'users']],
        'serial_repair_history' => ['fks' => ['product_serial_id' => 'product_serials', 'branch_id' => 'warehouses', 'created_by' => 'users']],
        'stock_movements' => ['fks' => ['branch_id' => 'warehouses', 'product_id' => 'products', 'movement_type_id' => 'movement_types', 'unit_id' => 'unit_of_measures', 'stock_batch_id' => 'stock_batches', 'created_by' => 'users']],
        'stock_adjustments' => ['fks' => ['branch_id' => 'warehouses', 'product_id' => 'products', 'unit_id' => 'unit_of_measures', 'adjustment_reason_id' => 'adjustment_reasons', 'stock_batch_id' => 'stock_batches', 'adjusted_by' => 'users', 'created_by' => 'users', 'updated_by' => 'users', 'deleted_by' => 'users']],
        'stock_transfers' => ['fks' => ['from_branch_id' => 'warehouses', 'to_branch_id' => 'warehouses', 'product_id' => 'products', 'unit_id' => 'unit_of_measures', 'reason_id' => 'stock_transfer_reasons', 'stock_batch_id' => 'stock_batches', 'created_by' => 'users', 'updated_by' => 'users', 'deleted_by' => 'users']],

        // --- Vouchers / loyalty / deals ---
        'vouchers' => ['fks' => ['voucher_type_id' => 'voucher_types', 'discount_type_id' => 'discount_types', 'customer_id' => 'customers', 'branch_id' => 'warehouses', 'status_id' => 'statuses', 'issued_by_invoice_id' => 'invoices', 'created_by' => 'users', 'updated_by' => 'users', 'deleted_by' => 'users']],
        'voucher_redemptions' => ['fks' => ['voucher_id' => 'vouchers', 'invoice_id' => 'invoices', 'customer_id' => 'customers', 'redeemed_by' => 'users', 'reversed_by' => 'users']],
        'loyalty_programs' => ['fks' => ['created_by' => 'users', 'updated_by' => 'users']],
        'loyalty_tiers' => ['fks' => ['program_id' => 'loyalty_programs', 'created_by' => 'users', 'updated_by' => 'users']],
        'customer_loyalty_balances' => ['fks' => ['customer_id' => 'customers', 'program_id' => 'loyalty_programs', 'tier_id' => 'loyalty_tiers', 'created_by' => 'users', 'updated_by' => 'users']],
        'loyalty_point_transactions' => ['fks' => ['customer_id' => 'customers', 'program_id' => 'loyalty_programs', 'invoice_id' => 'invoices', 'loyalty_transaction_type_id' => 'loyalty_transaction_types', 'created_by' => 'users']],
        'deals' => ['fks' => ['discount_type_id' => 'discount_types', 'created_by' => 'users', 'updated_by' => 'users', 'deleted_by' => 'users']],
        'deal_products' => ['fks' => ['deal_id' => 'deals', 'product_id' => 'products']],
    ],

    /*
    | Pivot tables without their own tenant_id whose isolation is inherited from
    | their tenant-scoped parents. Rows are copied/erased by membership in the
    | mapped id sets; keys omitted from `skip_remap` reference global tables and
    | are copied verbatim.
    */

    'pivots' => [
        'role_permissions' => ['fks' => ['role_id' => 'roles'], 'skip_remap' => ['permission_id']],
        'user_roles' => ['fks' => ['user_id' => 'users', 'role_id' => 'roles']],
        'user_permission_overrides' => ['fks' => ['user_id' => 'users', 'granted_by' => 'users'], 'skip_remap' => ['permission_id']],
        'user_warehouse_access' => ['fks' => ['user_id' => 'users', 'warehouse_id' => 'warehouses']],
        'user_till_access' => ['fks' => ['user_id' => 'users', 'till_id' => 'tills']],
        'stock_movement_serials' => ['fks' => ['stock_movement_id' => 'stock_movements', 'product_serial_id' => 'product_serials']],
    ],

    /*
    | Tables intentionally not cloned into a test instance. Audit trails are
    | per-environment operational history (and polymorphic), device tokens /
    | passkeys belong to physical devices — cloning them would push test
    | notifications to live hardware. grn_statuses is a vestigial table whose
    | ids 1..3 are global constants (grns.grn_status_id keeps its value).
    */

    'excluded' => ['audit_logs', 'invoice_audit_logs', 'device_tokens', 'passkeys', 'grn_statuses'],

    /*
    | Morph-type id columns. The id is remapped only when the resolved type
    | class maps to a cloned table; references to global classes (Geo, etc.)
    | keep their value.
    */

    'polymorphic' => [
        'customer_credit_ledger' => ['type' => 'reference_type', 'id' => 'reference_id'],
        'supplier_ledgers' => ['type' => 'reference_type', 'id' => 'reference_id'],
        'stock_movements' => ['type' => 'reference_type', 'id' => 'reference_id'],
        'damages' => ['type' => 'source_type', 'id' => 'source_id'],
    ],
];