<?php

namespace App\Http\Controllers\Client;

use App\Models\ClientProfile;
use App\Models\Client;
use App\Http\Controllers\Controller;
use App\Models\BuyBill;
use App\Models\Company;
use App\Models\Expense;
use App\Models\ExtraSettings;
use App\Models\OuterClient;
use App\Models\Package;
use App\Models\Product;
use App\Models\SaleBill;
use App\Models\SaleBillElement;
use App\Models\Supplier;
use App\Models\Type;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Auth;

// use Illuminate\Support\Facades\Artisan;

class HomeController extends Controller
{
    public function __construct()
    {
      $this->middleware('auth:client-web');
    }

    public function verification(){

        return view('client.auth.verify');
    }

    public function index()
    {
        // $result = Artisan::call('route:clear');
        $auth_id = Auth::user()->id;
        $company_id = Auth::user()->company_id;
        
        $user = Client::findOrFail($auth_id);
        $client_profile = $user->profile ;
         if ($client_profile) {
            //profile found
        } else {
            // profile not found
            
            $user->assignRole($user->role_name);
            $profile = ClientProfile::create([
                'city_name' => '',
                'age' => '',
                'gender' => '',
                'profile_pic' => 'app-assets/images/user.png',
                'client_id' => $auth_id,
                'company_id' => $company_id
            ]);
        }
        $company_id = Auth::user()->company_id;
        $company = Company::FindOrFail($company_id);
        $extra_settings = ExtraSettings::where('company_id', $company_id)->first();
        $currency = $extra_settings->currency;
        $outer_clients = $company->outerClients;
        $employees = $company->employees;

        $clients_balances_plus = array();
        $clients_balances_minus = array();
        foreach ($outer_clients as $outer_client) {
            $client_balance = $outer_client->balance;
            if ($client_balance > 0) {
                array_push($clients_balances_plus, $client_balance);
            } elseif ($client_balance < 0) {
                array_push($clients_balances_minus, abs($client_balance));
            }
        }
        $total_clients_balances_plus = array_sum($clients_balances_plus);
        $total_clients_balances_minus = array_sum($clients_balances_minus);


        $suppliers = $company->suppliers;
        $suppliers_balances_plus = array();
        $suppliers_balances_minus = array();
        foreach ($suppliers as $supplier) {
            $supplier_balance = $supplier->balance;
            if ($supplier_balance > 0) {
                array_push($suppliers_balances_plus, $supplier_balance);
            } elseif ($supplier_balance < 0) {
                array_push($suppliers_balances_minus, abs($supplier_balance));
            }
        }

        $total_suppliers_balances_plus = 0;
       $total_suppliers_balances_plus = array_sum($suppliers_balances_plus);
        $total_suppliers_balances_minus = 0;
       $total_suppliers_balances_minus = array_sum($suppliers_balances_minus);
        // اجمالى الخزن 
        $safes = $company->safes;
        $safes_balances = 0;
        foreach ($safes as $safe) {
            $safes_balances = $safes_balances + $safe->balance;

        }
        // اجمالى البنوك
        $banks = $company->banks;
        $banks_balances = 0;
        foreach ($banks as $bank) {
            $banks_balances = $banks_balances + $bank->bank_balance;
        }
        //  واجمالي قيمة البضاعة التى في المخازن
        $products = Product::where('company_id', $company_id)
            ->where('balance', '>', 0)
            ->get();
        $purchase_prices = array();
        foreach ($products as $product) {
            $product_price = $product->purchasing_price;
            $product_balance = $product->balance;

            $total_price = $product_price * $product_balance;
            array_push($purchase_prices, $total_price);
        }
        $total_purchase_prices = array_sum($purchase_prices);

        $all_products = Product::where('company_id', $company_id)
            ->get();
        $all_outer_clients = OuterClient::where('company_id', $company_id)
            ->get();
        $all_suppliers = Supplier::where('company_id', $company_id)
            ->get();
        $sale_bills = SaleBill::active()->with('elements')->where('company_id', $company_id)->where('status','done')->get();

        $profit = $this->calculatProfit($sale_bills);
        $profit_this_month = $this->calculatProfit($sale_bills->whereBetween('date',[Carbon::now()->startOfmonth(),Carbon::now()->endOfmonth()]));
        $profit_last_month = $this->calculatProfit($sale_bills ->whereBetween('date',[Carbon::now()->subMonth()->startOfmonth(),Carbon::now()->subMonth()->endOfmonth()]));

        $buy_bills = BuyBill::active()->with('elements')->where('company_id', $company_id)->where('status','done')->get();

    $expenses = Expense::where('company_id',$company_id)->where('type','مصروف')->get();

    $products_limited = Product::where('company_id',$company_id)
    ->whereColumn('balance','<=', 'min_balance')
    ->where('balance','>','0')
    ->get();
    $products_finished = Product::product()->where('company_id',$company_id)
    ->where('balance',0)
    ->get();

        return view('client.home', compact('user', 'total_purchase_prices', 'safes_balances','sale_bills','profit','expenses',
            'banks_balances', 'total_clients_balances_plus', 'total_clients_balances_minus', 'company','buy_bills','profit_this_month','profit_last_month',
            'total_suppliers_balances_plus', 'total_suppliers_balances_minus', 'currency',
            'all_products', 'all_outer_clients', 'all_suppliers','products_limited','products_finished','safes','employees'));

    }

    public function go_to_upgrade()
    {
        $company_id = Auth::user()->company_id;
        $company = Company::FindOrFail($company_id);
        $auth_id = Auth::user()->id;
        $user = Client::findOrFail($auth_id);
        $packages = Package::all();
//        $types = Type::where('type_name', '!=', 'تجربة')->get();
        return view('client.upgrade', compact('company_id', 'company', 'user', 'packages'));
    }

    public function go_to_upgrade2($id)
    {
        $company_id = Auth::user()->company_id;
        $company = Company::FindOrFail($company_id);
        $auth_id = Auth::user()->id;
        $user = Client::findOrFail($auth_id);
        $package = Package::FindOrFail($id);
        $types = Type::where('type_name', '!=', 'تجربة')
            ->where('package_id', $package->id)
            ->get();
        return view('client.upgrade2', compact('company_id', 'company', 'package', 'user', 'types'));
    }

    public function calculatProfit($sale_bills){
        $profit= 0 ;
        foreach ($sale_bills as $sale_bill) {
            $elements = SaleBillElement::where('sale_bill_id',$sale_bill->id)->get();
            foreach($elements as $element){
                $product_id = $element->product_id;
                $product = Product::FindOrFail($product_id);
                $profit += (($element->quantity ) * (($element->product_price) - $product->purchasing_price * $element->elements_unit->quantity )); 
            }   
        }
        return $profit;
    }
}
