<?php

session_start();

define('BASE_PATH', dirname(__DIR__));
define('BASE_URL', '/');

function isLoggedIn(): bool
{
    return !empty($_SESSION['user_id']);
}

function requireLogin(): void
{
    if (!isLoggedIn()) {
        header('Location: index.php');
        exit;
    }
}

function currentUser(): ?array
{
    return $_SESSION['user'] ?? null;
}

function flash(string $key, ?string $message = null): ?string
{
    if ($message !== null) {
        $_SESSION['flash'][$key] = $message;
        return null;
    }
    $msg = $_SESSION['flash'][$key] ?? null;
    unset($_SESSION['flash'][$key]);
    return $msg;
}

function e(?string $value): string
{
    return htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8');
}

function formatRupiah($amount): string
{
    return 'Rp ' . number_format((float) $amount, 0, ',', '.');
}

function optionsYaTidak(string $selected = ''): string
{
    $html = '<option value="" disabled' . ($selected === '' ? ' selected' : '') . '>Pilih</option>';
    foreach (['Ya', 'Tidak'] as $opt) {
        $html .= '<option value="' . $opt . '"' . ($selected === $opt ? ' selected' : '') . '>' . $opt . '</option>';
    }
    return $html;
}

function validYaTidak(?string $value): bool
{
    return in_array($value, ['Ya', 'Tidak'], true);
}

function pekerjaanOrtuList(): array
{
    return [
        'Buruh',
        'Karyawan BUMN',
        'Karyawan Swasta',
        'Pedagang Besar',
        'Pedagang Kecil',
        'Pensiunan',
        'Petani',
        'Peternak',
        'PNS/TNI/Polri',
        'Wiraswasta',
        'Wirausaha',
        'Lainnya',
        'Sudah Meninggal',
        'Tidak Bekerja',
        'Tidak Dapat Diterapkan',
    ];
}

function optionsPekerjaanOrtu(string $selected = ''): string
{
    $list = pekerjaanOrtuList();
    $html = '<option value="" disabled' . ($selected === '' ? ' selected' : '') . '>Pilih</option>';
    foreach ($list as $opt) {
        $html .= '<option value="' . e($opt) . '"' . ($selected === $opt ? ' selected' : '') . '>' . e($opt) . '</option>';
    }
    if ($selected !== '' && !in_array($selected, $list, true)) {
        $html .= '<option value="' . e($selected) . '" selected>' . e($selected) . '</option>';
    }
    return $html;
}

function validPekerjaanOrtu(?string $value): bool
{
    return in_array($value, pekerjaanOrtuList(), true);
}

function penghasilanOrtuList(): array
{
    return [
        'Kurang dari Rp. 500.000',
        'Rp. 500.000 - Rp. 999.999',
        'Rp. 1.000.000 - Rp. 1.999.999',
        'Rp. 2.000.000 - Rp. 4.999.999',
        'Rp. 5.000.000 - Rp. 20.000.000',
        'Lebih dari Rp. 20.000.000',
        'Tidak Berpenghasilan',
    ];
}

function optionsPenghasilanOrtu(string $selected = ''): string
{
    $list = penghasilanOrtuList();
    $html = '<option value="" disabled' . ($selected === '' ? ' selected' : '') . '>Pilih</option>';
    foreach ($list as $opt) {
        $html .= '<option value="' . e($opt) . '"' . ($selected === $opt ? ' selected' : '') . '>' . e($opt) . '</option>';
    }
    if ($selected !== '' && !in_array($selected, $list, true)) {
        $html .= '<option value="' . e($selected) . '" selected>' . e($selected) . '</option>';
    }
    return $html;
}

function validPenghasilanOrtu(?string $value): bool
{
    return in_array($value, penghasilanOrtuList(), true);
}
