<?php

namespace App\Http\Controllers\Client;

use App\Http\Controllers\Controller;
use App\Models\Company;
use App\Models\OuterClient;
use App\Models\OuterClientAddress;
use App\Models\OuterClientNote;
use App\Models\OuterClientPhone;
use App\Models\PreSettings;
use App\Models\TimeZone;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Ramsey\Uuid\Type\Time;


class OuterClientController extends Controller
{
    public function __construct()
    {
        $this->middleware('permission:عرض قائمة العملاء', ['only' => ['index']]);
        $this->middleware('permission:اضافة عميل جديد', ['only' => ['create', 'store']]);
        $this->middleware('permission:تعديل عميل', ['only' => ['edit', 'update']]);
        $this->middleware('permission:حذف عميل', ['only' => ['delete']]);
        
    }

    public function index()
    {
        $company_id = Auth::user()->company_id;
        $company = Company::FindOrFail($company_id);
        $outer_clients = OuterClient::where('company_id', $company_id)->get();
        $balances = array();
        foreach ($outer_clients as $outer_client) {
            $client_balance = $outer_client->balance;
            array_push($balances, $client_balance);
        }
        $total_balances = array_sum($balances);
        return view('client.outer_clients.index', compact('company', 'total_balances', 'company_id', 'outer_clients'));
    }

