<?php

/**
 * Tags is Static Segments
 */

class Am_Newsletter_Plugin_Mailchimp extends Am_Newsletter_Plugin
{
    protected const SEGMENT_VARS_NAME = 'mailchimp-merge-tags';

    protected function _initSetupForm(Am_Form_Setup $form)
    {
        $el = $form->addSecretText('api_key', ['class' => 'am-el-wide'])
            ->setLabel("MailChimp API Key");
        $el->addRule('required');
        $el->addRule('regex', 'API Key must be in form xxxxxxxxxxxx-xxx', '/^[a-zA-Z0-9]+-[a-zA-Z0-9]{2,4}$/');
        $form->addAdvCheckbox('disable_double_optin')
            ->setLabel("Disable Double Opt-in\n"
            . 'read more <a href="http://kb.mailchimp.com/article/how-does-confirmed-optin-or-double-optin-work" target="_blank" rel="noreferrer" class="link">on mailchimp site</a>');

        if ($this->isConfigured()) {
            $form->addAdvCheckbox('ecommerce', ['id' => 'ecommerce-trigger'])
                ->setLabel(___('Enable Ecommerce Tracking'));
            $fs = $form->addFieldset('', ['id' =>' ecommerce-fieldset'])
                ->setLabel('Store Configuration');
            $form->addScript()->setScript(<<<CUT
    jQuery(document).ready(function(){
        jQuery("#ecommerce-trigger").on('change', function(){
            jQuery("#ecommerce-fieldset").toggle(jQuery(this).is(':checked'));
        }).change();
    });
CUT
);
            $req = $this->getApi()->sendRequest('ecommerce/stores/' . $this->getStoreId());
            if (@$req['status'] == 404) {
                if ($this->getConfig('_list')) {
                    $resp = $this->getApi()->sendRequest(
                        'ecommerce/stores',
                        [
                            'id' => $this->getStoreId(),
                            'list_id' => $this->getConfig('_list'),
                            'name' => $this->getDi()->config->get('site_title'),
                            'platform' => 'aMember PRO',
                            'currency_code' => $this->getDi()->config->get('currency'),
                        ],
                        Am_HttpRequest::METHOD_POST
                    );
                    if (!@$resp['id']) {
                        $fs->addHtml()
                            ->setHtml("<div class='error'>Unable to create Store</div>")
                            ->setLabel(___('Store Setup'));
                    }
                } else {
                    $gr = $fs->addGroup()
                        ->setLabel(___('Setup Store'));
                    $gr->addHtml()
                        ->setHtml(<<<CUT
<div>Mailchimp require that each store should be associated with single audience(list).
Each list could have multiple stores associated, but store is assigned to single list, and that list can't be changed later.
Please select list and click save.</div>
CUT
                    );
                    $gr->addSelect('_list', ['id' => 'mailchimp-list'])
                        ->loadOptions($this->getListOptions());
                }
            } else {
                $fs->addHtml()->setHtml("<div class='info'>Store is configured</div>")
                    ->setLabel(___('Store Setup'));
            }
        }
        $form->addAdvCheckbox('debug')
            ->setLabel("Debug logging\nRecord debug information in the log");
    }

    public function isConfigured()
    {
        return (bool)$this->getConfig('api_key');
    }

    public function getApi(): Am_Mailchimp_Api
    {
        return new Am_Mailchimp_Api($this);
    }

    public function changeSubscription(User $user, array $addLists, array $deleteLists)
    {
        foreach ($addLists as $list_id) {
            if (!$this->addUserViaApi($user, $list_id)) return false;

            if ($tags = $this->getSegmentsForList($list_id)) {
                foreach ($tags as $tag) {
                    $this->getApi()->sendRequest(
                        "lists/{$list_id}/segments/{$tag}/members",
                        ['email_address' => $user->email],
                        Am_HttpRequest::METHOD_POST
                    );
                }
            }
        }

        foreach ($deleteLists as $list_id) {
            $ret = $this->getApi()->sendRequest(
                "lists/$list_id/members/" . md5(strtolower($user->email)),
                [
                    'status' => 'unsubscribed',
                    'status_if_new' => 'unsubscribed',
                    'email_address' => $user->email,
                ],
                Am_HttpRequest::METHOD_PUT
            );
            if (!$ret) return false;

            if ($tags = $this->getSegmentsForList($list_id)) {
                foreach ($tags as $tag) {
                    $this->getApi()->sendRequest(
                        "lists/{$list_id}/segments/{$tag}/members/" . md5(strtolower($user->email)),
                        null,
                        Am_HttpRequest::METHOD_DELETE
                    );
                }
            }
        }
        return true;
    }

