<?php

namespace App\Repositories\Core\Auth;

use App\Models\Core\Auth\UserClient;
use App\Models\Core\Auth\User;

use App\Repositories\Clients;

class UserClients
{
    public static function associate($user_id, $clients_list)
    {
        $clients_existing = self::getClientsByUser($user_id)->toArray();
        $clients_list = is_array($clients_list) ? $clients_list : array();

        if (is_array($clients_existing)) {
            $clients_new = array_diff($clients_list, $clients_existing);
            $clients_to_delete = array_diff($clients_existing, $clients_list);
        } else {
            $clients_new = $clients_list;
            $clients_to_delete = $clients_existing;
        }

        $history_element_infos = (!empty($clients_new)) ? self::associateClients($user_id, $clients_new) : false;
        $history_element_infos2 = (!empty($clients_to_delete)) ? self::dissociateClients($user_id, $clients_to_delete) : false;
            
        // $history_element = $old_translated_name['name'];
        // $history_element_id = $documentation_category_id;
        // Histories::insert(190, $history_element_id, $history_element);

        $data['nb'] = self::countByUser($user_id);

        return $data;
    }

    public static function countByUser($id)
    {
        return UserClient::byUser($id)->count();
    }

    public static function associateClients($user_id, $clients)
    {
        $history = "";
        foreach ($clients as $key => $client_id) {
            $client = Clients::get($client_id);
            if ($client) {
                self::associate_client($user_id, $client_id);
                $history .= $client['name'] . "| ";
            }
        }
        return $history;
    }

    public static function associate_client($user_id, $client_id)
    {
        self::copyUser($user_id, $client_id);
        return UserClient::create(['user_id' => $user_id, 'client_id' => $client_id]);
    }

    public static function changePasswordsByUser($user_id, $password)
    {
        try {
            $username = User::find($user_id)->username;
            $connection = app(Connection::class);
            $clients = self::getClientsByUser($user_id);
            foreach ($clients as $client_id) {
                Clients::switchClient($client_id);
                $client_user = User::on($connection->tenantName())->withTrashed()->where('username', $username)->first();
                if ($client_user) {
                    $client_user->update(['password' => $password]);
                }
            }
        } catch (Exception $e) {
        }
        // Partners::switchPartner();
    }

    public static function copyUser($user_id, $client_id)
    {
        $connection = app(Connection::class);
        $user = User::on($connection->systemName())->find($user_id);
        $password = $user->password;
        Clients::switchClient($client_id);
        $client_user = User::on($connection->tenantName())->withTrashed()->where('username', $user->username)->first();
        if (!$client_user) {
            $user = $user->toArray();
            $user['password'] = $password;
            unset($user['id']);
            unset($user['created_at']);
            $client_user = User::on($connection->tenantName())->create($user);
            $client_user->attachRole('superadministrator');
        } else {
            if ($client_user->trashed()) {
                $client_user->restore();
            }
            $client_user->attachRole('superadministrator');
        }
        // TODO Copy avatar
        //
        // dump($client_user->toArray());
        // exit;
        // $client_user = User::on($connection->tenantName())->firstOrCreate(['username' => $user->username], $user->toArray());
        Partners::switchPartner();
    }

    public static function dissociateClients($user_id, $clients)
    {
        $history = "";
        foreach ($clients as $key => $client_id) {
            self::dissociate_client($user_id, $client_id);
            $history .= $client['name'] . "| ";
        }
        return $history;
    }

    public static function dissociate_client($user_id, $client_id)
    {
        self::deleteUser($user_id, $client_id);
        return UserClient::byUser($user_id)->byClient($client_id)->delete();
    }

    public static function deleteUser($user_id, $client_id)
    {
        $connection = app(Connection::class);
        $user = User::on($connection->systemName())->find($user_id);
        Clients::switchClient($client_id);
        $user = User::on($connection->tenantName())->where('username', $user->username)->get();
        $user->detachRole('superadministrator');
        $user->delete();
        Partners::switchPartner();
    }

    public static function delete_associate_clients($id)
    {
        return UserClient::byUser($id)->delete();
    }

    public static function select_clients_by_id($id)
    {
        return UserClient::byUser($id)->get()->pluck('client_id')->toArray();
    }

    public static function getClientsByUser($id)
    {
        return UserClient::byUser($id)->get()->pluck('client_id');
    }

    public static function getUsersByClient($id)
    {
        return UserClient::byClient($id)->get()->pluck('user_id');
    }
}
