<?php

namespace App\Livewire;

use Livewire\Component;
use Livewire\WithPagination;
use Livewire\WithoutUrlPagination;

use App\Models\User;
use Modules\Main\Models\Deal\Client;
use Modules\Main\Models\Agent\Agent;
use Modules\Main\Models\Company\CompanyAdmin;
use Modules\Main\Models\Deal\Deal;
use Modules\Main\Models\Deal\DealStatus;
use Modules\CityManager\Models\CityManager;

use Livewire\Attributes\On;

class ClientsTable extends Component
{
    use WithPagination, WithoutUrlPagination;

    public $status;
    public $count;
    public $mode;//short || full

    public function mount($status = null, $count = 10, $mode = 'full')
    {
        $this->status = $status;
        $this->count = $count;
        $this->mode = $mode;
    }

    #[On('clientCreated')]
    public function getDeals()
    {
        $deals = false;
        $clients = false;
        $user = auth()->user();

        if($cityManager = CityManager::where('user_id', auth()->id())->first()) {
            $companies = $cityManager->city->companies->pluck('id');
            $deals = Deal::whereIn('agent_id', function ($query) use ($companies)
            {
                $query->select('id');
                $query->from('agents');
                $query->whereIn('company_id', $companies);
            });
        }
        else if ($company = AdminCompanyOfUser()) {
            $deals = Deal::whereIn('agent_id', function ($query) use ($company)
            {
                $query->select('id');
                $query->from('agents');
                $query->where('company_id', $company->id);
            });
        }
        elseif ($agent = Agent::where('user_id', $user->id)->first())
        {
            $deals = Deal::where('agent_id', $agent->id);
        }
        if (!$deals) {
            return Client::where('id',0);
        }
        return $deals;
    }

    function getClients()
    {
        $deals = $this->getDeals();
        $clients = Client::join('deal_clients', 'users.id', '=', 'deal_clients.client_id')
            ->whereIn('users.id', function ($query) use ($deals)
            {
                $query->select('client_id')
                    ->from('deal_clients')
                    ->whereIn('deal_id', $deals->get()->pluck('id'));
            })
            ->orderBy('deal_clients.id', 'desc')
            ->with('deals');
        ;
        $clients = $clients->select('users.*');
        $clients = $clients->orderBy('name');

        if ($this->status && $this->status == DealStatus::UNIQUE)
        {
            $clients = $clients->whereHas('deals', function ($query)
            {
                $query->where('status', DealStatus::UNIQUE);
            })->paginate($this->count, ['*'], 'unique_clients');


        }
        elseif ($this->status && $this->status == DealStatus::NOT_UNIQUE)
        {
            $clients = $clients->whereHas('deals', function ($query)
            {
                $query->whereIn('status', [DealStatus::MODERATION, DealStatus::NEW , DealStatus::NOT_UNIQUE]);
            })->paginate($this->count, ['*'], 'not_unique_clients');
        }
        else
        {
            $clients = $clients->paginate($this->count, ['*'], 'all_clients');
        }
        return $clients;
    }
    public function render()
    {

        return view(
            'livewire.clients-table',
            [
                'clients'  => $this->getClients(),
                'statuses' => DealStatus::class
            ]
        );
    }

}
