<?php
namespace Components\Newsletter;

class PageConfig
{
    public const MODE_NEWSLETTER = 'newsletter';
    public const MODE_AUTOMATION = 'automation';

    public string $mode;
    public string $title;
    public string $subtitle;
    public array $allowed_types;
    public bool $show_completed;
    public string $new_button_label;
    public string $creation_modal_title;
    public string $empty_state_text;
    public string $empty_state_title;
    public string $empty_state_icon;
    public string $empty_state_smart_active_title;
    public string $empty_state_smart_active_text;
    public string $empty_state_smart_scheduled_title;
    public string $empty_state_smart_scheduled_text;
    public string $default_smart_group;

    public function __construct(string $mode)
    {
        $this->mode = $mode;
        if ($mode === self::MODE_AUTOMATION) {
            $this->title = \mh_lng('MAILBEEZ_APP_LABEL_AUTOMATIONS', 'Automations');
            $this->subtitle = \mh_lng('MAILBEEZ_AUTOMATION_SUBTITLE', 'Ongoing trigger-based flows');
            $this->allowed_types = ['automation'];
            $this->show_completed = false;
            $this->new_button_label = \mh_lng('UI_NEW_AUTOMATION_FLOW', 'New Flow');
            $this->creation_modal_title = \mh_lng('UI_NEW_AUTOMATION_FLOW', 'New Automation Item');
            $this->empty_state_text = \mh_lng('UI_AUTOMATION_EMPTY_TEXT', 'Ongoing flows that stay active until paused/stopped.');
            $this->empty_state_title = \mh_lng('UI_AUTOMATION_EMPTY_TITLE', 'No automation flows yet');
            $this->empty_state_icon = \Components\Base::SVG_TRIGGER;
            $this->empty_state_smart_active_title = \mh_lng('UI_AUTOMATION_EMPTY_SMART_ACTIVE_TITLE', 'No active automations');
            $this->empty_state_smart_active_text = \mh_lng('UI_AUTOMATION_EMPTY_SMART_ACTIVE_TEXT', 'There are currently no active automations in this view.');
            $this->empty_state_smart_scheduled_title = \mh_lng('UI_AUTOMATION_EMPTY_SMART_SCHEDULED_TITLE', 'No scheduled automations');
            $this->empty_state_smart_scheduled_text = \mh_lng('UI_AUTOMATION_EMPTY_SMART_SCHEDULED_TEXT', 'There are currently no scheduled automations in this view.');
            $this->default_smart_group = 'smart-active';
        } else {
            $this->mode = self::MODE_NEWSLETTER;
            $this->title = \mh_lng('MAILBEEZ_APP_LABEL_NEWSLETTER', 'Newsletter');
            $this->subtitle = \mh_lng('MAILBEEZ_APP_DESC_NEWSLETTER', 'One-time or planned campaigns');
            $this->allowed_types = ['bulk_mail', 'scheduled_window'];
            $this->show_completed = true;
            $this->new_button_label = \mh_lng('UI_NEW_NEWSLETTER', 'New Newsletter');
            $this->creation_modal_title = \mh_lng('MAILBEEZ_NEWSLETTER_ITEM_MODAL_NEW_TITLE', 'New Newsletter Item');
            $this->empty_state_text = \mh_lng('UI_NEWSLETTER_EMPTY_TEXT', 'One-time and scheduled campaigns.');
            $this->empty_state_title = \mh_lng('UI_NEWSLETTER_EMPTY_TITLE', 'No newsletter flows yet');
            $this->empty_state_icon = \Components\Base::SVG_MAIL;
            $this->empty_state_smart_active_title = \mh_lng('UI_NEWSLETTER_EMPTY_SMART_ACTIVE_TITLE', 'No active newsletters');
            $this->empty_state_smart_active_text = \mh_lng('UI_NEWSLETTER_EMPTY_SMART_ACTIVE_TEXT', 'There are currently no active newsletters in this view.');
            $this->empty_state_smart_scheduled_title = \mh_lng('UI_NEWSLETTER_EMPTY_SMART_SCHEDULED_TITLE', 'No scheduled newsletters');
            $this->empty_state_smart_scheduled_text = \mh_lng('UI_NEWSLETTER_EMPTY_SMART_SCHEDULED_TEXT', 'There are currently no scheduled newsletters in this view.');
            $this->default_smart_group = 'smart-active';
        }
    }

    public static function fromRequest(): self
    {
        $mode = \mh_get('page_mode', '', 'string');
        if ($mode === '') {
            $mode = \mh_post('page_mode', self::MODE_NEWSLETTER, 'string');
        }
        return new self($mode);
    }

    public function getBaseUrl(string $route, array $params = []): string
    {
        $params['page_mode'] = $this->mode;
        return \Components\Router::url($route, $params);
    }

    /**
     * Get a unique ID for a DOM element based on the current page mode.
     */
    public function getTargetId(string $id): string
    {
        return $this->mode . '-' . $id;
    }

    /**
     * Get a unique event name based on the current page mode.
     */
    public function getEventName(string $event): string
    {
        return $this->mode . ':' . $event;
    }

    public function getPageMode(): string
    {
        return $this->mode;
    }

