<?php
/**
 * 247ChatMate — Agency Hub partial.
 * Checkpoint: AGENCYHUBRESPONSIBILITYSPLIT1
 *
 * Display-only split from admin/views/partials/agency-hub.php.
 * Expects parent Agency Hub variables to be prepared before include.
 */

defined('ABSPATH') || exit;
?>
    <section id="matewa-agency-bundle-licences" class="matewa-agency-bundle-panel" aria-label="Agency bundle licences">
        <div class="matewa-agency-client-board-header">
            <div>
                <span class="matewa-agency-hub-kicker">Licence seats</span>
                <h4>Agency bundle licences</h4>
                <p>Buy client-seat bundles through LemonSqueezy, then paste the agency bundle licence key here. Saved keys stay masked in the dashboard.</p>
            </div>
            <div class="matewa-agency-client-board-actions" aria-label="Agency checkout links">
                <?php foreach ($matewa_agency_bundle_plans as $matewa_plan_key => $matewa_plan) : ?>
                    <?php
                    $matewa_plan_label = isset($matewa_plan['label']) ? sanitize_text_field((string) $matewa_plan['label']) : 'Agency bundle';
                    $matewa_plan_url = isset($matewa_plan['checkout_url']) ? esc_url((string) $matewa_plan['checkout_url']) : '';
                    $matewa_plan_price = isset($matewa_plan['price']) ? sanitize_text_field((string) $matewa_plan['price']) : '';
                    ?>
                    <?php if ('' !== $matewa_plan_url) : ?>
                        <a class="matewa-agency-soft-pill is-link" href="<?php echo esc_url($matewa_plan_url); ?>" target="_blank" rel="noopener noreferrer"><?php echo esc_html($matewa_plan_label); ?><?php echo '' !== $matewa_plan_price ? esc_html(' · ' . $matewa_plan_price) : ''; ?></a>
                    <?php endif; ?>
                <?php endforeach; ?>
            </div>
        </div>

        <?php if ('' !== $matewa_agency_bundle_notice_message) : ?>
            <div class="matewa-agency-bundle-notice is-<?php echo esc_attr($matewa_agency_bundle_notice_type); ?>" role="status" aria-live="polite">
                <?php echo esc_html($matewa_agency_bundle_notice_message); ?>
            </div>
        <?php endif; ?>

        <div class="matewa-agency-bundle-entry" aria-label="Add agency bundle licence">
            <label for="matewa_agency_bundle_license_key">
                <strong>Add agency bundle licence</strong>
                <span>Paste the licence key from LemonSqueezy. It will be hidden after save.</span>
            </label>
            <div class="matewa-agency-bundle-entry-controls">
                <textarea
                    id="matewa_agency_bundle_license_key"
                    name="matewa_agency_bundle_license_key"
                    rows="1"
                    autocomplete="new-password"
                    spellcheck="false"
                    placeholder="Paste agency bundle licence key"
                ></textarea>
                <button type="submit" class="button button-primary" name="matewa_agency_bundle_action" value="add">Activate bundle</button>
            </div>
            <small>Local toggle controls whether this agency account uses the bundle. LemonSqueezy status still decides whether the licence is actually valid.</small>
        </div>

        <div class="matewa-agency-client-table-wrap matewa-agency-bundle-table-wrap">
            <table class="matewa-agency-client-table matewa-agency-bundle-table">
                <thead>
                    <tr>
                        <th scope="col">Bundle</th>
                        <th scope="col">Status</th>
                        <th scope="col">Seats</th>
                        <th scope="col">Expiry / renewal</th>
                        <th scope="col">Local use</th>
                        <th scope="col">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($matewa_agency_bundle_records)) : ?>
                        <tr>
                            <td data-label="Bundle">
                                <strong>No paid bundle added yet</strong>
                                <span>Use Buy client licences, then paste the bundle key here.</span>
                            </td>
                            <td data-label="Status"><span class="matewa-agency-table-pill is-warning">Waiting</span></td>
                            <td data-label="Seats">0</td>
                            <td data-label="Expiry / renewal">—</td>
                            <td data-label="Local use">—</td>
                            <td data-label="Actions">Add bundle first</td>
                        </tr>
                    <?php else : ?>
                        <?php foreach ($matewa_agency_bundle_records as $matewa_bundle) : ?>
                            <?php
                            $matewa_bundle_id = isset($matewa_bundle['bundle_id']) ? sanitize_key((string) $matewa_bundle['bundle_id']) : '';
                            $matewa_bundle_label = isset($matewa_bundle['plan_label']) && '' !== trim((string) $matewa_bundle['plan_label']) ? sanitize_text_field((string) $matewa_bundle['plan_label']) : 'Agency bundle';
                            $matewa_bundle_masked = isset($matewa_bundle['masked_key']) ? sanitize_text_field((string) $matewa_bundle['masked_key']) : 'Saved key hidden';
                            $matewa_bundle_valid = !empty($matewa_bundle['valid']);
                            $matewa_bundle_enabled = !empty($matewa_bundle['local_enabled']);
                            $matewa_bundle_status = isset($matewa_bundle['status']) ? sanitize_key((string) $matewa_bundle['status']) : 'not_checked';
                            $matewa_bundle_status_label = $matewa_bundle_enabled ? ($matewa_bundle_valid ? 'Active' : ucwords(str_replace('_', ' ', $matewa_bundle_status))) : 'Paused locally';
                            $matewa_bundle_status_class = $matewa_bundle_enabled && $matewa_bundle_valid ? 'is-ready' : 'is-warning';
                            $matewa_bundle_seats = isset($matewa_bundle['seats']) ? absint($matewa_bundle['seats']) : 0;
                            $matewa_bundle_used = isset($matewa_bundle['used_seats']) ? absint($matewa_bundle['used_seats']) : 0;
                            $matewa_bundle_available = max(0, $matewa_bundle_seats - $matewa_bundle_used);
                            $matewa_bundle_date_source = !empty($matewa_bundle['renews_at']) ? $matewa_bundle['renews_at'] : ($matewa_bundle['expires_at'] ?? '');
                            $matewa_bundle_date_label = $matewa_agency_format_date($matewa_bundle_date_source);
                            $matewa_bundle_checked = !empty($matewa_bundle['checked_at']) ? wp_date((string) get_option('date_format', 'Y-m-d'), (int) $matewa_bundle['checked_at']) : 'Not checked';
                            ?>
                            <tr>
                                <td data-label="Bundle">
                                    <strong><?php echo esc_html($matewa_bundle_label); ?></strong>
                                    <span><?php echo esc_html($matewa_bundle_masked); ?> · Last checked <?php echo esc_html($matewa_bundle_checked); ?></span>
                                </td>
                                <td data-label="Status"><span class="matewa-agency-table-pill <?php echo esc_attr($matewa_bundle_status_class); ?>"><?php echo esc_html($matewa_bundle_status_label); ?></span></td>
                                <td data-label="Seats"><?php echo esc_html((string) $matewa_bundle_used); ?> / <?php echo esc_html((string) $matewa_bundle_seats); ?> used<br><span><?php echo esc_html((string) $matewa_bundle_available); ?> available</span></td>
                                <td data-label="Expiry / renewal"><?php echo esc_html($matewa_bundle_date_label); ?><br><span><?php echo $matewa_bundle_valid ? esc_html('Set to renew / active unless billing changes') : esc_html('Check billing'); ?></span></td>
                                <td data-label="Local use"><?php echo $matewa_bundle_enabled ? esc_html('On') : esc_html('Paused'); ?></td>
                                <td data-label="Actions">
                                    <?php if ('' !== $matewa_bundle_id) : ?>
                                        <button type="submit" class="button button-small" name="matewa_agency_bundle_action" value="recheck" onclick="document.getElementById('matewa-agency-bundle-id-field').value='<?php echo esc_js($matewa_bundle_id); ?>';">Recheck</button>
                                        <?php if ($matewa_bundle_enabled) : ?>
                                            <button type="submit" class="button button-small" name="matewa_agency_bundle_action" value="toggle_off" onclick="document.getElementById('matewa-agency-bundle-id-field').value='<?php echo esc_js($matewa_bundle_id); ?>';">Pause</button>
                                        <?php else : ?>
                                            <button type="submit" class="button button-small" name="matewa_agency_bundle_action" value="toggle_on" onclick="document.getElementById('matewa-agency-bundle-id-field').value='<?php echo esc_js($matewa_bundle_id); ?>';">Use</button>
                                        <?php endif; ?>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
            <input type="hidden" id="matewa-agency-bundle-id-field" name="matewa_agency_bundle_id" value="">
        </div>
    </section>
