<?php

namespace Hubleto\App\Community\Api\Models;


use Hubleto\Framework\Db\Column\Boolean;
use Hubleto\Framework\Db\Column\Color;
use Hubleto\Framework\Db\Column\Decimal;
use Hubleto\Framework\Db\Column\Date;
use Hubleto\Framework\Db\Column\DateTime;
use Hubleto\Framework\Db\Column\File;
use Hubleto\Framework\Db\Column\Image;
use Hubleto\Framework\Db\Column\Integer;
use Hubleto\Framework\Db\Column\Json;
use Hubleto\Framework\Db\Column\Lookup;
use Hubleto\Framework\Db\Column\Password;
use Hubleto\Framework\Db\Column\Text;
use Hubleto\Framework\Db\Column\Varchar;
use Hubleto\Framework\Db\Column\Virtual;
use Hubleto\App\Community\Projects\Models\Project;
use Hubleto\App\Community\Auth\Models\User;

use Hubleto\App\Community\Workflow\Models\Workflow;
use Hubleto\App\Community\Workflow\Models\WorkflowStep;

class Key extends \Hubleto\Erp\Model
{
  public string $table = 'api_keys';
  public string $recordManagerClass = RecordManagers\Key::class;
  public ?string $lookupSqlValue = 'concat(substr({%TABLE%}.key, 1, 8), " ...")';
  public ?string $lookupUrlDetail = 'api/keys/{%ID%}';

  public array $relations = [
    'PERMISSIONS' => [ self::HAS_MANY, Permission::class, 'id_key', 'id'],
  ];

  /**
   * [Description for describeColumns]
   *
   * @return array
   * 
   */
  public function describeColumns(): array
  {
    return array_merge(parent::describeColumns(), [
      'key' => (new Varchar($this, $this->translate('Key')))->setDefaultVisible()->setReadonly()->setDefaultValue(\Hubleto\Framework\Helper::generateUuidV4())->addIndex('INDEX `key` (`key`)'),
      'valid_until' => (new DateTime($this, $this->translate('Valid until')))->setDefaultVisible()->setDefaultValue(date("Y-m-d H:i:s", strtotime("+14 days"))),
      'is_enabled' => (new Boolean($this, $this->translate('Enabled')))->setDefaultValue(true)->setDefaultVisible(),
      'notes' => (new Text($this, $this->translate('Notes'))),
      'ip_address_blacklist' => (new Varchar($this, $this->translate('IP Address blacklist'))),
      'ip_address_whitelist' => (new Varchar($this, $this->translate('IP Address whitelist'))),
      'id_created_by' => (new Lookup($this, $this->translate('Created by'), User::class))->setReactComponent('InputUserSelect')->setDefaultVisible()->setRequired()->setDefaultValue($this->getService(\Hubleto\Framework\AuthProvider::class)->getUserId()),
      'created' => (new DateTime($this, $this->translate('Created')))->setReadonly()->setDefaultValue(date("Y-m-d H:i:s")),
    ]);
  }

  /**
   * [Description for describeTable]
   *
   * @return \Hubleto\Framework\Description\Table
   * 
   */
  public function describeTable(): \Hubleto\Framework\Description\Table
  {
    $description = parent::describeTable();
    $description->ui['addButtonText'] = $this->translate('Add Key');
    $description->show(['header', 'fulltextSearch', 'columnSearch', 'moreActionsButton']);
    $description->hide(['footer']);

    return $description;
  }

  /**
   * [Description for describeForm]
   *
   * @return \Hubleto\Framework\Description\Form
   * 
   */
  public function describeForm(): \Hubleto\Framework\Description\Form
  {
    return parent::describeForm();
  }

}
