<?php

declare(strict_types=1);

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

namespace App\Business\Sales;

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

/**
 * 销售等长前后窗对比。两窗均调用销售分析 summary，不做原因诊断。
 */
final class SalesTrendCompareQuery
{
    public function __construct(
        private readonly SalesAnalysisReportService $salesAnalysisReportService,
    ) {}

    /**
     * @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', 'confirmed_order_count', 'partial_shipped_order_count', 'fully_shipped_order_count'],
                ['final_amount', 'receivable_amount', 'received_amount', 'unreceived_amount'],
                ['total_quantity'],
                'final_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->salesAnalysisReportService->normalizeFilters([
            ...$input,
            'business_time_from' => $from,
            'business_time_until' => $until,
        ]);

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

        return [
            'order_count' => (int) $summary['order_count'],
            'final_amount' => (string) $summary['final_amount'],
            'receivable_amount' => (string) $summary['receivable_amount'],
            'received_amount' => (string) $summary['received_amount'],
            'unreceived_amount' => (string) $summary['unreceived_amount'],
            'total_quantity' => (string) $summary['total_quantity'],
            'confirmed_order_count' => (int) $summary['confirmed_order_count'],
            'partial_shipped_order_count' => (int) $summary['partial_shipped_order_count'],
            'fully_shipped_order_count' => (int) $summary['fully_shipped_order_count'],
        ];
    }
}
