<?php

final class ExtensionStorePublisher extends ExtensionStoreDAO
  implements
    PhabricatorPolicyInterface,
    PhabricatorApplicationTransactionInterface,
    PhabricatorSubscribableInterface,
  //   PhabricatorFlaggableInterface,
  //   PhabricatorFerretInterface,
  //   PhabricatorFulltextInterface,
  //   PhabricatorMentionableInterface,
  //   PhabricatorConduitResultInterface,
    PhabricatorDestructibleInterface {


  protected $name;
  protected $publisherKey;
  protected $status;
  protected $description;
  protected $details = array();

  protected $viewPolicy;
  protected $editPolicy;

  // owners are just CAN_PUBLISH ?

  public static function initializeNewPublisher($actor) {
    return new self()
      ->setViewPolicy(PhabricatorPolicies::getMostOpenPolicy()) // TODO + xaction
      ->setEditPolicy($actor->getPHID())  // TODO + xaction
      ->setStatus(ExtensionStorePublisherStatus::STATUS_ACTIVE);
  }

  protected function getConfiguration() {
    return array(
      self::CONFIG_AUX_PHID => true,
      self::CONFIG_SERIALIZATION => array(
        'details' => self::SERIALIZATION_JSON,
      ),
      self::CONFIG_COLUMN_SCHEMA => array(
        'name' => 'sort255',
        'publisherKey' => 'sort64',
        'status' => 'text32',
        'description' => 'text',
      ),
      self::CONFIG_KEY_SCHEMA => array(
        'key_publisherkey' => array(
          'columns' => array('publisherKey'),
          'unique' => true,
        ),
      ),
    ) + parent::getConfiguration();
  }

  public function getPHIDType() {
    return ExtensionStorePublisherPHIDType::TYPECONST;
  }

  public function getURI() {
    $publisher_key = $this->getPublisherKey();
    return "/extensionstore/publisher/{$publisher_key}/";
  }

  public function isArchived() {
    return $this->status == ExtensionStorePublisherStatus::STATUS_ARCHIVED;
  }

/* -(  PhabricatorPolicyInterface  )----------------------------------------- */

  public function getCapabilities() {
    return array(
      PhabricatorPolicyCapability::CAN_VIEW,
      PhabricatorPolicyCapability::CAN_EDIT,
      // CAN_PUBLISH
    );
  }

  public function getPolicy($capability) {
    switch ($capability) {
      case PhabricatorPolicyCapability::CAN_VIEW:
        return PhabricatorPolicies::POLICY_PUBLIC;
    }
    // TODO
    return PhabricatorPolicies::POLICY_ADMIN;
  }

  public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
    return true;

  }


/* -(  PhabricatorApplicationTransactionInterface  )------------------------- */

  public function getApplicationTransactionEditor() {
    return new ExtensionStorePublisherTransactionEditor();
  }

  public function getApplicationTransactionTemplate() {
    return new ExtensionStorePublisherTransaction();
  }



/* -(  PhabricatorDestructibleInterface  )----------------------------------- */

  public function destroyObjectPermanently(
    PhabricatorDestructionEngine $engine) {

    $this->openTransaction();
      // TODO ......
      $this->delete();
    $this->saveTransaction();

  }

/* -(  PhabricatorSubscribableInterface  )----------------------------------- */

  public function isAutomaticallySubscribed($phid) {
    return false; // TODO - owners?
  }


}
