<?php
use Core\System\AdminAuthTheme;

$theme = AdminAuthTheme::get();

/* =============================
   URL NORMALIZER (same as header)
============================= */
if (!function_exists('mte_normalize_asset_url')) {
    function mte_normalize_asset_url($path): string
    {
        $path = trim((string)$path);

        if ($path === '') {
            return '';
        }

        // If already absolute URL (http/https), keep as is
        if (preg_match('#^https?://#i', $path)) {
            return $path;
        }

        // If already root path, keep as is
        if ($path[0] === '/') {
            return $path;
        }

        // Otherwise make it root-relative
        return '/' . $path;
    }
}

/* =============================
   Admin Logo from DB (same as header)
============================= */
$adminLogo = $theme['logo'] ?? '';

try {
    $_settingsModel = new \Core\App\Models\Settings();
    $logoPath = $_settingsModel->get('admin_logo', '')
             ?: $_settingsModel->get('site_logo', '');

    if (!empty($logoPath)) {
        $adminLogo = mte_normalize_asset_url($logoPath);
    } elseif (!empty($adminLogo)) {
        $adminLogo = mte_normalize_asset_url($adminLogo);
    }

} catch (\Throwable $e) {
    // fallback silently
}
?>
<!doctype html>
<html lang="<?= htmlspecialchars($theme['lang']) ?>"
      dir="<?= htmlspecialchars($theme['dir']) ?>">

<head>
<meta charset="utf-8">
<title><?= __('admin.login.title','Admin Login') ?></title>

<meta name="viewport" content="width=device-width, initial-scale=1">

<?php if (!empty($theme['font_css'])): ?>
<link rel="stylesheet" href="<?= $theme['font_css'] ?>">
<?php endif; ?>

<?php if ($theme['dir'] === 'rtl'): ?>
<link rel="stylesheet" href="/admin/assets/css/adminlte.rtl.css">
<?php else: ?>
<link rel="stylesheet" href="/admin/assets/css/adminlte.css">
<?php endif; ?>

<style>
/* =========================
   Direction
========================= */
body.rtl { direction: rtl; text-align: right; }
body.ltr { direction: ltr; text-align: left; }

body.rtl input::placeholder { direction: rtl; text-align: right; }
body.rtl input {
    direction: rtl;
    text-align: right;
}

/* =========================
   Font
========================= */
<?php if (!empty($theme['font_family'])): ?>
body,
.login-box,
.login-card-body,
input,
button,
p,
a,
h1,h2,h3,h4,h5 {
    font-family: <?= $theme['font_family'] ?> !important;
}
<?php endif; ?>

/* =========================
   Background – SVG Pattern
========================= */
body.login-page {
    min-height: 100vh;
    background-color: <?= !empty($theme['background']) ? $theme['background'] : '#f4f6f9' ?> !important;
    background-image: url("/admin/assets/img/bg-tech.svg");
    background-repeat: repeat;
    background-size: 360px 360px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px 14px;
    position: relative;
}

body.login-page::after {
    content: "";
    position: fixed;
    inset: 0;
    background: rgba(255,255,255,0.0);
    pointer-events: none;
    z-index: 0;
}

.login-box,
.login-logo,
.card {
    position: relative;
    z-index: 1;
}

/* =========================
   Layout
========================= */
.login-box {
    width: 100%;
    max-width: 430px;
    margin: 0 !important;
}

/* =========================
   Logo
========================= */
.login-logo {
    margin-bottom: 18px !important;
}
.login-logo img {
    max-height: 70px;
}

/* =========================
   Card
========================= */
.card {
    border: 0 !important;
    border-radius: 10px !important;
    box-shadow: 0 24px 60px rgba(0,0,0,.12) !important;
}
.login-card-body {
    padding: 26px !important;
}

/* =========================
   Text
========================= */
.auth-title {
    margin: 0 0 8px 0;
    font-size: 22px;
    font-weight: 700;
}
.auth-desc {
    margin: 0 0 18px 0;
    color: #6c757d;
    font-size: 14px;
    line-height: 1.6;
}

/* =========================
   Inputs
========================= */
.form-control {
    height: 44px;
    border-radius: 6px;
}

/* =========================
   Button
========================= */
<?php if (!empty($theme['primary'])): ?>
.btn-primary {
    background-color: <?= $theme['primary'] ?> !important;
    border-color: <?= $theme['primary'] ?> !important;
}
<?php endif; ?>
.btn {
    height: 44px;
    border-radius: 6px;
    font-weight: 700;
}

/* =========================
   Footer link
========================= */
.auth-footer {
    margin-top: 14px;
    text-align: center;
    font-size: 13px;
}
.auth-footer a {
    text-decoration: none;
}
</style>
</head>

<body class="login-page <?= $theme['dir'] === 'rtl' ? 'rtl' : 'ltr' ?>">

<div class="login-box">

    <div class="login-logo text-center">
        <?php if (!empty($adminLogo)): ?>
            <img src="<?= htmlspecialchars($adminLogo) ?>" alt="Logo">
        <?php else: ?>
            <b>MTEngine</b>
        <?php endif; ?>
    </div>

    <div class="card">
        <div class="card-body login-card-body">

            <?php if (($_GET['reset'] ?? '') === '1'): ?>
                <div class="alert alert-info">
                    <?= __('admin.reset.sent','If the email exists, a password reset link has been sent.') ?>
                </div>
            <?php elseif (($_GET['reset'] ?? '') === 'success'): ?>
                <div class="alert alert-success">
                    <?= __('admin.reset.success','Your password has been updated successfully. You can log in now.') ?>
                </div>
            <?php endif; ?>

            <div class="auth-title">
                <?= __('admin.login.heading','Sign in') ?>
            </div>

            <div class="auth-desc">
                <?= __('admin.login.desc','Sign in to your admin panel using your credentials.') ?>
            </div>

            <form method="post" action="/admin/login">
                <?= \Core\System\Csrf::field(); ?>

                <div class="mb-3">
                    <input type="text"
                           name="username"
                           class="form-control"
                           placeholder="<?= __('admin.login.username','Username') ?>"
                           required>
                </div>

                <div class="mb-3">
                    <input type="password"
                           name="password"
                           class="form-control"
                           placeholder="<?= __('admin.login.password','Password') ?>"
                           required>
                </div>

                <button type="submit" class="btn btn-primary w-100">
                    <?= __('admin.login.submit','Login') ?>
                </button>
            </form>

            <div class="auth-footer">
                <a href="/admin/forgot-password">
                    <?= __('admin.login.forgot','Forgot password?') ?>
                </a>
            </div>

        </div>
    </div>

</div>

</body>
</html>
