<?php

namespace App\Services;

use Carbon\Carbon;
use App\Models\Client;
use App\Models\Packages;
use App\Models\BillGenerate;
use App\Models\BillGeneratesArchive;
use App\Services\ExpireCheck;
use App\Models\CustomerAccount;
use App\Models\ResellerCommissionReference;
use App\Models\SubResellerCommissionReference;
use App\Services\PriceCalculate;
use Illuminate\Support\Facades\DB;

class CommissionService
{
  public function reseller_commission($amount, $actions, $reseller_id, $commission, $package_price, $client_id,$bill_or_payment = null,$bill_or_payment_id= null)
  {
    // dd($commission);
    if ($commission > 0) {
        // dd($bill_or_payment_id,$bill_or_payment);
        if($bill_or_payment == "bill"){
            ResellerCommissionReference::create([
                'received_amount' => $this->calculateCommission($commission, $package_price, $amount),
                'actions' => $actions,
                'user_id' => auth()->user()->id ?? 1,
                'reseller_id' => $reseller_id,
                'client_id' => $client_id,
                'bill_generate_id'=> $bill_or_payment_id,
              ]);
        }else if($bill_or_payment == "payment"){
            ResellerCommissionReference::create([
                'received_amount' => $this->calculateCommission($commission, $package_price, $amount),
                'actions' => $actions,
                'user_id' => auth()->user()->id ?? 1,
                'reseller_id' => $reseller_id,
                'client_id' => $client_id,
                'bill_payment_id'=> $bill_or_payment_id,

              ]);
        }else{

            ResellerCommissionReference::create([
              'received_amount' => $this->calculateCommission($commission, $package_price, $amount),
              'actions' => $actions,
              'user_id' => auth()->user()->id ?? 1,
              'reseller_id' => $reseller_id,
              'client_id' => $client_id

            ]);
        }
    }
  }

  public function subreseller_commission($amount, $actions, $pop_id, $commission, $package_price, $client_id)
  {
    if ($commission > 0) {
      SubResellerCommissionReference::create([
        'received_amount' => $this->calculateCommission($commission, $package_price, $amount),
        'actions' => $actions,
        'user_id' => auth()->user()->id,
        'pop_id' => $pop_id,
        'client_id' => $client_id
      ]);
    }
  }

  public function calculateCommission($commission, $package_price, $amount)
  {
    return (($commission * $amount) / $package_price);
  }
}