    public function changeEmail(User $user, $oldEmail, $newEmail)
    {
        $ef = 'email';
        $list_ids = $this->getDi()->newsletterUserSubscriptionTable->getSubscribedIds($user->pk());
        $lists = [];
        foreach ($this->getDi()->newsletterListTable->loadIds($list_ids) as $list) {
            if ($list->plugin_id != $this->getId()) continue;
            $lists[] = $list->plugin_list_id;
        }
        $user->set($ef, $oldEmail)->toggleFrozen(true);
        $this->changeSubscription($user, [], $lists);
        // subscribe again
        $user->set($ef, $newEmail)->toggleFrozen(false);
        $this->changeSubscription($user, $lists, []);
    }

    public function getLists()
    {
        $this->getDi()->cacheBackend->clean(
            Zend_Cache::CLEANING_MODE_MATCHING_TAG,
            ['mailchimp_data']
        );

        return $this->_getLists();
    }

    public function addUserViaApi(User $user, $list_id): bool
    {
        $ret = $this->getApi()->sendRequest(
            "lists/$list_id/members/" . md5(strtolower($user->email)),
            [
                'status' => $this->getConfig('disable_double_optin') ? 'subscribed' : 'pending',
                'status_if_new' => $this->getConfig('disable_double_optin') ? 'subscribed' : 'pending',
                'email_address' => $user->email,
                'merge_fields' => $this->getMergeFieldsForList($user, $list_id),
                'skip_merge_validation' => true
            ],
            Am_HttpRequest::METHOD_PUT
        );
        return (bool)$ret;
    }

    public function onSubscriptionChanged(Am_Event_SubscriptionChanged $event)
    {
        $user = $event->getUser();

        foreach ($event->getAdded() as $product_id) {
            $tags = $this->getSegmentsForProduct($product_id);
            foreach ($tags as $_) {
                [$list_id, $tag] = explode('-', $_);
                $this->getApi()->sendRequest(
                    "lists/{$list_id}/segments/{$tag}/members",
                    ['email_address' => $user->email],
                    Am_HttpRequest::METHOD_POST
                );
            }
        }
        foreach ($event->getDeleted() as $product_id) {
            $tags = $this->getSegmentsForProduct($product_id);
            foreach ($tags as $_) {
                [$list_id, $tag] = explode('-', $_);
                $this->getApi()->sendRequest(
                    "lists/{$list_id}/segments/{$tag}/members/" . md5(strtolower($user->email)),
                    null,
                    Am_HttpRequest::METHOD_DELETE
                );
            }
        }
    }

    public function onGridProductInitForm(Am_Event_Grid $event)
    {
        $grid = $event->getGrid();
        $form = $grid->getForm();
        $grid->addCallback(Am_Grid_Editable::CB_VALUES_FROM_FORM, function($values, $record){
            $record->data()->setBlob('mailchimp-segments', json_encode($values['_mailchimp_segments']));
        });
        $grid->addCallback(Am_Grid_Editable::CB_VALUES_TO_FORM, function(&$values, $record){
            $values['_mailchimp_segments'] = json_decode($record->data()->getBlob('mailchimp-segments') ?: '[]', true);
        });

        $form->addMagicSelect('_mailchimp_segments', ['class' => 'am-combobox'])
            ->setLabel("Mailchimp List Segments\n"
                . "tags users when user makes purchase of this product "
                . "user will be tagged only if account is suppose to be added "
                . "to list according to aMember CP -> Protect Content -> Newsletters configuration")
            ->loadOptions($this->getListSegmentOptions());
    }

