<?php

namespace Hubleto\App\Community\Leads;

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([
      '/^leads\/api\/log-activity\/?$/' => Controllers\Api\LogActivity::class,
      '/^leads\/api\/save-bulk-status-change\/?$/' => Controllers\Api\SaveBulkStatusChange::class,

      '/^leads\/boards\/lead-value-by-score\/?$/' => Controllers\Boards\LeadValueByScore::class,
      '/^leads\/boards\/lead-warnings\/?$/' => Controllers\Boards\LeadWarnings::class,

      '/^leads(\/(?<recordId>\d+))?\/?$/' => Controllers\Leads::class,
      '/^leads\/add?\/?$/' => ['controller' => Controllers\Leads::class, 'vars' => [ 'recordId' => -1 ]],
      '/^leads\/settings\/?$/' => Controllers\Settings::class,

      '/^leads\/tags\/?$/' => Controllers\Tags::class,
      '/^leads\/levels\/?$/' => Controllers\Levels::class,
      '/^leads\/lost-reasons\/?$/' => Controllers\LostReasons::class,
    ]);

    // $this->addSearchSwitch('l', 'leads');
    // $this->addSearchSwitch('t', 'taskleads');

    $settingsApp = $this->appManager()->getApp(\Hubleto\App\Community\Settings\Loader::class);
    $settingsApp->addSetting($this, [
      'title' => $this->translate('Lead Levels'),
      'icon' => 'fas fa-layer-group',
      'url' => 'leads/levels',
    ]);
    $settingsApp->addSetting($this, [
      'title' => $this->translate('Lead Tags'),
      'icon' => 'fas fa-tags',
      'url' => 'leads/levels',
    ]);
    $settingsApp->addSetting($this, [
      'title' => $this->translate('Lead Lost Reasons'),
      'icon' => 'fas fa-tags',
      'url' => 'leads/lost-reasons',
    ]);

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

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

    /** @var \Hubleto\App\Community\Dashboards\Manager */
    $boards = $this->getService(\Hubleto\App\Community\Dashboards\Manager::class);
    $boards->addBoard( $this, $this->translate('Lead value by score'), 'leads/boards/lead-value-by-score');
    $boards->addBoard( $this, $this->translate('Lead warnings'), 'leads/boards/lead-warnings');

    /** @var \Hubleto\App\Community\Desktop\AppMenuManager */
    $appMenu = $this->getService(\Hubleto\App\Community\Desktop\AppMenuManager::class);
    $appMenu->addItem($this, 'leads', $this->translate('Active leads'), 'fas fa-people-arrows');
    $appMenu->addItem($this, 'leads/archive', $this->translate('Archived leads'), 'fas fa-box-archive');
  }

  public function installApp(int $round): void
  {
    if ($round == 1) {
      $mLevel = $this->getModel(Models\Level::class);
      $mLead = $this->getModel(Models\Lead::class);
      $mLeadHistory = $this->getModel(Models\LeadHistory::class);
      $mLeadTag = $this->getModel(Models\Tag::class);
      $mCrossLeadTag = $this->getModel(Models\LeadTag::class);
      $mLeadTask = $this->getModel(Models\LeadTask::class);
      $mLeadCampaign = $this->getModel(Models\LeadCampaign::class);
      $mLeadActivity = $this->getModel(Models\LeadActivity::class);
      $mLeadDocument = $this->getModel(Models\LeadDocument::class);
      $mLostReasons = $this->getModel(Models\LostReason::class);

      $mLevel->upgradeSchema();
      $mLostReasons->upgradeSchema();
      $mLead->upgradeSchema();
      $mLeadHistory->upgradeSchema();
      $mLeadTag->upgradeSchema();
      $mCrossLeadTag->upgradeSchema();
      $mLeadActivity->upgradeSchema();
      $mLeadDocument->upgradeSchema();
      $mLeadCampaign->upgradeSchema();
      $mLeadTask->upgradeSchema();

      $mLeadTag->record->recordCreate([ 'name' => $this->translate("Complex"), 'color' => '#2196f3' ]);
      $mLeadTag->record->recordCreate([ 'name' => $this->translate("Great opportunity"), 'color' => '#4caf50' ]);
      $mLeadTag->record->recordCreate([ 'name' => $this->translate("Duplicate"), 'color' => '#9e9e9e' ]);
      $mLeadTag->record->recordCreate([ 'name' => $this->translate("Needs attention"), 'color' => '#795548' ]);

      $mLevel->record->recordCreate([ 'name' => $this->translate("Cold"), 'color' => '#2196f3' ]);
      $mLevel->record->recordCreate([ 'name' => $this->translate("Warm"), 'color' => '#4caf50' ]);
      $mLevel->record->recordCreate([ 'name' => $this->translate("Hot"), 'color' => '#9e9e9e' ]);
      $mLevel->record->recordCreate([ 'name' => $this->translate("Marketing qualified"), 'color' => '#795548' ]);
      $mLevel->record->recordCreate([ 'name' => $this->translate("Sales qualified"), 'color' => '#795548' ]);

      $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 . '/leads">
          <span class="icon"><i class="fas fa-people-arrows"></i></span>
          <span class="text">' . $this->translate('Leads') . '</span>
        </a>
        <a class="btn btn-transparent" href="' . $this->env()->projectUrl . '/calendar?show=leads">
          <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
  // {
  //   $mLead = $this->getModel(Models\Lead::class);
  //   $qLeads = $mLead->record->prepareReadQuery();
    
  //   foreach ($expressions as $e) {
  //     $qLeads = $qLeads->where(function($q) use ($e) {
  //       $q->orWhere('leads.id', 'like', '%' . $e . '%');
  //       $q->orWhere('leads.title', 'like', '%' . $e . '%');
  //     })
  //     ->where('leads.is_closed', false);
  //   }

  //   $leads = $qLeads->get()->toArray();

  //   $results = [];

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

  //   return $results;
  // }

}
