<?php

namespace App\Repositories\Core\App;

use App\Models\Core\App\ApplicationClient;
use App\Repositories\Clients;

class ApplicationClients
{
    public static function associate($client_id, $applications_list)
    {
        $applications_existing = self::getApplicationsByClient($client_id);
        $applications_list = is_array($applications_list) ? $applications_list : array();

        if (is_array($applications_existing)) {
            $applications_new = array_diff($applications_list, $applications_existing);
            $applications_to_delete = array_diff($applications_existing, $applications_list);
        } else {
            $applications_new = $applications_list;
            $applications_to_delete = [];
        }

        $history_element_infos = (!empty($applications_new)) ? self::associateApplications($client_id, $applications_new) : false;
        $history_element_infos2 = (!empty($applications_to_delete)) ? self::dissociateApplications($client_id, $applications_to_delete) : false;
                    
        $data['nb'] = self::countByClient($client_id);

        return $data;
    }

    public static function countByClient($id)
    {
        return ApplicationClient::byClient($id)->count();
    }

    public static function associateApplications($client_id, $applications)
    {
        $client_name = Clients::getName($client_id);
        foreach ($applications as $key => $application_id) {
            $application = Applications::get($application_id);
            if ($application) {
                self::associateApplication($client_id, $application_id);
            }
        }
        return true;
    }

    public static function associateApplication($client_id, $application_id)
    {
        return ApplicationClient::create(['client_id' => $client_id, 'application_id' => $application_id, 'active' => true ]);
    }

    public static function dissociateApplications($client_id, $applications)
    {
        $client_name = Clients::getName($client_id);
        foreach ($applications as $key => $application_id) {
            self::dissociateApplication($client_id, $application_id);
        }
        return true;
    }

    public static function dissociateApplication($client_id, $application_id)
    {
        return ApplicationClient::byClient($client_id)->byApplication($application_id)->delete();
    }

    public static function getByClient($id)
    {
        return ApplicationClient::byClient($id)->get()->toArray();
    }

    public static function getByApplication($id)
    {
        return ApplicationClient::byApplication($id)->get()->toArray();
    }

    public static function getClientsByApplication($id)
    {
        return ApplicationClient::byApplication($id)->get()->pluck('client_id')->toArray();
    }

    public static function getApplicationsByClient($id)
    {
        return ApplicationClient::byClient($id)->get()->pluck('application_id')->toArray();
    }

    public static function isActiveByName($name)
    {
        return (!Clients::isClient()) ? true : ApplicationClient::bySlug($name)->byClient(Clients::getId())->first()->active ?? false;
    }
}
