<?php

declare(strict_types=1);

require_once __DIR__ . '/wp_shim.php';
require_once dirname(__DIR__, 2) . '/feca_mailshots_plugin/src/autoload.php';

use FecaMailshots\Infrastructure\Env;
use FecaMailshots\Plugin;
use FecaMailshots\WordPress\FixtureWordPressFacade;

Env::load(dirname(__DIR__, 2) . '/credentials/.env');

$tunnelPort = getenv('MYSQL_TUNNEL_LOCAL_PORT');
$host = '127.0.0.1';
$port = is_string($tunnelPort) && $tunnelPort !== '' ? $tunnelPort : (string) Env::require('REMOTE_MYSQL_PORT');

$dbConfig = [
    'MYSQL_HOST' => $host,
    'MYSQL_PORT' => $port,
    'MYSQL_USER' => Env::require('REMOTE_MYSQL_USER'),
    'MYSQL_PASSWORD' => Env::require('REMOTE_MYSQL_PASSWORD'),
    'MAILSHOTS_REMOTE_MYSQL_DB' => Env::require('MAILSHOTS_REMOTE_MYSQL_DB'),
    'MEMBERS_REMOTE_MYSQL_DB' => Env::require('MEMBERS_REMOTE_MYSQL_DB'),
    'FEN_REMOTE_MYSQL_DB' => Env::require('FEN_REMOTE_MYSQL_DB'),
];

$wp = new FixtureWordPressFacade();
$container = Plugin::buildContainer($dbConfig, $wp);
$container->get(FecaMailshots\Admin\DataSourcesAdminPage::class)->register();
$container->get(FecaMailshots\Admin\MailshotsAdminPage::class)->register();
$container->get(FecaMailshots\Admin\AttachmentsAdminPage::class)->register();
$container->get(FecaMailshots\Admin\PdfAssetsAdminPage::class)->register();
$container->get(FecaMailshots\Admin\SetupAdminPage::class)->register();
$container->get(FecaMailshots\Admin\ProfileAdminPage::class)->register();
$container->get(FecaMailshots\Admin\MailshotTestAdminPage::class)->register();
$container->get(FecaMailshots\Admin\RunMailshotAdminPage::class)->register();
$container->get(FecaMailshots\Admin\ReviewRecipientsAdminPage::class)->register();
$container->get(FecaMailshots\Admin\DownloadPdfAdminPage::class)->register();

return ['container' => $container, 'wp' => $wp];
