<?php

use ChurchCRM\dto\SystemURLs;

require SystemURLs::getDocumentRoot() . '/Include/Header.php';

?>
<!-- Breadcrumb Navigation with Actions -->
<div class="row mb-3">
    <div class="col-12 d-flex align-items-center">
        <nav aria-label="breadcrumb" class="flex-grow-1">
            <ol class="breadcrumb mb-0 bg-light">
                <li class="breadcrumb-item"><a href="<?= SystemURLs::getRootPath() ?>/v2/dashboard"><i class="fa-solid fa-home"></i></a></li>
                <li class="breadcrumb-item"><a href="<?= SystemURLs::getRootPath() ?>/plugins/management"><?= gettext('Plugins') ?></a></li>
                <li class="breadcrumb-item active" aria-current="page">Mailchimp</li>
            </ol>
        </nav>
        <div class="btn-group btn-group-sm ms-2">
            <a href="https://login.mailchimp.com/" target="_blank" class="btn btn-outline-warning" title="<?= gettext('Open MailChimp') ?>">
                <i class="fa-brands fa-mailchimp fa-fw"></i><?= gettext('Open MailChimp') ?>
            </a>
            <a href="<?= SystemURLs::getRootPath() ?>/plugins/management/mailchimp" class="btn btn-outline-secondary" title="<?= gettext('Plugin Settings') ?>">
                <i class="fa-solid fa-cog fa-fw"></i>
            </a>
        </div>
    </div>
</div>

<?php if ($isMailChimpActive && !empty($mailChimpLists)) : ?>
    <?php
    // Calculate totals
    $totalMembers = 0;
    $totalCampaigns = 0;
    $latestCampaignDate = null;
    foreach ($mailChimpLists as $list) {
        $totalMembers += $list['stats']['member_count'] ?? 0;
        $totalCampaigns += $list['stats']['campaign_count'] ?? 0;
        $lastSent = $list['stats']['campaign_last_sent'] ?? null;
        if ($lastSent && ($latestCampaignDate === null || $lastSent > $latestCampaignDate)) {
            $latestCampaignDate = $lastSent;
        }
    }
    ?>

    <!-- Account Info Banner -->
    <?php if (!empty($accountInfo['account_name'])) : ?>
    <div class="alert alert-light border mb-3">
        <div class="d-flex align-items-center">
            <i class="fa-brands fa-mailchimp fa-2x text-warning me-3"></i>
            <div class="flex-grow-1">
                <strong><?= htmlspecialchars($accountInfo['account_name'], ENT_QUOTES, 'UTF-8') ?></strong>
                <?php if (!empty($accountInfo['email'])) : ?>
                    <span class="text-body-secondary ms-2">(<?= htmlspecialchars($accountInfo['email'], ENT_QUOTES, 'UTF-8') ?>)</span>
                <?php endif; ?>
            </div>
            <?php if ($latestCampaignDate) : ?>
            <div class="text-end text-body-secondary small">
                <i class="fa-solid fa-paper-plane me-1"></i><?= gettext('Last campaign') ?>: 
                <strong><?= date('M j, Y', strtotime($latestCampaignDate)) ?></strong>
            </div>
            <?php endif; ?>
        </div>
    </div>
    <?php endif; ?>

    <!-- Stats Overview -->
    <div class="row">
        <div class="col-lg-3 col-6">
            <div class="card card-sm">
                <div class="card-body">
                    <div class="row align-items-center">
                        <div class="col-auto">
                            <span class="bg-info text-white avatar rounded-circle">
                                <i class="fa-solid fa-list icon"></i>
                            </span>
                        </div>
                        <div class="col">
                            <div class="h3 m-0"><?= count($mailChimpLists) ?></div>
                            <div class="text-body-secondary"><?= gettext('Audiences') ?></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-lg-3 col-6">
            <div class="card card-sm">
                <div class="card-body">
                    <div class="row align-items-center">
                        <div class="col-auto">
                            <span class="bg-success text-white avatar rounded-circle">
                                <i class="fa-solid fa-users icon"></i>
                            </span>
                        </div>
                        <div class="col">
                            <div class="h3 m-0"><?= number_format($totalMembers) ?></div>
                            <div class="text-body-secondary"><?= gettext('Total Subscribers') ?></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-lg-3 col-6">
            <div class="card card-sm">
                <div class="card-body">
                    <div class="row align-items-center">
                        <div class="col-auto">
                            <span class="bg-primary text-white avatar rounded-circle">
                                <i class="fa-solid fa-paper-plane icon"></i>
                            </span>
                        </div>
                        <div class="col">
                            <div class="h3 m-0"><?= number_format($totalCampaigns) ?></div>
                            <div class="text-body-secondary"><?= gettext('Total Campaigns') ?></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-lg-3 col-6">
            <div class="card card-sm">
                <div class="card-body">
                    <div class="row align-items-center">
                        <div class="col-auto">
                            <span class="bg-success text-white avatar rounded-circle">
                                <i class="fa-solid fa-plug icon"></i>
                            </span>
                        </div>
                        <div class="col">
                            <div class="h3 m-0"><i class="fa-solid fa-check"></i></div>
                            <div class="text-body-secondary"><?= gettext('Connected') ?></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Audiences / Lists -->
    <div class="row">
        <?php foreach ($mailChimpLists as $list) : ?>
            <div class="col-lg-6 col-md-12 mb-4">
                <div class="card card h-100">
                    <div class="card-header d-flex align-items-center">
                        <h3 class="card-title">
                            <i class="fa-solid fa-list me-2"></i><?= htmlspecialchars($list['name'], ENT_QUOTES, 'UTF-8') ?>
                        </h3>
                        <div class="card-tools ms-auto">
                            <span class="badge bg-info"><?= number_format($list['stats']['member_count'] ?? 0) ?> <?= gettext('subscribers') ?></span>
                        </div>
                    </div>
                    <div class="card-body p-0">
                        <table class="table table-sm mb-0">
                            <tbody>
                                <tr>
                                    <td><i class="fa-solid fa-users text-info me-2"></i><?= gettext('Subscribers') ?></td>
                                    <td class="text-end"><strong><?= number_format($list['stats']['member_count'] ?? 0) ?></strong></td>
                                </tr>
                                <tr>
                                    <td><i class="fa-solid fa-paper-plane text-primary me-2"></i><?= gettext('Campaigns Sent') ?></td>
                                    <td class="text-end"><strong><?= number_format($list['stats']['campaign_count'] ?? 0) ?></strong></td>
                                </tr>
                                <?php if (!empty($list['stats']['campaign_last_sent'])) : ?>
                                <tr>
                                    <td><i class="fa-solid fa-calendar-check text-success me-2"></i><?= gettext('Last Campaign') ?></td>
                                    <td class="text-end"><?= date('M j, Y', strtotime($list['stats']['campaign_last_sent'])) ?></td>
                                </tr>
                                <?php endif; ?>
                                <?php if (!empty($list['stats']['last_sub_date'])) : ?>
                                <tr>
                                    <td><i class="fa-solid fa-user-plus text-success me-2"></i><?= gettext('Last Subscriber') ?></td>
                                    <td class="text-end"><?= date('M j, Y', strtotime($list['stats']['last_sub_date'])) ?></td>
                                </tr>
                                <?php endif; ?>
                                <tr>
                                    <td><i class="fa-solid fa-user-minus text-warning me-2"></i><?= gettext('Unsubscribed') ?></td>
                                    <td class="text-end"><?= number_format($list['stats']['unsubscribe_count'] ?? 0) ?></td>
                                </tr>
                                <tr>
                                    <td><i class="fa-solid fa-broom text-secondary me-2"></i><?= gettext('Cleaned') ?></td>
                                    <td class="text-end"><?= number_format($list['stats']['cleaned_count'] ?? 0) ?></td>
                                </tr>
                                <tr>
                                    <td><i class="fa-solid fa-envelope-open text-success me-2"></i><?= gettext('Avg Open Rate') ?></td>
                                    <td class="text-end"><?= number_format(($list['stats']['open_rate'] ?? 0) * 100, 1) ?>%</td>
                                </tr>
                                <tr>
                                    <td><i class="fa-solid fa-mouse-pointer text-info me-2"></i><?= gettext('Avg Click Rate') ?></td>
                                    <td class="text-end"><?= number_format(($list['stats']['click_rate'] ?? 0) * 100, 1) ?>%</td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                    <div class="card-footer bg-light">
                        <div class="btn-group btn-group-sm d-flex" role="group">
                            <a href="<?= SystemURLs::getRootPath() ?>/plugins/mailchimp/list/<?= $list['id'] ?>/unsubscribed" class="btn btn-outline-primary flex-fill">
                                <i class="fa-solid fa-user-plus me-1"></i><?= gettext('Not Subscribed') ?>
                            </a>
                            <a href="<?= SystemURLs::getRootPath() ?>/plugins/mailchimp/list/<?= $list['id'] ?>/missing" class="btn btn-outline-warning flex-fill">
                                <i class="fa-solid fa-user-slash me-1"></i><?= gettext('Not in CRM') ?>
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        <?php endforeach; ?>
    </div>