    /**
     * Centralized logic to map internal schedule types to a BeezUI page mode.
     */
    public static function resolveModeFromScheduleType(string $scheduleType, bool $isFollowup = false): string
    {
        if ($isFollowup || $scheduleType === 'automation') {
            return self::MODE_AUTOMATION;
        }
        return self::MODE_NEWSLETTER;
    }

    public function getSmartFilterLabel(string $smartType): string
    {
        $smartType = strtolower(trim($smartType));
        $map = [
            'favorite' => (string)\mh_lng('UI_NEWSLETTER_COLLECTION_SMART_FAVORITE', 'Favorite'),
            'recent' => (string)\mh_lng('UI_NEWSLETTER_COLLECTION_SMART_RECENTLY_EDITED', 'Recently edited'),
            'active' => (string)\mh_lng('UI_NEWSLETTER_COLLECTION_SMART_ACTIVE', 'Active'),
            'scheduled' => (string)\mh_lng('UI_NEWSLETTER_COLLECTION_SMART_SCHEDULED', 'Scheduled'),
            'draft' => (string)\mh_lng('UI_NEWSLETTER_COLLECTION_SMART_DRAFT', 'Draft'),
            'paused' => (string)\mh_lng('UI_NEWSLETTER_COLLECTION_SMART_PAUSED', 'Paused'),
            'completed' => (string)\mh_lng('UI_NEWSLETTER_COLLECTION_SMART_COMPLETED', 'Completed'),
        ];

        if (isset($map[$smartType])) {
            return $map[$smartType];
        }

        if ($smartType === '') {
            return (string)\mh_lng('UI_COLLECTIONS', 'Collections');
        }

        return ucfirst($smartType);
    }

    /**
     * @return array{title:string,text:string}
     */
    public function getSmartEmptyState(string $smartType): array
    {
        $smartLabel = $this->getSmartFilterLabel($smartType);

        if ($this->mode === self::MODE_AUTOMATION) {
            return [
                'title' => sprintf((string)\mh_lng('UI_AUTOMATION_EMPTY_SMART_GENERIC_TITLE', 'No automations in “%s”'), $smartLabel),
                'text' => sprintf((string)\mh_lng('UI_AUTOMATION_EMPTY_SMART_GENERIC_TEXT', 'There are currently no automations matching the “%s” smart filter.'), $smartLabel),
            ];
        }

        return [
            'title' => sprintf((string)\mh_lng('UI_NEWSLETTER_EMPTY_SMART_GENERIC_TITLE', 'No newsletters in “%s”'), $smartLabel),
            'text' => sprintf((string)\mh_lng('UI_NEWSLETTER_EMPTY_SMART_GENERIC_TEXT', 'There are currently no newsletters matching the “%s” smart filter.'), $smartLabel),
        ];
    }

    public function getSmartFilterExplainer(string $smartType): string
    {
        $smartType = strtolower(trim($smartType));

        if ($this->mode === self::MODE_AUTOMATION) {
            $map = [
                'favorite' => 'UI_AUTOMATION_SMART_FILTER_EXPLAINER_FAVORITE',
                'recent' => 'UI_AUTOMATION_SMART_FILTER_EXPLAINER_RECENT',
                'active' => 'UI_AUTOMATION_SMART_FILTER_EXPLAINER_ACTIVE',
                'scheduled' => 'UI_AUTOMATION_SMART_FILTER_EXPLAINER_SCHEDULED',
                'draft' => 'UI_AUTOMATION_SMART_FILTER_EXPLAINER_DRAFT',
                'paused' => 'UI_AUTOMATION_SMART_FILTER_EXPLAINER_PAUSED',
                'completed' => 'UI_AUTOMATION_SMART_FILTER_EXPLAINER_COMPLETED',
            ];

            if (isset($map[$smartType])) {
                return (string)\mh_lng($map[$smartType], 'This smart view helps you focus on the automations that matter right now.');
            }

            $smartLabel = $this->getSmartFilterLabel($smartType);
            return sprintf((string)\mh_lng('UI_AUTOMATION_SMART_FILTER_EXPLAINER', 'Filter <strong>%s</strong> shows matching automations so you can focus faster.'), $smartLabel);
        }

        $map = [
            'favorite' => 'UI_NEWSLETTER_SMART_FILTER_EXPLAINER_FAVORITE',
            'recent' => 'UI_NEWSLETTER_SMART_FILTER_EXPLAINER_RECENT',
            'active' => 'UI_NEWSLETTER_SMART_FILTER_EXPLAINER_ACTIVE',
            'scheduled' => 'UI_NEWSLETTER_SMART_FILTER_EXPLAINER_SCHEDULED',
            'draft' => 'UI_NEWSLETTER_SMART_FILTER_EXPLAINER_DRAFT',
            'paused' => 'UI_NEWSLETTER_SMART_FILTER_EXPLAINER_PAUSED',
            'completed' => 'UI_NEWSLETTER_SMART_FILTER_EXPLAINER_COMPLETED',
        ];

        if (isset($map[$smartType])) {
            return (string)\mh_lng($map[$smartType], 'This smart view helps you focus on the newsletters that matter right now.');
        }

        $smartLabel = $this->getSmartFilterLabel($smartType);
        return sprintf((string)\mh_lng('UI_NEWSLETTER_SMART_FILTER_EXPLAINER', 'Filter <strong>%s</strong> shows matching newsletters so you can focus faster.'), $smartLabel);
    }
}
