<?php

namespace App\Support;

use App\Models\Coupon;
use App\Models\Setting;

class PricingCalculator
{
    public static function calculate(array $input): array
    {
        $pageCount = max(1, (int) ($input['page_count'] ?? 1));
        $copies = max(1, (int) ($input['copies'] ?? 1));
        $printMode = $input['print_mode'] ?? 'bw';
        $sides = $input['sides'] ?? 'single';
        $bindingType = $input['binding_type'] ?? 'none';
        $laminationType = $input['lamination_type'] ?? 'none';
        $pvcPrinting = (bool) ($input['pvc_printing'] ?? false);
        $coupon = $input['coupon'] ?? null;

        $bwRate = (float) Setting::getValue('pricing_per_page_bw', 3);
        $colorMultiplier = (float) Setting::getValue('pricing_color_multiplier', 2);
        $doubleSidedDiscount = (float) Setting::getValue('pricing_double_sided_discount', 0.9);
        $bindingCharge = (float) Setting::getValue('pricing_binding_charge', 25);
        $laminationCharge = (float) Setting::getValue('pricing_lamination_charge', 20);
        $pvcCharge = (float) Setting::getValue('pricing_pvc_card_charge', 90);
        $deliveryCharge = (float) Setting::getValue('pricing_delivery_charge', 60);
        $gstPercentage = (float) Setting::getValue('pricing_gst_percentage', 18);

        $unitRate = $printMode === 'color' ? $bwRate * $colorMultiplier : $bwRate;
        $billableUnits = $sides === 'double' ? (int) ceil($pageCount / 2) * $copies : $pageCount * $copies;
        $documentPrintCost = $billableUnits * $unitRate;
        $documentPrintCost = $sides === 'double' ? $documentPrintCost * $doubleSidedDiscount : $documentPrintCost;

        $serviceCharges = 0;
        if ($bindingType !== 'none') {
            $serviceCharges += $bindingCharge;
        }
        if ($laminationType !== 'none') {
            $serviceCharges += $laminationCharge;
        }
        if ($pvcPrinting) {
            $serviceCharges += $pvcCharge;
        }

        $subtotal = round($documentPrintCost + $serviceCharges, 2);

        $discount = 0;
        if ($coupon instanceof Coupon && $coupon->is_active && ($coupon->min_order_amount === null || $subtotal >= (float) $coupon->min_order_amount)) {
            $discount = $coupon->type === 'percent'
                ? round($subtotal * ((float) $coupon->value / 100), 2)
                : min($subtotal, (float) $coupon->value);
        }

        $taxableAmount = max(0, $subtotal - $discount);
        $gst = round($taxableAmount * ($gstPercentage / 100), 2);
        $total = round($taxableAmount + $gst + $deliveryCharge, 2);

        return [
            'unit_rate' => round($unitRate, 2),
            'subtotal' => $subtotal,
            'discount' => round($discount, 2),
            'gst' => $gst,
            'delivery' => round($deliveryCharge, 2),
            'total' => $total,
            'breakdown' => [
                'page_count' => $pageCount,
                'copies' => $copies,
                'billable_units' => $billableUnits,
                'print_mode' => $printMode,
                'sides' => $sides,
                'binding_type' => $bindingType,
                'lamination_type' => $laminationType,
                'pvc_printing' => $pvcPrinting,
                'gst_percentage' => $gstPercentage,
                'service_charges' => round($serviceCharges, 2),
            ],
        ];
    }
}
