<?php
$unconfirmed = 0;
$in_progress = 0;
$confirmed = 0;
$resolved = 0;
$verified = 0;
$project_bug_count = 0;
if (!empty($all_project)) {
    foreach ($all_project as $v_project) {
        $bugs_info = $this->db->where('project_id', $v_project->project_id)->get('tbl_bug')->result();
        if (!empty($bugs_info)) {
            foreach ($bugs_info as $v_bugs) {
                $project_bug_count++;
                if ($v_bugs->bug_status == 'unconfirmed') $unconfirmed++;
                if ($v_bugs->bug_status == 'in_progress') $in_progress++;
                if ($v_bugs->bug_status == 'confirmed') $confirmed++;
                if ($v_bugs->bug_status == 'resolved') $resolved++;
                if ($v_bugs->bug_status == 'verified') $verified++;
            }
        }
    }
}

$summary_cards = array(
    array(
        'label' => lang('bugs'),
        'value' => $project_bug_count,
        'meta' => 'Project-linked defects in the current report scope',
        'icon' => 'shield-alert',
        'tone' => 'text-amber-600 dark:text-amber-300 bg-amber-500/10',
    ),
    array(
        'label' => lang('unconfirmed'),
        'value' => $unconfirmed,
        'meta' => 'New defects pending validation',
        'icon' => 'alert-circle',
        'tone' => 'text-amber-600 dark:text-amber-300 bg-amber-500/10',
    ),
    array(
        'label' => lang('in_progress'),
        'value' => $in_progress,
        'meta' => 'Defects currently under active work',
        'icon' => 'loader-circle',
        'tone' => 'text-sky-600 dark:text-sky-300 bg-sky-500/10',
    ),
    array(
        'label' => lang('resolved'),
        'value' => $resolved,
        'meta' => 'Fixes implemented and ready for validation',
        'icon' => 'check-check',
        'tone' => 'text-violet-600 dark:text-violet-300 bg-violet-500/10',
    ),
    array(
        'label' => lang('verified'),
        'value' => $verified,
        'meta' => 'Defects confirmed closed by validation',
        'icon' => 'badge-check',
        'tone' => 'text-emerald-600 dark:text-emerald-300 bg-emerald-500/10',
    ),
);

$status_categories = array(
    lang('unconfirmed'),
    lang('in_progress'),
    lang('confirmed'),
    lang('resolved'),
    lang('verified'),
);
$status_series = array(array(
    'name' => lang('bugs'),
    'categories' => $status_categories,
    'data' => array($unconfirmed, $in_progress, $confirmed, $resolved, $verified),
));

$assignment_categories = array();
$assignment_unconfirmed = array();
$assignment_in_progress = array();
$assignment_verified = array();
$assignment_resolved = array();
if (!empty($user_bugs) && !empty($assign_user)) {
    foreach ($user_bugs as $user => $v_bugs_user) {
        if ($user == 'all') {
            continue;
        }
        foreach ($assign_user as $v_user) {
            if ((string)$v_user->user_id !== (string)$user) {
                continue;
            }
            $assignment_categories[] = !empty($v_user->username) ? $v_user->username : fullname($v_user->user_id);
            $assignment_unconfirmed[] = !empty($v_bugs_user['unconfirmed']) ? count((array)$v_bugs_user['unconfirmed']) : 0;
            $assignment_in_progress[] = !empty($v_bugs_user['in_progress']) ? count((array)$v_bugs_user['in_progress']) : 0;
            $assignment_verified[] = !empty($v_bugs_user['verified']) ? count((array)$v_bugs_user['verified']) : 0;
            $assignment_resolved[] = !empty($v_bugs_user['resolved']) ? count((array)$v_bugs_user['resolved']) : 0;
            break;
        }
    }
}
if (empty($assignment_categories)) {
    $assignment_categories = array(lang('no_data_found'));
    $assignment_unconfirmed = array(0);
    $assignment_in_progress = array(0);
    $assignment_verified = array(0);
    $assignment_resolved = array(0);
}
$assignment_series = array(
    array('name' => lang('unconfirmed'), 'categories' => $assignment_categories, 'data' => $assignment_unconfirmed),
    array('name' => lang('in_progress'), 'categories' => $assignment_categories, 'data' => $assignment_in_progress),
    array('name' => lang('verified'), 'categories' => $assignment_categories, 'data' => $assignment_verified),
    array('name' => lang('resolved'), 'categories' => $assignment_categories, 'data' => $assignment_resolved),
);

