<?php

//
// This example demonstrates how to write a drush
// script.  These scripts are run with the php-script command.
//
use Drush\Drush;

$this->output()->writeln("User names regenerating");

$this->output()->writeln("The extra options/arguments to this command were:");
$this->output()->writeln(print_r($extra, true));
// if ($extra[0] === "delete") {
//   $delete = true;
// } else {
//   $delete = false;
// }


$database = \Drupal::database();

//  __ __       _  _     
// |  \  \ ___ <_>| | ___
// |     |<_> || || |<_-<
// |_|_|_|<___||_||_|/__/
                      

$this->output()->writeln("");

$result = $database->query("
    SELECT mail
    FROM {users_field_data}
    GROUP BY mail
    HAVING COUNT(*) > 1");

$dupmail = [];
foreach ($result as $user) {
  // $this->output()->writeln($user->mail);
  $dupmail[] = $user->mail;
}

$this->output()->writeln(count($dupmail) . ' dup emails');


$this->output()->writeln("");


$this->output()->writeln("All users Name <- Mail");
$ufd_query = $database->select('users_field_data', 'ufd')
  ->fields('ufd', ['uid', 'name', 'mail'])
  ->condition('ufd.uid', [0,1], 'NOT IN')
  ->condition('mail', $dupmail, 'NOT IN');


$ufd_result = $ufd_query->execute();


foreach ($ufd_result as $ufd) {
  $user_updated = $database->update('users_field_data')
  ->fields([
    'name' => $ufd->mail
  ])
  ->condition('uid', $ufd->uid)
  ->execute();
}


//  __ __              _                 
// |  \  \ ___ ._ _ _ | |_  ___  _ _  ___
// |     |/ ._>| ' ' || . \/ ._>| '_><_-<
// |_|_|_|\___.|_|_|_||___/\___.|_|  /__/
                                      
$this->output()->writeln("");

$this->output()->writeln("All users Name <- Profile member");

$p_query = $database->select('profile', 'p');
$p_query->join('profile__field_name', 'pfn', 'pfn.entity_id = p.profile_id');
$p_query->join('profile__field_first_name', 'pffn', 'pffn.entity_id = p.profile_id');
$p_query->join('user__roles', 'ur', 'ur.entity_id = p.uid');
$p_query->join('users_field_data', 'ufd', 'ufd.uid = p.uid');
$p_query
  ->fields('p', ['profile_id', 'uid'])
  ->fields('pfn', ['field_name_value'])
  ->fields('pffn', ['field_first_name_value'])
  ->fields('ur', ['entity_id', 'roles_target_id'])
  ->fields('ufd', ['mail'])
  ->condition('p.type', 'member')
  ->condition('ur.roles_target_id', 'adherent');

$p_result = $p_query->execute();

// avoid duplicates
$members = [];
foreach ($p_result as $p) {
  // $this->output()->writeln("profile name ". $p->field_first_name_value . ' ' . $p->field_name_value);
  $name = $p->field_first_name_value . ' ' . $p->field_name_value;
  $name = str_replace(' Col.', '', $name);
  if (!in_array($name, array_values($members), true)) {
    $members[] = [
      "name" => $name,
      "mail" => $p->mail,
      "uid" => $p->uid
    ];
  }
}

// $members = array_unique($members);
// array_multisort($members);
// $this->output()->writeln(print_r($members, true));

// $this->output()->writeln(print_r(array_count_values($members)));

$this->output()->writeln("members count ". count($members));

$prev_name = "";
foreach ($members as $member) {
  $this->output()->writeln("updating " . $member['uid'] . " " . $member['name'] . " | " . $member['mail']);
  $user_updated = $database->update('users_field_data')
  ->fields([
    'name' => $member['name']
  ])
  ->condition('uid', $member['uid'])
  ->execute();
}
  
//  ___             _                            
// |  _> _ _  ___ _| |_ ___ ._ _ _  ___  _ _  ___
// | <__| | |<_-<  | | / . \| ' ' |/ ._>| '_><_-<
// `___/`___|/__/  |_| \___/|_|_|_|\___.|_|  /__/
                                              

$this->output()->writeln("All users Name <- Profile customer");

$p_query = $database->select('profile', 'p');
$p_query->join('profile__address', 'pa', 'pa.entity_id = p.profile_id');
$p_query->join('user__roles', 'ur', 'ur.entity_id = p.uid');
$p_query->join('users_field_data', 'ufd', 'ufd.uid = p.uid');
$p_query
  ->fields('p', ['profile_id', 'uid'])
  ->fields('pa', ['address_given_name', 'address_family_name'])
  ->fields('ur', ['entity_id', 'roles_target_id'])
  ->fields('ufd', ['mail'])
  ->condition('p.type', 'customer')
  ->condition('ufd.mail', ['info@materio.com'], 'NOT IN')
  ->condition('ur.roles_target_id', 'adherent');

$p_result = $p_query->execute();

// avoid duplicates
$customers = [];
foreach ($p_result as $p) {
  // $this->output()->writeln("profile name ". $p->field_first_name_value . ' ' . $p->field_name_value);
  $name = $p->address_given_name . ' ' . $p->address_family_name;
  $name = str_replace(' Col.', '', $name);
  if (!in_array($name, array_values($customers), true)) {
    $customers[] = [
      "name" => $name,
      "mail" => $p->mail,
      "uid" => $p->uid
    ];
  }
}

// $customers = array_unique($customers);
// array_multisort($customers);
// $this->output()->writeln(print_r($customers, true));

// $this->output()->writeln(print_r(array_count_values($customers)));


$this->output()->writeln("customers count ". count($customers));

$prev_name = "";
foreach ($customers as $customer) {
  $this->output()->writeln("updating " . $customer['uid'] . " " . $customer['name'] . " | " . $customer['mail']);
  $user_updated = $database->update('users_field_data')
  ->fields([
    'name' => $customer['name']
  ])
  ->condition('uid', $customer['uid'])
  ->execute();
}
  