<?php elseif ($isMailChimpActive) : ?>
    <!-- No Lists Found -->
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-body text-center py-5">
                    <i class="fa-brands fa-mailchimp fa-4x text-body-secondary mb-3"></i>
                    <h4 class="text-body-secondary"><?= gettext('No Audiences Found') ?></h4>
                    <p class="text-body-secondary"><?= gettext('Your MailChimp account does not have any audiences (lists) configured.') ?></p>
                    <a href="https://mailchimp.com/help/create-audience/" target="_blank" class="btn btn-primary">
                        <i class="fa-solid fa-arrow-up-right-from-square me-1"></i><?= gettext('Create an Audience in MailChimp') ?>
                    </a>
                </div>
            </div>
        </div>
    </div>
<?php else : ?>
    <!-- Not Configured -->
    <div class="row">
        <div class="col-12">
            <div class="callout callout-warning">
                <h5><i class="fa-solid fa-triangle-exclamation me-2"></i><?= gettext('MailChimp is not configured') ?></h5>
                <p><?= gettext('Please configure your MailChimp API key to use this integration.') ?></p>
                <a href="<?= SystemURLs::getRootPath() ?>/plugins/management/mailchimp" class="btn btn-warning">
                    <i class="fa-solid fa-cog me-1"></i><?= gettext('Configure MailChimp') ?>
                </a>
            </div>
        </div>
    </div>
<?php endif; ?>

<?php
require SystemURLs::getDocumentRoot() . '/Include/Footer.php';
