<?php

namespace Hubleto\App\Community\Leads;

class Calendar extends \Hubleto\App\Community\Calendar\Calendar
{
  public function getCalendarConfig(): array
  {
    return [
      'position' => 4,
      'color' => '#c6aa39',
      'title' => $this->translate('Leads'),
      'addNewActivityButtonText' => $this->translate('Add new activity linked to lead'),
      'icon' => 'fas fa-people-arrows',
      'formComponent' => 'LeadFormActivity',
    ];
  }

  public function loadEvent(int $id): array
  {
    return $this->prepareLoadActivityQuery($this->getModel(Models\LeadActivity::class), $id)->first()?->toArray();
  }

  public function loadEvents(string $dateStart, string $dateEnd, array $filter = [], $idUser = 0): array
  {
    $idLead = $this->router()->urlParamAsInteger('idLead');
    $mLeadActivity = $this->getModel(Models\LeadActivity::class);
    $activities = $this->prepareLoadActivitiesQuery($mLeadActivity, $dateStart, $dateEnd, $filter)->with('LEAD.CUSTOMER');
    if ($idLead > 0) {
      $activities = $activities->where("id_lead", $idLead);
    }

    $events = $this->convertActivitiesToEvents(
      'leads',
      $activities->get()?->toArray(),
      function (array $activity) {
        if (isset($activity['LEAD'])) {
          $lead = $activity['LEAD'];
          $customer = $lead['CUSTOMER'] ?? [];
          return 'Lead #' . $lead['id'] . (isset($customer['name']) ? ', ' . $customer['name'] : '');
        } else {
          return '';
        }
      }
    );

    return $events;
  }

}
