<?php

namespace App\Core\Traits;

use App\Models\Setting;
use App\Models\Transaction;

trait CalculatesFees
{
    public function calculateFee(float $amount, ?float $serviceCharge = null, ?string $category = null): float
    {
        if ($category === 'electricity') {
            $enabled = Setting::getValue('electricity_service_charge_enabled', '0') === '1';
            if ($enabled) {
                $charge = (float) Setting::getValue('electricity_service_charge', 0);
                return $charge > 0 ? $charge : 0;
            }
            return 0;
        }

        if ($category === 'cable') {
            $enabled = Setting::getValue('cable_service_charge_enabled', '0') === '1';
            if ($enabled) {
                $charge = (float) Setting::getValue('cable_service_charge', 0);
                return $charge > 0 ? $charge : 0;
            }
            return 0;
        }

        if ($serviceCharge !== null && $serviceCharge > 0) {
            return $serviceCharge;
        }

        return 0;
    }

    public function checkDailyTransactionLimit(string $userId): bool
    {
        $limit = (int) Setting::getValue('max_daily_transactions', 50);

        $todayCount = Transaction::where('user_id', $userId)
            ->whereDate('created_at', today())
            ->count();

        return $todayCount < $limit;
    }
}
