<?php

class TieredPricing {
    
    public static function calculateTieredPrice($serviceId, $quantity) {
        $tieredServices = [1, 2];
        
        if (!in_array($serviceId, $tieredServices)) {
            return null;
        }
        
        if ($quantity < 5) {
            return 5.00;
        }
        
        if ($quantity >= 5 && $quantity < 20) {
            return 4.00;
        }
        
        if ($quantity >= 20) {
            return 4.50;
        }
        
        return 20.00;
    }
    
    public static function calculateTotalWithTiers($serviceId, $quantity, $basePrice) {
        $tieredServices = [1, 2];
        
        if (!in_array($serviceId, $tieredServices)) {
            return $basePrice * $quantity;
        }
        
        if ($quantity <= 4) {
            return 20.00;
        }
        
        if ($quantity >= 5 && $quantity < 15) {
            return 18.00;
        }
        
        if ($quantity >= 15 && $quantity < 20) {
            return 17.00;
        }
        
        if ($quantity == 20) {
            return 16.00;
        }
        
        if ($quantity > 20) {
            $baseTotal = 16.00;
            $extraQuantity = $quantity - 20;
            $extraGroups = ceil($extraQuantity / 5);
            $extraCost = $extraGroups * 5;
            return $baseTotal + $extraCost;
        }
        
        return $basePrice * $quantity;
    }
    
    public static function getTierInfo($serviceId, $quantity) {
        $tieredServices = [1, 2];
        
        if (!in_array($serviceId, $tieredServices)) {
            return null;
        }
        
        $info = [
            'is_tiered' => true,
            'current_tier' => '',
            'current_price' => 0,
            'next_tier_at' => null,
            'next_tier_price' => null,
            'savings' => 0
        ];
        
        if ($quantity <= 4) {
            $info['current_tier'] = '1-4 pieces';
            $info['current_price'] = 20.00;
            $info['next_tier_at'] = 5;
            $info['next_tier_price'] = 18.00;
            $info['savings'] = 0;
        } elseif ($quantity >= 5 && $quantity < 15) {
            $info['current_tier'] = '5-14 pieces';
            $info['current_price'] = 18.00;
            $info['next_tier_at'] = 15;
            $info['next_tier_price'] = 17.00;
            $info['savings'] = 20.00 - 18.00;
        } elseif ($quantity >= 15 && $quantity < 20) {
            $info['current_tier'] = '15-19 pieces';
            $info['current_price'] = 17.00;
            $info['next_tier_at'] = 20;
            $info['next_tier_price'] = 16.00;
            $info['savings'] = 20.00 - 17.00;
        } elseif ($quantity == 20) {
            $info['current_tier'] = '20 pieces';
            $info['current_price'] = 16.00;
            $info['next_tier_at'] = 25;
            $info['next_tier_price'] = 21.00;
            $info['savings'] = 20.00 - 16.00;
        } else {
            $info['current_tier'] = '20+ pieces';
            $baseTotal = 16.00;
            $extraQuantity = $quantity - 20;
            $extraGroups = ceil($extraQuantity / 5);
            $extraCost = $extraGroups * 5;
            $actualTotal = $baseTotal + $extraCost;
            $info['current_price'] = $actualTotal;
            $info['next_tier_at'] = null;
            $info['next_tier_price'] = null;
            $info['savings'] = 20.00 - $actualTotal;
        }
        
        return $info;
    }
    
    public static function isTieredService($serviceId) {
        return in_array($serviceId, [1, 2]);
    }
}