    public function onGridNewsletterInitGrid(Am_Event_Grid $event)
    {
        $grid = $event->getGrid();
        $grid->addCallback(Am_Grid_Editable::CB_VALUES_FROM_FORM, function (&$values, NewsletterList $record) {
            if (isset($values['_merge_vars'])) {
                $vars = $record->getVars();
                $vars['mailchimp-merge-vars'] = $values['_merge_vars'];
                $record->setVars($vars);
            }
            if (isset($values['_segments'])) {
                $vars = $record->getVars();
                $vars[self::SEGMENT_VARS_NAME] = $values['_segments'];
                $record->setVars($vars);
            }

        });
        $grid->addCallback(Am_Grid_Editable::CB_VALUES_TO_FORM, function(&$values, NewsletterList $record){
            $vars = $record->getVars();
            $values['_merge_vars'] = !empty($vars['mailchimp-merge-vars'])
                ? $vars['mailchimp-merge-vars']
                : ['FNAME' => 'name_f', 'LNAME' => 'name_l', 'LOGIN' => 'login'];
            $values['_segments'] = !empty($vars[self::SEGMENT_VARS_NAME])
                ? $vars[self::SEGMENT_VARS_NAME]
                : [];
        });

        $grid->addCallback(Am_Grid_Editable::CB_INIT_FORM, function(Am_Form_Admin $form) use ($grid) {
            $list = $grid->getRecord();
            if (
                $list
                && $list->plugin_id == $this->getId()
                && $list->isLoaded()
            ) {
                $list_id = $list->plugin_list_id;
                $mFields = $this->getMergeFieldOptions($list_id);
                if (!empty($mFields)) {
                    $amFields = [
                        "" => "-- Please Select --",
                        "login" => 'aMember: Username (login)',
                        "email" => 'aMember: E-Mail Address (email)',
                        "name_f" => 'aMember: First Name (name_f)',
                        "name_l" => 'aMember: Last Name (name_l)',
                        "street" => 'aMember: Street Address (street)',
                        "street2" => 'aMember: Street Address, Second Line (street2)',
                        "city" => 'aMember: City (city)',
                        "state" => 'aMember: State (state)',
                        "zip" => 'aMember: ZIP Code (zip)',
                        "country" => 'aMember: Country (country)',
                        "phone" => 'aMember: Phone Number (phone)',
                    ];
                    foreach ($this->getDi()->userTable->customFields()->getAll() as $fk => $fv) {
                        $amFields[$fk] = sprintf('aMember: %s (%s)', $fv->title, $fk);
                    }
                    $fs = $form->addAdvFieldset();
                    $fs->setLabel(___('Define Merge Fields Integration'));

                    foreach($mFields as $k => $v) {
                        $fs->addSelect("_merge_vars[{$k}]")
                            ->setLabel(sprintf("%s [%s]", $v, $k))
                            ->loadOptions($amFields);
                    }
                }
                $ret = $this->getSegmentOptions($list_id);
                if (!empty($ret)) {
                    $fs = $form->addAdvFieldset();
                    $fs->setLabel(___('Define Segments Integration'));
                    $fs->addMagicSelect('_segments')
                        ->setLabel(___('Add these Segments'))
                        ->loadOptions($ret);
                }
            }
        });
    }

    public function onUserAfterInsert(Am_Event_UserAfterInsert $e)
    {
        parent::onUserAfterInsert($e);

        if (
            $this->getConfig('ecommerce')
            && $this->getDi()->request->getCookie('mc_cid')
            && $this->getDi()->request->getCookie('mc_eid')
        ) {
            $user = $e->getUser();
            $user->data()
                ->set('mc_cid', $this->getDi()->request->getCookie('mc_cid'))
                ->set('mc_eid', $this->getDi()->request->getCookie('mc_eid'))
                ->update();
        }
    }

    public function onSignupUserUpdated(Am_Event $event)
    {
        if (
            $this->getConfig('ecommerce')
            && $this->getDi()->request->getCookie('mc_cid')
            && !$event->getUser()->data()->get('mc_cid')
        ) {
            $user = $event->getUser();
            $user->data()
                ->set('mc_cid', $this->getDi()->request->getCookie('mc_cid'))
                ->update();
        }
    }

