<?php

declare(strict_types=1);

/**
 * 本文件属于 DBErp 进销存系统。
 *
 * 版权所有 © 2026 北京珑大钜商科技有限公司。保留所有权利。
 *
 * 未经官方授权，不得删除、篡改或隐藏本文件中的版权声明、产品标识与来源信息。
 * 商业授权、官方更新与技术支持服务，请以珑大官方支持中心记录为准。
 *
 * @link https://support.loongdom.cn
 * @see  https://www.dberp.com.cn
 */

namespace App\Business\Purchase;

use App\Business\Report\PurchaseAnalysisReportService;
use App\Business\Report\ReportPeriodCompareMapper;
use App\Tools\Support\ToolPeriodComparison;

/**
 * 采购等长前后窗对比。两窗均调用采购分析 summary，不做原因诊断。
 */
final class PurchaseTrendCompareQuery
{
    public function __construct(
        private readonly PurchaseAnalysisReportService $purchaseAnalysisReportService,
    ) {}

    /**
     * @param  array<string, mixed>  $input
     * @param  array{code: string, meaning: string}  $currency
     * @return array<string, mixed>
     */
    public function compare(array $input, array $currency): array
    {
        $window = ToolPeriodComparison::equalPrecedingWindow(
            $input['date_from'] ?? null,
            $input['date_until'] ?? null,
            'date_from',
            'date_until',
        );

        $current = $this->summary($input, $window['from'], $window['until']);
        $previous = $this->summary($input, $window['previous_from'], $window['previous_until']);

        return [
            'current' => [
                'period' => [
                    'from' => $window['from'],
                    'until' => $window['until'],
                ],
                ...$current,
            ],
            'previous' => [
                'period' => [
                    'from' => $window['previous_from'],
                    'until' => $window['previous_until'],
                ],
                ...$previous,
            ],
            'change' => ReportPeriodCompareMapper::change(
                $current,
                $previous,
                [
                    'order_count',
                    'overdue_arrival_order_count',
                    'partial_received_order_count',
                    'fully_received_order_count',
                    'exception_count',
                    'return_order_count',
                ],
                ['total_amount', 'paid_amount', 'unpaid_amount'],
                ['total_quantity', 'arrived_quantity', 'remaining_arrival_quantity'],
                'total_amount',
            ),
            'currency' => $currency,
        ];
    }

    /**
     * @param  array<string, mixed>  $input
     * @return array<string, int|string>
     */
    private function summary(array $input, string $from, string $until): array
    {
        $filters = $this->purchaseAnalysisReportService->normalizeFilters([
            ...$input,
            'business_time_from' => $from,
            'business_time_until' => $until,
        ]);

        $summary = $this->purchaseAnalysisReportService->summary($filters);

        return [
            'order_count' => (int) $summary['order_count'],
            'total_amount' => (string) $summary['total_amount'],
            'paid_amount' => (string) $summary['paid_amount'],
            'unpaid_amount' => (string) $summary['unpaid_amount'],
            'total_quantity' => (string) $summary['total_quantity'],
            'arrived_quantity' => (string) $summary['arrived_quantity'],
            'remaining_arrival_quantity' => (string) $summary['remaining_arrival_quantity'],
            'overdue_arrival_order_count' => (int) $summary['overdue_arrival_order_count'],
            'partial_received_order_count' => (int) $summary['partial_received_order_count'],
            'fully_received_order_count' => (int) $summary['fully_received_order_count'],
            'exception_count' => (int) $summary['exception_count'],
            'return_order_count' => (int) $summary['return_order_count'],
        ];
    }
}
