<?php
declare(strict_types=1);

namespace Autogedal\BlackFriday\Block\Adminhtml;

use Autogedal\BlackFriday\Model\SubscriberStorage;
use Magento\Backend\Block\Template;
use Magento\Backend\Block\Template\Context;
use Magento\Framework\App\ResourceConnection;

class TestEmail extends Template
{
    public function __construct(Context $context, private readonly ResourceConnection $resource, array $data = [])
    {
        parent::__construct($context, $data);
    }

    /** @return array<int, array{email: string, favorites_count: int}> subscribers with favourites, newest first */
    public function getSubscribers(): array
    {
        $connection = $this->resource->getConnection();
        return $connection->fetchAll($connection->select()
            ->from($this->resource->getTableName(SubscriberStorage::TABLE), ['email', 'favorites_count'])
            ->where('favorites_count > 0')
            ->order('updated_at DESC')
            ->limit(500));
    }

    public function getSendUrl(): string
    {
        return $this->getUrl('*/*/sendTest');
    }

    public function getBackUrl(): string
    {
        return $this->getUrl('*/*/index');
    }
}