    public function onPaymentAfterInsert(Am_Event_PaymentAfterInsert $e)
    {
        $payment = $e->getPayment();
        $user = $e->getUser();
        if (!$this->getConfig('ecommerce')) return;

        $ret = $this->getApi()->sendRequest(
            "lists/" . $this->getConfig('_list') . "/members/" . md5(strtolower($user->email)),
            [],
            Am_HttpRequest::METHOD_GET
        );
        if (empty($ret['id']))
            return;

        $lines = [];
        foreach ($payment->getInvoice()->getItems() as $invoiceItem) {
            $lines[] = [
                'id' => (string)$invoiceItem->invoice_item_id,
                'product_id' => (string)$invoiceItem->item_id,
                'product_variant_id' => (string)$invoiceItem->billing_plan_id,
                'quantity' => (int)$invoiceItem->qty,
                'price' => $invoiceItem->first_price,
                'discount' => $invoiceItem->first_discount
            ];
            $product = $invoiceItem->tryLoadProduct();
            if (empty($product))
                continue;
            if (!$product->data()->get('mailchimp-id')) {
                $variants = [];
                foreach ($product->getBillingPlans() as $bp) {
                    /** @var BillingPlan $bp */
                    $variants[] = [
                        'id' => (string)$bp->pk(),
                        'title' => $bp->title,
                        'price' => $bp->first_price
                    ];
                }
                $prResp = $this->getApi()->sendRequest(
                    'ecommerce/stores/' . $this->getStoreId() . '/products',
                    [
                        'id' => (string)$product->getProductId(),
                        'title' => $product->getTitle(),
                        'description' => $product->getDescription(),
                        'variants' => $variants
                    ],
                    Am_HttpRequest::METHOD_POST
                );

                if (@$prResp['id']) {
                    $product->data()->set('mailchimp-id', $prResp['id'])->update();
                }
            }
        }
        $data = [
            'id' => (string)$payment->invoice_payment_id,
            'customer' => [
                'id' => (string)$user->user_id,
                'email_address' => $user->email,
                'first_name'=> $user->name_f,
                'last_name' => $user->name_l,
                'opt_in_status' => false
            ],
            'financial_status' => 'paid',
            'currency_code' => $payment->currency,
            'order_total' => $payment->amount,
            'discount_total' => $payment->discount,
            'tax_total' => $payment->tax,
            'processed_at_foreign' => $payment->dattm,
            'lines' => $lines
        ];
        if ($mc_cid = $this->getVar($user, 'mc_cid')) {
            $data['campaign_id'] = $mc_cid;
        }
        $this->getApi()->sendRequest(
            'ecommerce/stores/' . $this->getStoreId() . '/orders',
            $data,
            Am_HttpRequest::METHOD_POST
        );
    }

    public function onGetFlexibleActions(Am_Event $event)
    {
        try {
            $segments = $this->getListSegmentOptions();
        } catch (Am_Exception $e) {
            return;
        }
        $lists = [];
        foreach ($segments as $list_segment => $title) {
            [$list_id, $segment_id] = explode('-', $list_segment, 2);
            if (empty($lists[$list_id])) {
                $t = preg_replace('#/.+#', '', $title);
                $event->addReturn(new Am_FlexibleAction_MailchimpListAdd($list_id, $t, $this));
                $event->addReturn(new Am_FlexibleAction_MailchimpListDelete($list_id, $t, $this));
                $lists[$list_id] = true;
            }
            $event->addReturn(new Am_FlexibleAction_MailchimpSegmentAdd($list_id, $segment_id, $title, $this));
            $event->addReturn(new Am_FlexibleAction_MailchimpSegmentDelete($list_id, $segment_id, $title, $this));
        }
    }

    public function onInitFinished()
    {
        // Do not track anything for admin pages or if tracking is disabled.
        if (defined('AM_ADMIN') || !$this->getConfig('ecommerce_tracking'))
            return;

        // mailchimp send two variables via get: mc_cid, and mc_eid both should be set in cookies.
        if ($mc_cid = $this->getDi()->request->getFiltered('mc_cid'))
            Am_Cookie::set('mc_cid', $mc_cid, time() + 3600 * 24 * 30);

        if ($mc_eid = $this->getDi()->request->getFiltered('mc_eid'))
            Am_Cookie::set('mc_eid', $mc_eid, time() + 3600 * 24 * 30);
    }

    protected function getMergeFieldsForList(User $user, $list_id): array
    {
        $default = [
            'FNAME' => $user->name_f,
            'LNAME' => $user->name_l,
            'LOGIN' => $user->login
        ];
        $list = $this->getDi()->newsletterListTable->findFirstBy(['plugin_id' => $this->getId(), 'plugin_list_id' => $list_id]);
        if (!$list)
            return $default;

        $vars = $list->getVars();
        if (empty($vars['mailchimp-merge-vars']))
            return $default;

        $ret = [];
        foreach ($vars['mailchimp-merge-vars'] as $k => $v) {
            $ret[$k] = $user->get($v) ?: $user->data()->get($v);
        }

        return array_filter($ret);
    }

