<?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;


class Usage extends \Hubleto\Erp\Model
{
  public string $table = 'api_usages';
  public string $recordManagerClass = RecordManagers\Usage::class;
  public ?string $lookupSqlValue = 'concat("Api usage #", {%TABLE%}.id)';
  public ?string $lookupUrlDetail = 'api/usages/{%ID%}';

  public array $relations = [
    'KEY' => [ self::BELONGS_TO, Key::class, 'id_key', 'id' ],
  ];

  public function describeColumns(): array
  {
    return array_merge(parent::describeColumns(), [
      'id_key' => (new Lookup($this, $this->translate('Key'), Key::class))->setDefaultVisible()->setRequired()->setReadonly(),
      'app' => (new Varchar($this, $this->translate('App')))->setDefaultVisible()->setRequired()->addIndex('INDEX `app` (`app`)')->setReadonly(),
      'controller' => (new Varchar($this, $this->translate('Controller')))->setDefaultVisible()->setRequired()->addIndex('INDEX `controller` (`controller`)')->setReadonly(),
      'used_on' => (new DateTime($this, $this->translate('Used on')))->setReadonly()->setDefaultValue(date("Y-m-d H:i:s"))->setReadonly(),
      'ip_address' => (new Varchar($this, $this->translate('IP address')))->setDefaultVisible()->addIndex('INDEX `ip_address` (`ip_address`)')->setReadonly(),
      'status' => (new Varchar($this, $this->translate('Status'))->setReadonly())
        ->setEnumValues([
          0 => $this->translate('Unknown'),
          1 => $this->translate('Success'),
          2 => $this->translate('Error'),
        ])
      ,
    ]);
  }

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

}
