<?php
/**
 * Task Comms Portal – Route Handler
 *
 * Pretty permalink: /task-dashboard/{token}
 * (keeps the old /task-comms/{token} working too)
 */

add_action( 'init', function () {

    // NEW preferred slug
    add_rewrite_rule(
        '^task-dashboard/([^/]+)/?$',
        'index.php?asana_comms_token=$matches[1]',
        'top'
    );

    // OPTIONAL: keep legacy slug working
    add_rewrite_rule(
        '^task-comms/([^/]+)/?$',
        'index.php?asana_comms_token=$matches[1]',
        'top'
    );

    // Register the token query-var (already present, keep it)
    add_rewrite_tag( '%asana_comms_token%', '([^&]+)' );
} );

add_action( 'template_redirect', function () {
    $token = get_query_var( 'asana_comms_token' );
    if ( $token ) {
        include plugin_dir_path( __FILE__ ) . 'pages/task-dashboard.php';
        exit;
    }
} );
