<?php

namespace Hubleto\App\Community\Notifications\Models;


use Hubleto\Framework\Db\Column\Integer;
use Hubleto\Framework\Db\Column\Text;
use Hubleto\Framework\Db\Column\Varchar;
use Hubleto\Framework\Db\Column\Color;
use Hubleto\Framework\Db\Column\DateTime;
use Hubleto\Framework\Db\Column\Lookup;
use Hubleto\Framework\Db\Column\Json;
use Hubleto\App\Community\Auth\Models\User;


class Notification extends \Hubleto\Erp\Model
{
  public string $table = 'notifications';
  public string $recordManagerClass = RecordManagers\Notification::class;
  public ?string $lookupSqlValue = '{%TABLE%}.subject';

  private static array $categories = [
    0 => 'No category'
  ];

  public array $relations = [
    'FROM' => [ self::BELONGS_TO, User::class, 'id_from', 'id' ],
    'TO' => [ self::BELONGS_TO, User::class, 'id_to', 'id' ],
  ];

  public static function addCategory(int $id, string $category): bool
  {
    if (!isset(self::$categories[$id])) {
      self::$categories[$id] = $category;
      return true;
    } else {
      return false;
    }
  }

  public static function getCategories(): array
  {
    return self::$categories;
  }

  public function describeColumns(): array
  {
    $user = $this->getService(\Hubleto\Framework\AuthProvider::class)->getUser();
    return array_merge(parent::describeColumns(), [
      'priority' => (new Integer($this, $this->translate('Priority')))->setRequired()->setDefaultValue(1),
      'id_from' => (new Lookup($this, $this->translate('From'), User::class))->setReactComponent('InputUserSelect')->setRequired()->setDefaultVisible(),
      'id_to' => (new Lookup($this, $this->translate('To'), User::class))->setReactComponent('InputUserSelect')->setRequired()->setDefaultVisible(),
      'subject' => (new Varchar($this, $this->translate('Subject')))->setRequired()->setCssClass('font-bold')->setDefaultVisible(),
      'body' => (new Text($this, $this->translate('Body')))->setRequired(),
      'url' => (new Varchar($this, $this->translate('URL')))->setReactComponent('InputHyperlink')->setDefaultVisible(),
      'category' => (new Integer($this, $this->translate('Category')))->setRequired()->setEnumValues(array_map(fn($v) => $this->translate($v), self::getCategories())),
      'color' => (new Color($this, $this->translate('Color')))->setIcon(self::COLUMN_COLOR_DEFAULT_ICON),
      'tags' => (new Json($this, $this->translate('Tags'))),
      'datetime_sent' => (new DateTime($this, $this->translate('Sent')))->setRequired()->setReadonly()->setDefaultValue(date('Y-m-d H:i:s'))->setDefaultVisible(),
      'datetime_read' => (new DateTime($this, $this->translate('Read'))),
    ]);
  }

  /**
   * [Description for describeTable]
   *
   * @return \Hubleto\Framework\Description\Table
   * 
   */
  public function describeTable(): \Hubleto\Framework\Description\Table
  {
    $folder = $this->router()->urlParamAsString('folder');

    $description = parent::describeTable();

    $description->ui['title'] = '';
    $description->show(['header', 'fulltextSearch', 'columnSearch', 'moreActionsButton']);
    $description->hide(['footer']);
    $description->permissions['canCreate'] = false;
    $description->permissions['canDelete'] = false;
    $description->permissions['canUpdate'] = false;

    return $description;
  }

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

    $description->permissions['canDelete'] = false;
    $description->permissions['canUpdate'] = false;

    return $description;
  }

}