    protected function getSegmentsForList($list_id)
    {
        $list = $this->getDi()->newsletterListTable->findFirstBy(['plugin_id' => $this->getId(), 'plugin_list_id' => $list_id]);

        if (!$list)
            return [];

        $vars = $list->getVars();

        if (empty($vars[self::SEGMENT_VARS_NAME]))
            return [];

        return $vars[self::SEGMENT_VARS_NAME];
    }

    protected function getSegmentsForProduct($product_id)
    {
        $product = $this->getDi()->productTable->load($product_id);
        $tags = $product->data()->getBlob('mailchimp-segments');
        if ($tags) {
            return json_decode($tags, true);
        }
        return [];
    }

    protected function _getLists()
    {
        $lists = $this->getApi()->sendRequest('lists', ['count' => 1000]);
        $ret = [];
        foreach ($lists['lists'] as $l) {
            $ret[$l['id']] = ['title' => $l['name']];
        }
        return $ret;
    }

    protected function getListSegmentOptions()
    {
        return $this->getDi()->cacheFunction->call(
            fn() => $this->_getListSegmentOptions(),
            [],
            ['mailchimp_data', 'id' => __CLASS__ . __FUNCTION__]
        );
    }

    protected function _getListSegmentOptions()
    {
        $options = [];
        foreach ($this->_getLists() as $list_id => $list) {
            foreach ($this->getSegmentOptions($list_id) as $tag_id => $tag_name) {
                $options["{$list_id}-{$tag_id}"] = $list['title'] . "/" . $tag_name;
            }
        }
        return $options;
    }

    protected function getSegmentOptions($list_id)
    {
        return $this->getDi()->cacheFunction->call(
            fn($list_id) => $this->_getSegmentOptions($list_id),
            [$list_id],
            ['mailchimp_data', 'id' => __CLASS__ . __FUNCTION__ . md5($list_id)]
        );
    }

    protected function _getSegmentOptions($list_id)
    {
        $ret = $this->getApi()->sendRequest("lists/{$list_id}/segments", ['count' => 200]);

        $options = [];
        if (!empty($ret) && !empty($ret['segments'])) {
            foreach ($ret['segments'] as $v) {
                $options[$v['id']] = $v['name'];
            }
        }
        return $options;
    }

    protected function getMergeFieldOptions(string $list_id): array
    {
        return $this->getDi()->cacheFunction->call(
            fn($list_id) => $this->_getMergeFieldOptions($list_id),
            [$list_id],
            ['mailchimp_data', 'id' => __CLASS__ . __FUNCTION__ . md5($list_id)]
        );
    }

    protected function _getMergeFieldOptions(string $list_id): array
    {
        $mFields = [];
        $ret = $this->getApi()->sendRequest("lists/{$list_id}/merge-fields");
        if (!empty($ret)) {
            foreach ($ret['merge_fields'] as $field) {
                $mFields[$field['tag']] = "Mailchimp: " . $field['name'];
            }
        }

        return $mFields;
    }

    protected function getVar(User $user, $name)
    {
        return $user->data()->get($name) ?: $this->getDi()->request->getCookie($name);
    }

    protected function getStoreId(): string
    {
        return $this->getDi()->security->siteHash('MAILCHIMP-STORE');
    }
}

class Am_Mailchimp_Api extends Am_HttpRequest
{
    protected Am_Newsletter_Plugin_Mailchimp $plugin;
    protected $vars = [];
    protected $params = [];

    public function __construct(Am_Newsletter_Plugin_Mailchimp $plugin)
    {
        $this->plugin = $plugin;
        parent::__construct();
    }

