<?php
use BLR\Backline\Core\Access;
use BLR\Backline\Core\SettingsPage;
use BLR\Backline\Modules\Dashboard\Pages as DashboardPages;
use BLR\Backline\Modules\Events\Module as EventsModule;
use BLR\Backline\Modules\Finances\Module as FinancesModule;
use BLR\Backline\Modules\Notifications\Module as NotificationsModule;
use BLR\Backline\Modules\Outreach\Module as OutreachModule;
use BLR\Backline\Modules\Tasks\Module as TasksModule;

if (!defined('ABSPATH')) {
    exit;
}

$__blr_ba_data = isset($ctx['dashboard']) && is_array($ctx['dashboard']) ? $ctx['dashboard'] : DashboardPages::data($ctx);
foreach ($__blr_ba_data as $__blr_ba_key => $__blr_ba_value) {
    ${$__blr_ba_key} = $__blr_ba_value;
}
unset($__blr_ba_data, $__blr_ba_key, $__blr_ba_value);
?>
<div class="container-fluid p-0 blr-dashboard-simple">

    <div class="dashboard-intro d-flex flex-column flex-lg-row align-items-lg-center justify-content-between gap-3">
        <div>
            <h4 class="mb-1"><?php echo esc_html__('Dashboard', 'blr-backline'); ?></h4>
        </div>
        <?php if (!empty($quick_actions)) : ?>
            <div class="d-flex flex-wrap gap-2">
                <?php foreach ($quick_actions as $action_button) : ?>
                    <a class="<?php echo esc_attr($action_button['class']); ?>" href="<?php echo esc_url($action_button['url']); ?>"><?php echo esc_html($action_button['label']); ?></a>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </div>

    <div class="card summary-shell mb-4">
                <div class="card-header">
                    <h5 class="card-title mb-1"><?php echo esc_html__('At a glance', 'blr-backline'); ?></h5>
                </div>
        <div class="card-body">
                        <?php if (empty($active_modules)) : ?>
                            <div class="section-muted">
                                <?php
                                echo esc_html(
                                    $is_admin_user
                                        ? __('Enable a module to start using Backline from this dashboard.', 'blr-backline')
                                        : __('No active tools are currently available to you.', 'blr-backline')
                                );
                                ?>
                            </div>
                        <?php else : ?>
                            <div class="row g-3">
                                <?php foreach ( $active_modules as $module_card ) : ?>
                                    <div class="col">
                                        <a class="d-block h-100 text-decoration-none" href="<?php echo esc_url( $module_card['url'] ); ?>">
                                            <div class="h-100 p-3 border rounded">
                                                <div class="tool-name"><?php echo esc_html( $module_card['title'] ); ?></div>
                                                <div class="tool-meta"><?php echo wp_kses( $module_card['meta'], [ 'br' => [], ] );?></div>
                                            </div>
                                        </a>
                                    </div>
                                <?php endforeach; ?>
                            </div>
                        <?php endif; ?>

        </div>
    </div>



    
    <?php do_action('blr_backline_dashboard_after_summary_cards', $dashboard_data, $ctx); ?>
    <?php do_action('blr_backline_dashboard_after_primary_row', $dashboard_data, $ctx); ?>




    <div class="row g-3">

        <?php if ($upcoming) : ?>
                <div class="col-12 col-lg-4">
                    <div class="card dashboard-main-card">
                        <div class="card-header">
                            <h5 class="card-title mb-1"><?php echo esc_html__('Upcoming Events', 'blr-backline'); ?></h5>
                            <div class="headline-actions">
                                <a class="small text-decoration-none" href="<?php echo esc_url($base . 'events/'); ?>"><?php echo esc_html__('View all', 'blr-backline'); ?></a>
                            </div>
                        </div>
                        <div class="card-body">
                            <?php if (!$events_available && !$tasks_available && !$outreach_available) : ?>
                                <div class="section-muted"><?php echo esc_html__('No timeline modules are active right now.', 'blr-backline'); ?></div>
                            <?php endif; ?>
                            <?php if ($events_available) : ?>
                                <div class="section-block">
                                    <div class="section-block-header">
                                        <a class="small text-decoration-none" href="<?php echo esc_url($base . 'events/'); ?>"><?php echo esc_html__('View all', 'blr-backline'); ?></a>
                                    </div>
                                    <?php if (!$upcoming) : ?>
                                        <div class="section-muted"><?php echo esc_html__('No upcoming events.', 'blr-backline'); ?></div>
                                    <?php else : ?>
                                        <div class="list-group list-group-flush">
                                            <?php foreach ($upcoming as $event_post) : ?>
                                                <?php
                                                $event_id     = (int) $event_post->ID;
                                                $event_start  = (string) get_post_meta($event_id, 'blr_event_start', true);
                                                $event_status = (string) get_post_meta($event_id, 'blr_event_status', true);
                                                ?>
                                                <a class="list-group-item list-group-item-action item-link p-2" href="<?php echo esc_url($base . 'events/view/' . $event_id . '/'); ?>">
                                                    <div class="list-title"><?php echo esc_html(get_the_title($event_post)); ?></div>
                                                    <div class="list-meta mb-1"><?php echo esc_html($format_datetime($event_start)); ?></div>
                                                    <div class="inline-meta d-flex flex-wrap align-items-center justify-content-between gap-2">
                                                        <span><?php echo esc_html($get_event_location($event_id)); ?></span>
                                                        <span><?php echo esc_html(EventsModule::status_label($event_status)); ?></span>
                                                    </div>
                                                </a>
                                            <?php endforeach; ?>
                                        </div>
                                    <?php endif; ?>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
        <?php endif; ?>


        <?php if ($due_tasks) : ?>
                <div class="col-12 col-lg-4">
                    <div class="card dashboard-main-card">
                        <div class="card-header">
                            <h5 class="card-title mb-1"><?php echo esc_html__('Tasks Due', 'blr-backline'); ?></h5>
                        </div>
                        <div class="card-body">
                            <?php if ($tasks_available) : ?>
                                <div class="section-block">
                                    <div class="section-block-header">
                                        <a class="small text-decoration-none" href="<?php echo esc_url($base . 'tasks/'); ?>"><?php echo esc_html__('View all', 'blr-backline'); ?></a>
                                    </div>
                                    <?php if (!$due_tasks) : ?>
                                        <div class="section-muted"><?php echo esc_html__('No dated tasks need attention right now.', 'blr-backline'); ?></div>
                                    <?php else : ?>
                                        <div class="list-group list-group-flush">
                                            <?php foreach ($due_tasks as $task_post) : ?>
                                                <?php
                                                $task_id          = (int) $task_post->ID;
                                                $task_status      = (string) get_post_meta($task_id, 'blr_task_status', true);
                                                $task_priority    = (string) get_post_meta($task_id, 'blr_task_priority', true);
                                                $task_due_date    = (string) get_post_meta($task_id, 'blr_task_due_date', true);
                                                $task_assignee_id = (int) get_post_meta($task_id, 'blr_task_assignee_user_id', true);
                                                $task_assignee    = $get_user_label($task_assignee_id);
                                                ?>
                                                <a class="list-group-item list-group-item-action item-link p-2" href="<?php echo esc_url($base . 'tasks/view/' . $task_id . '/'); ?>">
                                                    <div class="list-title"><?php echo esc_html(get_the_title($task_post)); ?></div>
                                                    <div class="list-meta mb-1"><?php echo esc_html__('Assigned to', 'blr-backline'); ?> <?php echo esc_html($task_assignee); ?></div>
                                                    <div class="inline-meta d-flex flex-wrap align-items-center justify-content-between gap-2">
                                                        <span class="<?php echo esc_attr(TasksModule::due_state_class($task_due_date, $task_status)); ?>">
                                                            <?php echo esc_html__('Due', 'blr-backline'); ?> <?php echo esc_html($format_date($task_due_date)); ?>
                                                        </span>
                                                        <span><?php echo esc_html($task_priority !== '' ? $task_priority : $task_status); ?></span>
                                                    </div>
                                                </a>
                                            <?php endforeach; ?>
                                        </div>
                                    <?php endif; ?>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
        <?php endif; ?>


        <?php if ($latest_outreach) : ?>
                <div class="col-12 col-lg-4">
                    <div class="card dashboard-main-card">
                        <div class="card-header">
                            <h5 class="card-title mb-1"><?php echo esc_html__('Latest Outreach', 'blr-backline'); ?></h5>
                        </div>
                        <div class="card-body">
                            <?php if ($outreach_available) : ?>
                                <div class="section-block">
                                    <div class="section-block-header">
                                        <a class="small text-decoration-none" href="<?php echo esc_url($base . 'outreach/'); ?>"><?php echo esc_html__('View all', 'blr-backline'); ?></a>
                                    </div>
                                    <?php if (!$latest_outreach) : ?>
                                        <div class="section-muted"><?php echo esc_html__('No outreach campaigns yet.', 'blr-backline'); ?></div>
                                    <?php else : ?>
                                        <div class="list-group list-group-flush">
                                            <?php foreach ($latest_outreach as $campaign_post) : ?>
                                                <?php
                                                $campaign_id        = (int) $campaign_post->ID;
                                                $campaign_from      = trim((string) get_post_meta($campaign_id, 'blr_outreach_from_name', true));
                                                $campaign_last_sent = (string) get_post_meta($campaign_id, 'blr_outreach_last_sent_at', true);
                                                $campaign_author    = $get_user_label((int) $campaign_post->post_author);
                                                $campaign_stats     = OutreachModule::get_campaign_stats($campaign_id);
                                                $campaign_status    = OutreachModule::resolved_campaign_status($campaign_id, $campaign_stats);
                                                $campaign_sender    = $campaign_from !== '' ? $campaign_from : $campaign_author;
                                                ?>
                                                <a class="list-group-item list-group-item-action item-link p-2" href="<?php echo esc_url($base . 'outreach/view/' . $campaign_id . '/'); ?>">
                                                    <div class="list-title"><?php echo esc_html(get_the_title($campaign_post)); ?></div>
                                                    <div class="list-meta mb-1"><?php echo esc_html__('Sent by', 'blr-backline'); ?> <?php echo esc_html($campaign_sender); ?></div>
                                                    <div class="inline-meta d-flex flex-wrap align-items-center justify-content-between gap-2">
                                                        <span><?php echo esc_html(OutreachModule::status_label($campaign_status)); ?></span>
                                                        <span>
                                                            <?php
                                                            echo esc_html(
                                                                $campaign_last_sent !== ''
                                                                    ? $format_datetime($campaign_last_sent)
                                                                    : __('Not sent yet', 'blr-backline')
                                                            );
                                                            ?>
                                                        </span>
                                                    </div>
                                                </a>
                                            <?php endforeach; ?>
                                        </div>
                                    <?php endif; ?>
                                </div>
                            <?php endif; ?>

                        </div>
                    </div>
                </div>
        <?php endif; ?>


    </div>

    <?php do_action('blr_backline_dashboard_bottom', $dashboard_data, $ctx); ?>
</div>
