<?php
// MJ Marketplace v2.9.0 Advanced Admin Layout Pro helpers
if (!function_exists('mj_v290_ensure_schema')) {
function mj_v290_ensure_schema(): void {
    if (!mj_is_installed()) { return; }
    if (function_exists('mj_v289_ensure_schema')) { try { mj_v289_ensure_schema(); } catch (Throwable $e) {} }
    try { q("CREATE TABLE IF NOT EXISTS mj_admin_layout_settings (
        admin_id BIGINT NOT NULL PRIMARY KEY,
        layout_mode VARCHAR(40) NOT NULL DEFAULT 'comfortable',
        theme_mode VARCHAR(40) NOT NULL DEFAULT 'light',
        sidebar_default VARCHAR(40) NOT NULL DEFAULT 'expanded',
        fixed_header TINYINT(1) NOT NULL DEFAULT 1,
        card_shadow TINYINT(1) NOT NULL DEFAULT 1,
        dashboard_widgets TEXT NULL,
        updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4"); } catch (Throwable $e) {}
    try { q("CREATE TABLE IF NOT EXISTS mj_admin_task_notes (
        id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
        admin_id BIGINT NULL,
        task_title VARCHAR(190) NOT NULL,
        task_status VARCHAR(50) NOT NULL DEFAULT 'open',
        task_priority VARCHAR(30) NOT NULL DEFAULT 'normal',
        related_type VARCHAR(80) NULL,
        related_id BIGINT NULL,
        due_at DATETIME NULL,
        note TEXT NULL,
        created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
        updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
        INDEX(admin_id), INDEX(task_status), INDEX(task_priority), INDEX(related_type,related_id)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4"); } catch (Throwable $e) {}
    try {
        if (table_exists('mj_admin_task_notes') && !mj_has_col('mj_admin_task_notes','task_priority')) { q("ALTER TABLE mj_admin_task_notes ADD COLUMN task_priority VARCHAR(30) NOT NULL DEFAULT 'normal' AFTER task_status"); }
        if (table_exists('mj_admin_task_notes') && !mj_has_col('mj_admin_task_notes','related_type')) { q("ALTER TABLE mj_admin_task_notes ADD COLUMN related_type VARCHAR(80) NULL AFTER task_priority"); }
        if (table_exists('mj_admin_task_notes') && !mj_has_col('mj_admin_task_notes','related_id')) { q("ALTER TABLE mj_admin_task_notes ADD COLUMN related_id BIGINT NULL AFTER related_type"); }
    } catch(Throwable $e) {}

    // v2.9.0 requested default: featured and sponsored listing each SGD 10 per default month.
    foreach ([
        'listing_feature_charge_amount' => '10.00',
        'listing_sponsor_charge_amount' => '10.00',
        'listing_upgrade_days' => '30',
        'listing_upgrade_currency' => 'SGD'
    ] as $k=>$v) {
        try {
            $cur = setting($k, '');
            if ($cur === '') { mj_setting_set($k, $v); }
        } catch(Throwable $e) {}
    }
}
}

if (!function_exists('mj_v290_admin_task_counts')) {
function mj_v290_admin_task_counts(): array {
    $out = ['open'=>0,'urgent'=>0,'due_today'=>0,'done'=>0];
    if (!table_exists('mj_admin_task_notes')) { return $out; }
    try { $out['open'] = safe_count('SELECT COUNT(*) c FROM mj_admin_task_notes WHERE task_status IN ("open","pending","in_progress")'); } catch(Throwable $e) {}
    try { $out['urgent'] = safe_count('SELECT COUNT(*) c FROM mj_admin_task_notes WHERE task_status IN ("open","pending","in_progress") AND task_priority IN ("urgent","high")'); } catch(Throwable $e) {}
    try { $out['due_today'] = safe_count('SELECT COUNT(*) c FROM mj_admin_task_notes WHERE task_status IN ("open","pending","in_progress") AND DATE(due_at)<=CURDATE()'); } catch(Throwable $e) {}
    try { $out['done'] = safe_count('SELECT COUNT(*) c FROM mj_admin_task_notes WHERE task_status IN ("done","completed")'); } catch(Throwable $e) {}
    return $out;
}
}

if (!function_exists('mj_v290_finance_metrics')) {
function mj_v290_finance_metrics(): array {
    if (function_exists('mj293_finance_metrics')) {
        $x = mj293_finance_metrics();
        return [
            'pending_proofs'=>(int)($x['pending_review_count'] ?? 0),
            'approved_proofs'=>(int)($x['paid_count'] ?? 0),
            'rejected_proofs'=>(int)($x['rejected_count'] ?? 0),
            'proof_amount'=>(float)($x['pending_review_amount'] ?? 0),
            'pending_amount'=>(float)($x['pending_review_amount'] ?? 0),
            'awaiting_payment_amount'=>(float)($x['awaiting_user_payment_amount'] ?? 0),
            'paid_amount'=>(float)($x['paid_amount'] ?? 0),
            'open_amount'=>(float)($x['open_amount'] ?? 0),
            'invoices_unpaid'=>(int)($x['awaiting_user_payment_count'] ?? 0),
            'invoices_paid'=>(int)($x['paid_count'] ?? 0),
            'refunds_pending'=>(int)($x['refunds_pending'] ?? 0),
        ];
    }
    $m = ['pending_proofs'=>0,'approved_proofs'=>0,'rejected_proofs'=>0,'proof_amount'=>0.0,'invoices_unpaid'=>0,'invoices_paid'=>0,'refunds_pending'=>0];
    return $m;
}
}

if (!function_exists('mj_v290_simple_rows')) {
function mj_v290_simple_rows(string $sql, array $params = []): array { try { return all_rows($sql, $params); } catch(Throwable $e) { return []; } }
}