$trend_categories = array();
$trend_unconfirmed = array();
$trend_in_progress = array();
$trend_resolved = array();
if (!empty($yearly_report)) {
    foreach ($yearly_report as $name => $v_report) {
        $trend_categories[] = date('M', safe_strtotime(date('Y') . '-' . $name . '-01'));
        $month_unconfirmed = 0;
        $month_in_progress = 0;
        $month_resolved = 0;
        if (!empty($v_report)) {
            foreach ($v_report as $s_report) {
                if ($s_report->bug_status == 'unconfirmed') {
                    $month_unconfirmed++;
                }
                if ($s_report->bug_status == 'in_progress') {
                    $month_in_progress++;
                }
                if ($s_report->bug_status == 'resolved') {
                    $month_resolved++;
                }
            }
        }
        $trend_unconfirmed[] = $month_unconfirmed;
        $trend_in_progress[] = $month_in_progress;
        $trend_resolved[] = $month_resolved;
    }
}
$trend_series = array(
    array('name' => lang('unconfirmed'), 'categories' => $trend_categories, 'data' => $trend_unconfirmed),
    array('name' => lang('in_progress'), 'categories' => $trend_categories, 'data' => $trend_in_progress),
    array('name' => lang('resolved'), 'categories' => $trend_categories, 'data' => $trend_resolved),
);
?>
<div class="space-y-6 sales-report-page">
    <div class="grid grid-cols-1 gap-3 md:grid-cols-2 xl:grid-cols-5 sales-report-cardgrid">
        <?php foreach ($summary_cards as $card): ?>
            <div class="rounded-xl border border-border bg-card p-4 shadow-sm sales-report-card">
                <div class="flex items-start justify-between gap-3">
                    <div>
                        <p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= $card['label'] ?></p>
                        <p class="mt-2 text-xl font-bold text-foreground"><?= $card['value'] ?></p>
                        <p class="mt-1 text-xs text-muted-foreground"><?= $card['meta'] ?></p>
                    </div>
                    <span class="inline-flex h-10 w-10 items-center justify-center rounded-xl <?= $card['tone'] ?>">
                        <i data-lucide="<?= $card['icon'] ?>" class="h-5 w-5"></i>
                    </span>
                </div>
            </div>
        <?php endforeach; ?>
    </div>

    <section class="grid grid-cols-1 gap-6 xl:grid-cols-12">
        <div class="rounded-2xl border border-border bg-background/60 sales-report-module p-4 xl:col-span-4">
            <div class="mb-4 border-b border-border pb-4">
                <h3 class="text-lg font-semibold text-foreground"><?= lang('bugs') . ' ' . lang('status') ?></h3>
                <p class="text-sm text-muted-foreground">Status mix of project-linked defects.</p>
            </div>
            <div class="sales-report-chart" data-chart="bar" data-height="320" data-height-sm="240" data-series='<?= json_encode($status_series, JSON_UNESCAPED_SLASHES | JSON_HEX_APOS) ?>'></div>
        </div>
        <div class="rounded-2xl border border-border bg-background/60 sales-report-module p-4 xl:col-span-8">
            <div class="mb-4 border-b border-border pb-4">
                <h3 class="text-lg font-semibold text-foreground"><?= lang('bugs_r_assignment') ?></h3>
                <p class="text-sm text-muted-foreground">Assigned project defect workload by user.</p>
            </div>
            <div class="sales-report-chart" data-chart="bar" data-height="320" data-height-sm="240" data-series='<?= json_encode($assignment_series, JSON_UNESCAPED_SLASHES | JSON_HEX_APOS) ?>'></div>
        </div>
    </section>

    <section class="rounded-2xl border border-border bg-background/60 sales-report-module p-4">
        <div class="mb-4 border-b border-border pb-4">
            <h3 class="text-lg font-semibold text-foreground"><?= lang('bugs') ?> Trend</h3>
            <p class="text-sm text-muted-foreground">Monthly project-bug movement through the current year.</p>
        </div>
        <div class="sales-report-chart" data-chart="area" data-height="320" data-height-sm="240" data-series='<?= json_encode($trend_series, JSON_UNESCAPED_SLASHES | JSON_HEX_APOS) ?>'></div>
    </section>
</div>