    public function sendRequest($path, $params = [], $method = self::METHOD_GET)
    {
        [, $server] = explode('-', $this->plugin->getConfig('api_key'), 2);

        $server = filterId($server);

        if (empty($server))
            throw new Am_Exception_Configuration("Wrong API Key set for MailChimp");

        $url = "https://{$server}.api.mailchimp.com/3.0/" . $path;

        $this->setMethod($method == self::METHOD_GET ? self::METHOD_GET : self::METHOD_POST);

        if (!in_array($method, [self::METHOD_POST, self::METHOD_GET])) {
            $this->setHeader('X-HTTP-Method-Override', $method);
        }

        $this->setAuth('anystring', $this->plugin->getConfig('api_key'));
        $this->setHeader('Content-Type', 'application/json');

        if ($method == self::METHOD_GET) {
            $this->setUrl($url . '?' . http_build_query($params));
        } else {
            $this->setUrl($url);
            if ($params)
                $this->setBody(json_encode($params));
        }
        try {
            $ret = parent::send();
        } catch(Exception $e) {
            $this->plugin->getDi()->errorLogTable->logException($e);
            return false;
        }
        $this->plugin->debug($this, $ret);
        $arr = json_decode($ret->getBody(), true);
        if (!$arr && ($method != self::METHOD_DELETE)) {
            $this->plugin->getDi()->logger->error(
                "MailChimp API Error - unknown response [{body}]",
                ['body' => $ret->getBody()]
            );
            return false;
        }
        if (isset($arr['errors'])) {
            $this->plugin->getDi()->logger->error(
                "MailChimp API Error - {errors}",
                ['errors' => $arr['errors']]
            );
            return false;
        }
        if (isset($arr['error'])) {
            $this->plugin->getDi()->logger->error(
                "MailChimp API Error - [{error}]",
                ['error' => $arr['error']]
            );
            return false;
        }
        return $arr;
    }
}

if (interface_exists('Am_FlexibleAction_Interface', true)):

abstract class Am_FlexibleAction_Mailchimp implements Am_FlexibleAction_Interface
{
    protected string $list_id;
    protected Am_Newsletter_Plugin_Mailchimp $plugin;

    function __construct($list_id, $title, Am_Newsletter_Plugin_Mailchimp $plugin)
    {
        $this->list_id = $list_id;
        $this->title = $title;
        $this->plugin = $plugin;
    }

    function commit() {}
}

class Am_FlexibleAction_MailchimpListAdd extends Am_FlexibleAction_Mailchimp
{
    function getTitle()
    {
        return "MailChimp: Add to List: " . Am_Html::escape($this->title);
    }

    function getId()
    {
        return 'mailchimp-list-add-' . $this->list_id;
    }

    function run(User $user)
    {
        $this->plugin->addUserViaApi($user, $this->list_id);
    }
}

class Am_FlexibleAction_MailchimpListDelete extends Am_FlexibleAction_Mailchimp
{
    function getTitle()
    {
        return "MailChimp: Delete From List: " . Am_Html::escape($this->title);
    }

    function getId()
    {
        return 'mailchimp-list-delete-' . $this->list_id;
    }

    function run(User $user)
    {
        $this->plugin->getApi()->sendRequest(
            "lists/{$this->list_id}/members/" . md5(strtolower($user->email)),
            null,
            Am_HttpRequest::METHOD_DELETE
        );
    }
}

class Am_FlexibleAction_MailchimpSegmentAdd extends Am_FlexibleAction_Mailchimp
{
    protected string $segment;

    function __construct($list_id, $segment, $title, Am_Newsletter_Plugin_Mailchimp $plugin)
    {
        $this->segment = $segment;
        parent::__construct($list_id, $title, $plugin);
    }

    function getTitle()
    {
        return "MailChimp: Add Segment: " . Am_Html::escape($this->title);
    }

    function getId()
    {
        return 'mailchimp-tag-add-' . $this->list_id . '__' . $this->segment;
    }

    function run(User $user)
    {
        if (!$this->plugin->addUserViaApi($user, $this->list_id))
            return false;

        $this->plugin->getApi()->sendRequest(
            "lists/{$this->list_id}/segments/{$this->segment}/members",
            ['email_address' => $user->email],
            Am_HttpRequest::METHOD_POST
        );
    }
}

class Am_FlexibleAction_MailchimpSegmentDelete extends Am_FlexibleAction_Mailchimp
{
    protected string $segment;

    function __construct($list_id, $segment, $title, Am_Newsletter_Plugin_Mailchimp $plugin)
    {
        $this->segment = $segment;
        parent::__construct($list_id, $title, $plugin);
    }

    function getTitle()
    {
        return "MailChimp: Delete Segment: " . Am_Html::escape($this->title);
    }

    function getId()
    {
        return 'mailchimp-tag-delete-' . $this->list_id . '__' . $this->segment;
    }

    function run(User $user)
    {
        $this->plugin->getApi()->sendRequest(
            "lists/{$this->list_id}/segments/{$this->segment}/members/" . md5(strtolower($user->email)),
            null,
            Am_HttpRequest::METHOD_DELETE
        );
    }
}

endif;