    public function create()
    {
        $company_id = Auth::user()->company_id;
        $company = Company::FindOrFail($company_id);
        $timezones = TimeZone::all();

        $user = Auth::user();
        $type_name = $user->company->subscription->type->type_name;
        if ($type_name == "تجربة"){
            $outer_clients_count = "غير محدود";
        }
        else{
            $outer_clients_count = $user->company->subscription->type->package->outer_clients_count;
        }
        $company_outer_clients_count = $company->outerClients->count();
        if ($outer_clients_count == "غير محدود") {
            return view('client.outer_clients.create', compact('company_id', 'timezones', 'company'));
        }
        else{
            if($outer_clients_count > $company_outer_clients_count){
                return view('client.outer_clients.create', compact('company_id', 'timezones', 'company'));
            }
            else{
                return redirect()->route('client.home')->with('error','باقتك الحالية لا تسمح بالمزيد من العملاء');
            }
        }
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'client_name' => 'required',
            'client_category' => 'required',
            'balance' => 'required',
        ]);
        $data = $request->all();
        $company_id = $data['company_id'];
        $company = Company::FindOrFail($company_id);

        $type_name = $company->subscription->type->type_name;
        if ($type_name == "تجربة"){
            $outer_clients_count = "غير محدود";
        }
        else{
            $outer_clients_count = $company->subscription->type->package->outer_clients_count;
        }
        $company_outer_clients_count = $company->suppliers->count();
        if ($outer_clients_count == "غير محدود") {
            
        }
        else{
            if(!$outer_clients_count > $company_outer_clients_count){

                return response()->json(['message' => 'باقتك الحالية لا تسمح بالمزيد من العملاء','status'=>false]);
            }
        }


        $balance_type = $request->balance_type;
        if($balance_type == "for"){
            $data['balance'] = -1 * $request->balance;
        }
        elseif($balance_type == "on"){
            $data['balance'] = $request->balance;
        }
        $data['balance_start'] = $data['balance'];
        $outer_client = OuterClient::create($data);
        $notes = $request->notes;
        $phones = $request->phones;
        $addresses = $request->addresses;

        if (isset($notes) && !empty($notes)) {
            foreach ($notes as $note) {
                OuterClientNote::create([
                    'outer_client_id' => $outer_client->id,
                    'client_note' => $note,
                    'company_id' => $company_id,
                ]);
            }
        }

        if (isset($addresses) && !empty($addresses)) {
            foreach ($addresses as $address) {
                OuterClientAddress::create([
                    'outer_client_id' => $outer_client->id,
                    'client_address' => $address,
                    'company_id' => $company_id,
                ]);
            }
        }

        if (isset($phones) && !empty($phones)) {
            foreach ($phones as $phone) {
                OuterClientPhone::create([
                    'outer_client_id' => $outer_client->id,
                    'client_phone' => $phone,
                    'company_id' => $company_id,
                ]);
            }
        }

        return response()->json(['message' => 'تم إضافة العميل بنجاح','status'=>true]);
    }

    public function show($id)
    {
        $outer_client = OuterClient::FindOrFail($id);
        return view('client.outer_clients.show', compact('outer_client'));
    }

    public function edit($id)
    {
        $company_id = Auth::user()->company_id;
        $company = Company::FindOrFail($company_id);
        $timezones = TimeZone::all();
        $outer_client = OuterClient::findOrFail($id);
        return view('client.outer_clients.edit', compact('timezones', 'outer_client', 'company_id', 'company'));
    }

    public function update(Request $request)
    {
        $this->validate($request, [
            'client_name' => 'required',
            'client_category' => 'required',
            'balance_start' => 'required',
            ]);
        $data = $request->all();
        $company_id = $data['company_id'];
        $balance_type = $request->balance_type;
        $outer_client = OuterClient::FindOrFail($request->id);
        if($balance_type == "for"){
            $data['balance_start'] = -1 * $request->balance_start;
        }
        elseif($balance_type == "on"){
            $data['balance_start'] = $request->balance_start;
        }
        $outer_client->update($data);
        $notes = $request->notes;
        $addresses = $request->addresses;
        $phones = $request->phones;
        $outer_client->notes()->delete();
        $outer_client->addresses()->delete();
        $outer_client->phones()->delete();
        foreach ($notes as $note) {
            OuterClientNote::create([
                'outer_client_id' => $outer_client->id,
                'client_note' => $note,
                'company_id' => $company_id,
            ]);
        }
        foreach ($addresses as $address) {
            OuterClientAddress::create([
                'outer_client_id' => $outer_client->id,
                'client_address' => $address,
                'company_id' => $company_id,
            ]);
        }
        foreach ($phones as $phone) {
            OuterClientPhone::create([
                'outer_client_id' => $outer_client->id,
                'client_phone' => $phone,
                'company_id' => $company_id,
            ]);
        }
        return response()->json(['message' => 'تم تعديل العميل بنجاح','status'=>true]);
    }

    public function delete(Request $request)
    {
        $outer_client = OuterClient::FindOrFail($request->id);
        if ($outer_client->balance != '0') {
            return response()->json(['message' => 'خطأ في حذف العميل', 'sol'=>'يرجي تسوية ديون العميل أولا','status'=>false]);
        } else {
            $outer_client->phones()->delete();
            $outer_client->notes()->delete();
            $outer_client->addresses()->delete();
            $outer_client->delete();
            return response()->json(['message' => 'تم حذف العميل بنجاح','status'=>true]);
        }
    }

    public function print()
    {
        $company_id = Auth::user()->company_id;
        $company = Company::FindOrFail($company_id);
        $outer_clients = OuterClient::where('company_id', $company_id)->get();
        return view('client.outer_clients.print', compact('outer_clients', 'company'));
    }

    public function filter_clients()
    {
        $company_id = Auth::user()->company_id;
        $company = Company::FindOrFail($company_id);
        $nationals = OuterClient::where('company_id', $company_id)
            ->groupBy('client_national')
            ->select('client_national')
            ->get();
        return view('client.outer_clients.filter', compact('nationals', 'company'));
    }

    public function filter_key(Request $request)
    {
        $company_id = Auth::user()->company_id;
        $company = Company::FindOrFail($company_id);
        $nationals = OuterClient::where('company_id', $company_id)
            ->groupBy('client_national')
            ->select('client_national')
            ->get();
        if (isset($request->national)) {
            $national = $request->national;
            $outer_clients = OuterClient::where('company_id', $company_id)
                ->where('client_national', 'like', '%' . $national . '%')
                ->get();
        } elseif (isset($request->category)) {
            $category = $request->category;
            $outer_clients = OuterClient::where('company_id', $company_id)
                ->where('client_category', 'like', '%' . $category . '%')
                ->get();
        }
        return view('client.outer_clients.filter', compact('outer_clients', 'nationals', 'company'));
    }

    public function filter_name(Request $request)
    {
        $company_id = Auth::user()->company_id;
        $company = Company::FindOrFail($company_id);
        $client_name = $request->client_name;
        $OuterClient = OuterClient::where('company_id', $company_id)
            ->where('client_name', 'LIKE', '%' . $client_name . '%')
            ->first();
        if (empty($OuterClient)) {
            return redirect()->route('client.home')->with('error', 'لا يوجد عميل بهذا الاسم');
        } else {
            return redirect()->route('client.outer_clients.show', $OuterClient->id);
        }
    }
}
