<?php
/**
 * Copyright (C) 2021  Jaap Jansma (jaap.jansma@civicoop.org)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

namespace Civi\ActionProvider\Action\Contact;

use Civi\ActionProvider\Action\AbstractAction;
use Civi\ActionProvider\Parameter\ParameterBagInterface;
use Civi\ActionProvider\Parameter\Specification;
use Civi\ActionProvider\Parameter\SpecificationBag;
use CRM_ActionProvider_ExtensionUtil as E;

class CreateNote extends AbstractAction {

  /**
   * Run the action
   *
   * @param ParameterBagInterface $parameters
   *   The parameters to this action.
   * @param ParameterBagInterface $output
   *   The parameters this action can send back
   *
   * @return void
   */
  protected function doAction(ParameterBagInterface $parameters, ParameterBagInterface $output) {
    $apiCall = \Civi\Api4\Note::create();
    $apiCall->setCheckPermissions(false);
    $apiCall->addValue('entity_table', 'civicrm_contact');
    $apiCall->addValue('entity_id', $parameters->getParameter('contact_id'));
    $apiCall->addValue('note', $parameters->getParameter('note'));
    if ($parameters->doesParameterExists('note_date')) {
      $apiCall->addValue('note_date', $parameters->getParameter('note_date'));
    }
    if ($parameters->doesParameterExists('subject')) {
      $apiCall->addValue('subject', $parameters->getParameter('subject'));
    }
    $result = $apiCall->execute()->first();
    $output->setParameter('id', $result['id']);
  }

  /**
   * Returns the specification of the configuration options for the actual
   * action.
   *
   * @return SpecificationBag
   */
  public function getConfigurationSpecification() {
    return new SpecificationBag([]);
  }

  /**
   * Returns the specification of the parameters of the actual action.
   *
   * @return SpecificationBag
   */
  public function getParameterSpecification() {
    return new SpecificationBag([
      new Specification('contact_id', 'Integer', E::ts('Contact ID'), true),
      new Specification('note', 'String', E::ts('Note'), true),
      new Specification('subject', 'String', E::ts('Subject'), false),
      new Specification('note_date', 'Date', E::ts('Note Date'), false),
    ]);
  }

  /**
   * Returns the specification of the output parameters of this action.
   *
   * This function could be overriden by child classes.
   *
   * @return SpecificationBag
   */
  public function getOutputSpecification() {
    return new SpecificationBag([
      new Specification('id', 'Integer', E::ts('Note ID'))
    ]);
  }


}
