<?php

namespace Hubleto\App\Community\Warehouses\Models;


use Hubleto\Framework\Db\Column\Varchar;
use Hubleto\Framework\Db\Column\Lookup;
use Hubleto\Framework\Db\Column\Decimal;
use Hubleto\Framework\Db\Column\Image;
use Hubleto\Framework\Db\Column\Integer;
use Hubleto\Framework\Db\Column\Text;
use Hubleto\App\Community\Auth\Models\User;

class Warehouse extends \Hubleto\Erp\Model
{
  public string $table = 'warehouses';
  public string $recordManagerClass = RecordManagers\Warehouse::class;
  public ?string $lookupSqlValue = '{%TABLE%}.name';
  public ?string $lookupUrlAdd = 'warehouses/add';
  public ?string $lookupUrlDetail = 'warehouses/{%ID%}';

  public array $relations = [
    'TYPE' => [ self::BELONGS_TO, WarehouseType::class, 'id_type', 'id' ],
    'OPERATION_MANAGER' => [ self::BELONGS_TO, User::class, 'id_operaion_manager', 'id' ],
  ];

  public const OPERATIONAL_STATUS_ACTIVE = 1;
  public const OPERATIONAL_STATUS_INACTIVE = 2;
  public const OPERATIONAL_STATUS_MAINTENANCE = 3;

  public const OPERATIONAL_STATUSES = [
    self::OPERATIONAL_STATUS_ACTIVE => 'Active',
    self::OPERATIONAL_STATUS_INACTIVE => 'Inactive',
    self::OPERATIONAL_STATUS_MAINTENANCE => 'Maintenance',
  ];

  public function describeColumns(): array
  {
    return array_merge(parent::describeColumns(), [
      'name' => (new Varchar($this, $this->translate('Name')))->setRequired()->setDefaultVisible()->setIcon(self::COLUMN_NAME_DEFAULT_ICON),
      'id_type' => (new Lookup($this, $this->translate('Warehouse type'), WarehouseType::class))->setDefaultVisible(),
      'address' => (new Varchar($this, $this->translate('Address')))->setDefaultVisible()->setIcon(self::COLUMN_ADDRESS_DEFAULT_ICON),
      'address_plus_code' => (new Varchar($this, $this->translate('Address Plus code'))),
      'contact_person' => (new Varchar($this, $this->translate('Contact person'))),
      'contact_email' => (new Varchar($this, $this->translate('Contact email')))->setDefaultVisible(),
      'contact_phone' => (new Varchar($this, $this->translate('Contact phone'))),
      'lng' => (new Decimal($this, $this->translate('Coordinates: longitude'))),
      'lat' => (new Decimal($this, $this->translate('Coordinates: latitude'))),
      'description' => (new Text($this, $this->translate('Description'))),
      'capacity' => (new Decimal($this, $this->translate('Capacity')))->setReadonly()->setDefaultVisible()
        ->setDescription($this->translate('Automatically calculated as total capacity of all locations in warehouse.'))
      ,
      'capacity_unit' => (new Varchar($this, $this->translate('Capacity unit')))->setDefaultVisible(),
      'current_stock_status' => (new Decimal($this, $this->translate('Current stock status')))->setDefaultVisible(),
      'operational_status' => (new Integer($this, $this->translate('Operational status')))->setDefaultVisible()
        ->setEnumValues(array_map(fn($v) => $this->translate($v), self::OPERATIONAL_STATUSES))
        ->setDefaultValue(self::OPERATIONAL_STATUS_ACTIVE)
        ->setEnumCssClasses([
          self::OPERATIONAL_STATUS_ACTIVE => 'bg-green-100 text-green-800',
          self::OPERATIONAL_STATUS_INACTIVE => 'bg-red-100 text-red-800',
          self::OPERATIONAL_STATUS_MAINTENANCE => 'bg-yellow-100 text-yellow-800',
        ])
      ,
      'photo_1' => (new Image($this, $this->translate('Photo #1'))),
      'photo_2' => (new Image($this, $this->translate('Photo #2'))),
      'photo_3' => (new Image($this, $this->translate('Photo #3'))),
      'id_operation_manager' => (new Lookup($this, $this->translate('Manager of operation'), User::class))->setReactComponent('InputUserSelect')->setDefaultVisible(),
    ]);
  }

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

    $description->addFilter('fWarehouseOperationalStatus', [
      'title' => $this->translate('Operational status'),
      'type' => 'multipleSelectButtons',
      'options' => array_map(fn($v) => $this->translate($v), self::OPERATIONAL_STATUSES)
    ]);

    $fWarehouseTypeOptions = [];
    foreach ($this->getModel(WarehouseType::class)->record->get() as $value) {
      $fWarehouseTypeOptions[$value->id] = $value->name;
    }
    $description->addFilter('fWarehouseType', [
      'title' => $this->translate('Type'),
      'type' => 'multipleSelectButtons',
      'options' => $fWarehouseTypeOptions,
    ]);

    return $description;
  }

}
