<?php

namespace App\Services;

use Illuminate\Support\Facades\Gate;

class PriceCalculate
{

    static function calculate($day, $price, $total)
    {

        //     if(config('app.billing_day_count')==false){
        //       $d = $day;
        //     }else{
        //       $d = config('app.billing_day_count');
        //     }
        //     // dd($d);
        //     return round(($price/$d)*$total);
        // }


        // Validate and convert inputs to numeric values
        $day = is_numeric($day) ? (float)$day : 0;
        $price = is_numeric($price) ? (float)$price : 0;
        $total = is_numeric($total) ? (float)$total : 0;

        // Get billing day count with validation
        $billingDayCount = config('app.billing_day_count');

        if (config('app.billing_day_count') == false) {
            $d = $day;
        } else {
            $d = is_numeric($billingDayCount) ? (float)$billingDayCount : $day;
        }

        // Ensure we don't divide by zero
        if ($d <= 0) {
            return 0;
        }

        $calculated = ($price / $d) * $total;

        // When exact calculator is enabled and permission granted,
        // return value with 2 decimal places (e.g. 7.34)
        if (function_exists('checkSettings')
            && checkSettings('exact_calculator_calculation') === 'enable'
            && Gate::allows('exact_calculator_calculation')
        ) {
            return round($calculated, 2);
        }

        // Default behavior: rounded to integer
        return round($calculated);
    }
}
