<?php

use Drupal\user_provisioning\Api\MoAuthApi;
use Drupal\user_provisioning\moUserProvisioningConstants;
use Drupal\user_provisioning\moUserProvisioningUtilities;

/**
 * Implements hook_schema().
 */
function user_provisioning_schema()
{
    $schema['mo_user_provisioning_users'] = array(
        'description' => t('User relationship between scim client and server entity'),
        'fields' => array(
            'uid' => array(
                'description' => 'The {users}.uid this record affects.',
                'type' => 'int',
                'unsigned' => TRUE,
                'not null' => TRUE,
                'default' => 0,
            ),
            'id' => array(
                'description' => t('Returned id of the resource from the SCIM SERVER'),
                'type' => 'varchar',
                'length' => 128,
                'not null' => TRUE,
                'default' => '',
            ),
        ),
        'foreign keys' => array(
            'data_user' => array(
                'table' => 'users',
                'columns' => array(
                    'uid' => 'uid',
                ),
            ),
        ),
    );

    $schema['mo_user_provisioning_audits_and_logs'] = array(
        'description' => t('Audits and logs information of the provisioning operations'),
        'fields' => array(
            'uid' => array(
                'description' => 'The {users}.uid this record affects.',
                'type' => 'int',
                'unsigned' => TRUE,
                'not null' => TRUE,
                'default' => 0,
            ),
            'name' => array(
                'description' => 'The name of the entity.',
                'type' => 'varchar_ascii',
                'length' => 128,
                'not null' => TRUE,
                'default' => '',
            ),
            'created' => array(
                'description' => 'The Unix timestamp when the operation was executed.',
                'type' => 'int',
                'unsigned' => TRUE,
                'not null' => TRUE,
                'default' => 0,
            ),
            'operation' => array(
                'description' => 'The operation type.',
                'type' => 'varchar_ascii',
                'length' => 128,
                'not null' => TRUE,
                'default' => '',
            ),
            'status' => array(
                'description' => 'The status of the operation.',
                'type' => 'int',
                'not null' => TRUE,
                'default' => 1,
                'size' => 'small',
            ),
        ),
        'foreign keys' => array(
            'data_user' => array(
                'table' => 'users',
                'columns' => array(
                    'uid' => 'uid',
                ),
            ),
        ),
    );

    return $schema;
}

/**
 * Implements hook_install().
 */
function user_provisioning_install($is_syncing)
{
    $timezone = date_default_timezone_get();
    date_default_timezone_set($timezone);
    $install_date = date('M j\, Y h:i:s A \(e\)');
    $install_date = moUserProvisioningUtilities::drupalIsCli() ? "[Command Line] " . $install_date : $install_date;
    $editConfig = \Drupal::configFactory()->getEditable('user_provisioning.settings');
    $editConfig->set('user_provisioning_feedback', '1')->save();
    $editConfig->set('user_provisioning_install_date',$install_date)->save();
}

