<?php

namespace Hubleto\App\Community\Orders\Models\RecordManagers;

use Hubleto\App\Community\Products\Models\RecordManagers\Product;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Hubleto\App\Community\Auth\Models\RecordManagers\User;
use Hubleto\App\Community\Invoices\Models\RecordManagers\Item as InvoiceItem;

class Item extends \Hubleto\Erp\RecordManager
{
  public $table = 'orders_items';

  /** @return BelongsTo<Order, covariant OrderItem> */
  public function ORDER(): BelongsTo
  {
    return $this->belongsTo(Order::class, 'id_order', 'id');
  }

  /** @return BelongsTo<Item, covariant Item> */
  public function PRODUCT(): BelongsTo
  {
    return $this->belongsTo(Product::class, 'id_product', 'id');
  }

  /** @return BelongsTo<User, covariant User> */
  public function INVOICE_ITEM(): BelongsTo
  {
    return $this->belongsTo(InvoiceItem::class, 'id_invoice_item', 'id');
  }

  /** @return BelongsTo<User, covariant Lead> */
  public function OWNER(): BelongsTo
  {
    return $this->belongsTo(User::class, 'id_owner', 'id');
  }

  /** @return BelongsTo<User, covariant Lead> */
  public function MANAGER(): BelongsTo
  {
    return $this->belongsTo(User::class, 'id_manager', 'id');
  }

  /**
   * [Description for prepareSelectsForReadQuery]
   *
   * @param mixed|null $query
   * @param int $level
   * @param array|null|null $includeRelations
   * 
   * @return array
   * 
   */
  public function prepareSelectsForReadQuery(mixed $query = null, int $level = 0, array|null $includeRelations = null): array
  {
    $hubleto = \Hubleto\Erp\Loader::getGlobalApp();
    $filters = $hubleto->router()->urlParamAsArray("filters");
    $selects = parent::prepareSelectsForReadQuery($query, $level, $includeRelations);

    if (isset($filters['fGroupBy']) && is_array($filters['fGroupBy'])) {
      $selects[] = 'sum(ifnull(orders_items.unit_price, 0) * ifnull(orders_items.amount, 0) * (1 - ifnull(orders_items.discount, 0)/100)) as total_price_excl_vat';
      $selects[] = 'sum(ifnull(orders_items.unit_price, 0) * ifnull(orders_items.amount, 0) * (1 - ifnull(orders_items.discount, 0)/100) * (1 + ifnull(orders_items.vat, 0)/100)) as total_price_incl_vat';
    }

    return $selects;
  }

  /**
   * [Description for prepareReadQuery]
   *
   * @param mixed|null $query
   * @param int $level
   * @param array|null|null $includeRelations
   * 
   * @return mixed
   * 
   */
  public function prepareReadQuery(mixed $query = null, int $level = 0, array|null $includeRelations = null): mixed
  {
    $query = parent::prepareReadQuery($query, $level, $includeRelations);

    $hubleto = \Hubleto\Erp\Loader::getGlobalApp();
    $idOrder = $hubleto->router()->urlParamAsInteger("idOrder");

    if ($idOrder > 0) $query = $query->where($this->table . '.id_order', $idOrder);

    $filters = $hubleto->router()->urlParamAsArray("filters");

    switch ($filters["fStatus"] ?? 0) {
      case 1: $query = $query->whereNull($this->table . ".id_invoice_item"); break;
      case 2: $query = $query->where($this->table . ".id_invoice_item", ">", 0); break;
    }

    switch ($filters["fDue"] ?? 0) {
      case 1: $query = $query->whereDate($this->table . ".date_due", "<=", date("Y-m-d")); break;
      case 2: $query = $query->whereDate($this->table . ".date_due", ">", date("Y-m-d")); break;
    }

    if (isset($filters['fGroupBy'])) {
      $fGroupBy = (array) $filters['fGroupBy'];
      if (in_array('order', $fGroupBy)) $query = $query->groupBy('id_order');
    }

    return $query;
  }

  /**
   * [Description for prepareLookupQuery]
   *
   * @param string $search
   * 
   * @return mixed
   * 
   */
  public function prepareLookupQuery(string $search): mixed
  {
    $query = parent::prepareLookupQuery($search);

    $hubleto = \Hubleto\Erp\Loader::getGlobalApp();
    $idOrder = $hubleto->router()->urlParamAsInteger("idOrder");

    if ($idOrder > 0) $query = $query->where($this->table . '.id_order', $idOrder);
    return $query;
  }
}
