<?php

namespace App\Services\Calculation;

use App\Models\Contract;

class PriceCalculationService
{
    public static function forContract(Contract $contract): CalculationPipeline
    {
        $pipeline = new CalculationPipeline();

        // Bước 1: Tổng giá trị trước chiết khấu
        $pipeline->addStep(new CalculationStep(
            name: 'Giá trị QSDĐ',
            outputKey: 'land_value',
            formula: fn ($data) => (float) ($data['land_value'] ?? 0),
            roundingRule: RoundingRule::UNIT,
            dependencies: []
        ));

        $pipeline->addStep(new CalculationStep(
            name: 'Giá trị Móng',
            outputKey: 'foundation_value',
            formula: fn ($data) => (float) ($data['foundation_value'] ?? 0),
            roundingRule: RoundingRule::UNIT,
            dependencies: []
        ));

        $pipeline->addStep(new CalculationStep(
            name: 'Tổng giá trị trước chiết khấu',
            outputKey: 'subtotal',
            formula: fn ($data) => $data['land_value'] + $data['foundation_value'],
            roundingRule: RoundingRule::UNIT,
            dependencies: ['land_value', 'foundation_value']
        ));

        // Bước 2: Chiết khấu
        $pipeline->addStep(new CalculationStep(
            name: 'Chiết khấu',
            outputKey: 'discount_amount',
            formula: function ($data) {
                $details = $data['discount_details'] ?? [];
                if (!empty($details['total_amount'])) {
                    return (float) $details['total_amount'];
                }
                if (!empty($details['total_percentage'])) {
                    return $data['subtotal'] * ((float) $details['total_percentage'] / 100);
                }
                return 0;
            },
            roundingRule: RoundingRule::UNIT,
            dependencies: ['subtotal', 'discount_details']
        ));

        // Bước 3: Sau chiết khấu
        $pipeline->addStep(new CalculationStep(
            name: 'Giá trị sau chiết khấu',
            outputKey: 'net_value',
            formula: fn ($data) => $data['subtotal'] - $data['discount_amount'],
            roundingRule: RoundingRule::UNIT,
            dependencies: ['subtotal', 'discount_amount']
        ));

        // Bước 4: VAT (nếu có)
        $pipeline->addStep(new CalculationStep(
            name: 'Thuế VAT',
            outputKey: 'vat_amount',
            formula: function ($data) {
                $vatRate = (float) ($data['vat_rate'] ?? 0);
                return $data['net_value'] * ($vatRate / 100);
            },
            roundingRule: RoundingRule::UNIT,
            dependencies: ['net_value', 'vat_rate']
        ));

        // Bước 5: Tổng thanh toán
        $pipeline->addStep(new CalculationStep(
            name: 'Tổng thanh toán',
            outputKey: 'total_payment',
            formula: fn ($data) => $data['net_value'] + $data['vat_amount'],
            roundingRule: RoundingRule::UNIT,
            dependencies: ['net_value', 'vat_amount']
        ));

        return $pipeline;
    }

    public static function calculateForContract(Contract $contract, array $overrides = []): CalculationResult
    {
        $pipeline = self::forContract($contract);
        $data = [
            'land_value' => (float) $contract->land_value,
            'foundation_value' => (float) $contract->foundation_value,
            'discount_details' => $contract->discount_details ?? [],
            'vat_rate' => (float) ($contract->metadata['vat_rate'] ?? 0),
        ];

        // Áp dụng ghi đè nếu có
        if (!empty($overrides)) {
            foreach ($pipeline->getSteps() as $step) {
                if (isset($overrides[$step->outputKey()])) {
                    $step->override((int) $overrides[$step->outputKey()]);
                }
            }
        }

        return $pipeline->execute($data);
    }
}
