<?php

declare(strict_types=1);

namespace App\Business\Integration\Adapters;

use App\Business\Integration\Contracts\ChannelSyncAdapterInterface;
use App\Business\Integration\DTOs\SyncContext;

final class DbshopChannelAdapter implements ChannelSyncAdapterInterface
{
    public function describeCapabilities(): array
    {
        return [
            '提供 DBShop 商品、客户、订单的基础拉取样例。',
            '提供 ERP 库存向 DBShop 的基础推送回执样例。',
            '保持联通逻辑停留在 Integration 域，不在 ERP 主域散落商城逻辑。',
        ];
    }

    public function pullProducts(array $filters = []): array
    {
        $products = [
            [
                'external_id' => 'dbshop-product-1001',
                'external_code' => 'SPU-1001',
                'name' => 'DBShop 联名马克杯',
                'barcode' => '6900000001001',
                'mnemonic_code' => 'DBMK',
                'brand_name' => 'DBShop',
                'spec_model' => '白色 400ml',
                'external_sku' => 'SKU-1001-WHT',
                'sales_price' => '69.0000',
                'cost_price' => '33.0000',
                'is_enabled' => true,
                'can_purchase' => true,
                'can_sale' => true,
                'manages_inventory' => true,
                'category' => [
                    'external_id' => 'dbshop-category-home',
                    'external_code' => 'HOME',
                    'name' => '家居好物',
                ],
            ],
            [
                'external_id' => 'dbshop-product-1002',
                'external_code' => 'SPU-1002',
                'name' => 'DBShop 旅行收纳包',
                'barcode' => '6900000001002',
                'mnemonic_code' => 'DBSN',
                'brand_name' => 'DBShop',
                'spec_model' => '深灰 大号',
                'external_sku' => 'SKU-1002-GREY',
                'sales_price' => '129.0000',
                'cost_price' => '62.0000',
                'is_enabled' => true,
                'can_purchase' => true,
                'can_sale' => true,
                'manages_inventory' => true,
                'category' => [
                    'external_id' => 'dbshop-category-travel',
                    'external_code' => 'TRAVEL',
                    'name' => '出行装备',
                ],
            ],
        ];

        return $this->applyFilter($products, $filters);
    }

    public function pullCustomers(array $filters = []): array
    {
        $customers = [
            [
                'external_id' => 'dbshop-member-2001',
                'name' => '陈北',
                'mobile' => '13800000001',
                'email' => 'chenbei@example.com',
                'address' => '上海市浦东新区测试路 66 号',
            ],
            [
                'external_id' => 'dbshop-member-2002',
                'name' => '林桥',
                'mobile' => '13800000002',
                'email' => 'linqiao@example.com',
                'address' => '杭州市西湖区示例街 8 号',
            ],
        ];

        return $this->applyFilter($customers, $filters);
    }

    public function pullOrders(array $filters = []): array
    {
        $orders = [
            [
                'external_id' => 'dbshop-order-3001',
                'order_no' => 'DS202604190001',
                'status' => 'paid',
                'business_time' => '2026-04-19 18:20:00',
                'customer_external_id' => 'dbshop-member-2001',
                'customer_name' => '陈北',
                'customer_mobile' => '13800000001',
                'remark' => 'DBShop 首批联通订单样例',
                'items' => [
                    [
                        'external_line_id' => 'dbshop-order-3001-line-1',
                        'external_product_id' => 'dbshop-product-1001',
                        'external_sku' => 'SKU-1001-WHT',
                        'quantity' => '2.0000',
                        'unit_price' => '69.0000',
                        'discount_amount' => '10.0000',
                    ],
                ],
            ],
            [
                'external_id' => 'dbshop-order-3002',
                'order_no' => 'DS202604190002',
                'status' => 'pending_payment',
                'business_time' => '2026-04-19 19:05:00',
                'customer_external_id' => 'dbshop-member-2002',
                'customer_name' => '林桥',
                'customer_mobile' => '13800000002',
                'remark' => '待支付样例订单',
                'items' => [
                    [
                        'external_line_id' => 'dbshop-order-3002-line-1',
                        'external_product_id' => 'dbshop-product-1002',
                        'external_sku' => 'SKU-1002-GREY',
                        'quantity' => '1.0000',
                        'unit_price' => '129.0000',
                        'discount_amount' => '0.0000',
                    ],
                ],
            ],
        ];

        return $this->applyFilter($orders, $filters);
    }

    public function pushInventory(array $payload, SyncContext $context): array
    {
        return [
            'accepted' => true,
            'channel' => 'dbshop',
            'reference' => $context->reference,
            'synced_at' => now()->toDateTimeString(),
            'available_quantity' => $payload['available_quantity'] ?? null,
            'realtime_quantity' => $payload['realtime_quantity'] ?? null,
        ];
    }

    private function applyFilter(array $records, array $filters): array
    {
        return array_values(array_filter($records, function (array $record) use ($filters): bool {
            if (($filters['external_id'] ?? null) !== null && ($record['external_id'] ?? null) !== $filters['external_id']) {
                return false;
            }

            if (($filters['external_code'] ?? null) !== null && ($record['external_code'] ?? null) !== $filters['external_code']) {
                return false;
            }

            if (($filters['order_no'] ?? null) !== null && ($record['order_no'] ?? null) !== $filters['order_no']) {
                return false;
            }

            return true;
        }));
    }
}