<?php

namespace Hubleto\App\Community\Deals;

class Loader extends \Hubleto\Erp\App
{

  /**
   * Inits the app: adds routes, settings, calendars, event listeners, menu items, ...
   *
   * @return void
   * 
   */
  public function init(): void
  {
    parent::init();

    $this->router()->get([
      '/^deals\/api\/log-activity\/?$/' => Controllers\Api\LogActivity::class,
      '/^deals\/api\/generate-pdf\/?$/' => Controllers\Api\GeneratePdf::class,
      '/^deals\/api\/get-preview-html\/?$/' => Controllers\Api\GetPreviewHtml::class,
      '/^deals\/api\/get-preview-vars\/?$/' => Controllers\Api\GetPreviewVars::class,
      '/^deals\/api\/create-from-lead\/?$/' => Controllers\Api\CreateFromLead::class,
      '/^deals\/api\/generate-quotation-pdf\/?$/' => Controllers\Api\GenerateQuotationPdf::class,
      '/^deals\/api\/generate-invoice\/?$/' => Controllers\Api\GenerateInvoice::class,
      '/^deals\/api\/set-parent-lead\/?$/' => Controllers\Api\SetParentLead::class,

      '/^deals\/boards\/deal-warnings\/?$/' => Controllers\Boards\DealWarnings::class,
      '/^deals\/boards\/most-valuable-deals\/?$/' => Controllers\Boards\MostValuableDeals::class,
      '/^deals\/boards\/deal-value-by-result\/?$/' => Controllers\Boards\DealValueByResult::class,

      '/^deals(\/(?<recordId>\d+))?\/?$/' => Controllers\Deals::class,
      '/^deals\/add\/?$/' => ['controller' => Controllers\Deals::class, 'vars' => ['recordId' => -1]],
      '/^deals\/tags\/?$/' => Controllers\Tags::class,
      '/^deals\/lost-reasons\/?$/' => Controllers\LostReasons::class,
      // '/^deals\/settings\/?$/' => Controllers\Settings::class,
    ]);
    
    $this->addSearchSwitch('d', 'deals');

    /** @var \Hubleto\App\Community\Settings\Loader $settingsApp */
    $settingsApp = $this->appManager()->getApp(\Hubleto\App\Community\Settings\Loader::class);
    $settingsApp->addSetting($this, [
      'title' => $this->translate('Deal Tags'),
      'icon' => 'fas fa-tags',
      'url' => 'deals/tags',
    ]);
    $settingsApp->addSetting($this, [
      'title' => $this->translate('Deal Lost Reasons'),
      'icon' => 'fas fa-tags',
      'url' => 'deals/lost-reasons',
    ]);

    /** @var \Hubleto\App\Community\Calendar\Manager */
    $calendarManager = $this->getService(\Hubleto\App\Community\Calendar\Manager::class);
    $calendarManager->addCalendar($this, 'deals', Calendar::class);

    /** @var \Hubleto\App\Community\Workflow\Manager */
    $workflowManager = $this->getService(\Hubleto\App\Community\Workflow\Manager::class);
    $workflowManager->addWorkflow($this, 'deals', Workflow::class);

    /** @var \Hubleto\App\Community\Dashboards\Manager */
    $dashboardManager = $this->getService(\Hubleto\App\Community\Dashboards\Manager::class);
    $dashboardManager->addBoard($this, $this->translate('Deal warnings'), 'deals/boards/deal-warnings');
    $dashboardManager->addBoard($this, $this->translate('Most valuable deals'), 'deals/boards/most-valuable-deals');
    $dashboardManager->addBoard($this, $this->translate('Deal value by result'), 'deals/boards/deal-value-by-result');
  }

  public function installApp(int $round): void
  {
    if ($round == 1) {
      $mDeal = $this->getModel(Models\Deal::class);
      $mDealHistory = $this->getModel(Models\DealHistory::class);
      $mDealTag = $this->getModel(Models\Tag::class);
      $mCrossDealTag = $this->getModel(Models\DealTag::class);
      $mDealLead = $this->getModel(Models\DealLead::class);
      $mDealTask = $this->getModel(Models\DealTask::class);
      $mItem = $this->getModel(Models\Item::class);
      $mDealActivity = $this->getModel(Models\DealActivity::class);
      $mDealDocument = $this->getModel(Models\DealDocument::class);
      $mLostReasons = $this->getModel(Models\LostReason::class);

      $mLostReasons->upgradeSchema();
      $mDeal->upgradeSchema();
      $mDealHistory->upgradeSchema();
      $mDealTag->upgradeSchema();
      $mDealLead->upgradeSchema();
      $mDealTask->upgradeSchema();
      $mCrossDealTag->upgradeSchema();
      $mItem->upgradeSchema();
      $mDealActivity->upgradeSchema();
      $mDealDocument->upgradeSchema();

      $mDealTag->record->recordCreate([ 'name' => $this->translate("Important"), 'color' => '#fc2c03' ]);
      $mDealTag->record->recordCreate([ 'name' => $this->translate("ASAP"), 'color' => '#62fc03' ]);
      $mDealTag->record->recordCreate([ 'name' => $this->translate("Extenstion"), 'color' => '#033dfc' ]);
      $mDealTag->record->recordCreate([ 'name' => $this->translate("New Customer"), 'color' => '#fcdb03' ]);
      $mDealTag->record->recordCreate([ 'name' => $this->translate("Existing Customer"), 'color' => '#5203fc' ]);

      $mLostReasons->record->recordCreate(["reason" => $this->translate("Price")]);
      $mLostReasons->record->recordCreate(["reason" => $this->translate("Solution")]);
      $mLostReasons->record->recordCreate(["reason" => $this->translate("Demand canceled by customer")]);
      $mLostReasons->record->recordCreate(["reason" => $this->translate("Other")]);
    }
  }

  /**
   * [Description for renderSecondSidebar]
   *
   * @return string
   *
   */
  public function renderSecondSidebar(): string
  {
    return '
      <div class="flex flex-col gap-2">
        <a class="btn btn-square btn-primary-outline" href="' . $this->env()->projectUrl . '/deals">
          <span class="icon"><i class="fas fa-handshake"></i></span>
          <span class="text">' . $this->translate('Deals') . '</span>
        </a>
        <a class="btn btn-transparent" href="' . $this->env()->projectUrl . '/calendar?show=deals">
          <span class="icon"><i class="fas fa-calendar-days"></i></span>
          <span class="text">' . $this->translate('Calendar') . '</span>
        </a>
      </div>
    ';
  }

  /**
   * Implements fulltext search functionality for tasks
   *
   * @param array $expressions List of expressions to be searched and glued with logical 'or'.
   * 
   * @return array
   * 
   */
  public function search(array $expressions): array
  {
    $mDeal = $this->getModel(Models\Deal::class);
    $qDeals = $mDeal->record->prepareReadQuery();
    
    foreach ($expressions as $e) {
      $qDeals = $qDeals->where(function($q) use ($e) {
        $q->orWhere('deals.identifier', 'like', '%' . $e . '%');
        $q->orWhere('deals.title', 'like', '%' . $e . '%');
      })
      ->where('deals.is_closed', false);
    }

    $deals = $qDeals->get()->toArray();

    $results = [];

    foreach ($deals as $deal) {
      $results[] = [
        "id" => $deal['id'],
        "label" => $deal['identifier'] . ' ' . $deal['title'],
        "url" => 'deals/' . $deal['id'],
        // "description" => $task[''],
      ];
    }

    return $results;
  }

}
