<?php
declare(strict_types=1);

require_once __DIR__ . '/../app/init.php';
app_require_login();

$requestedCompanyId = (int) ($_GET['company_id'] ?? 0);
$companyId = app_effective_company_id($requestedCompanyId > 0 ? $requestedCompanyId : null, true);
if ($companyId !== null && $companyId > 0) {
    app_set_context_company_id($companyId);
}
$company = $companyId ? app_company_by_id((int) $companyId) : null;
$counts = app_module_counts($companyId);
$visibleBySlug = [];
foreach (app_visible_modules() as $module) {
    $slug = (string) ($module['slug'] ?? '');
    if ($slug !== '') {
        $visibleBySlug[$slug] = $module;
    }
}

$isArabic = app_ui_language() === 'ar';
$txt = static fn (string $en, string $ar): string => $isArabic ? $ar : $en;
$companyQuery = $companyId ? ('company_id=' . urlencode((string) $companyId)) : '';
$card = static function (string $slug, string $accent) use ($visibleBySlug, $counts, $companyQuery): ?array {
    $module = $visibleBySlug[$slug] ?? null;
    if (!is_array($module)) {
        return null;
    }
    return [
        'title' => (string) ($module['title'] ?? $slug),
        'subtitle' => (string) ($module['description'] ?? ''),
        'href' => 'module.php?module=' . urlencode($slug) . ($companyQuery !== '' ? '&' . $companyQuery : ''),
        'icon' => (string) ($module['icon'] ?? strtoupper(substr($slug, 0, 2))),
        'count' => (int) ($counts[$slug] ?? 0),
        'accent' => $accent,
    ];
};

$masterCards = array_filter([
    $card('hr_departments', '#0f766e'),
    $card('hr_job_titles', '#2563eb'),
    $card('hr_shifts', '#0891b2'),
    $card('hr_leave_types', '#f97316'),
    $card('hr_salary_components', '#8b5cf6'),
    $card('employees', '#7c3aed'),
    $card('hr_employee_contracts', '#475569'),
]);
$operationCards = array_filter([
    $card('hr_attendance', '#f59e0b'),
    $card('hr_leave_requests', '#ef4444'),
    $card('payroll_runs', '#16a34a'),
    $card('hr_payroll_items', '#0f766e'),
]);

$reportCards = [
    ['title' => $txt('HR Reports', 'تقارير الموارد البشرية'), 'href' => 'hr_reports.php' . ($companyQuery !== '' ? '?' . $companyQuery : ''), 'icon' => 'HR', 'accent' => '#0ea5e9', 'count' => 3],
    ['title' => $txt('Payroll Summary', 'ملخص الرواتب'), 'href' => 'hr_reports.php?report=payroll' . ($companyQuery !== '' ? '&' . $companyQuery : ''), 'icon' => 'PR', 'accent' => '#16a34a', 'count' => (int) ($counts['payroll_runs'] ?? 0)],
    ['title' => $txt('Attendance Report', 'تقرير الحضور'), 'href' => 'hr_reports.php?report=attendance' . ($companyQuery !== '' ? '&' . $companyQuery : ''), 'icon' => 'AT', 'accent' => '#f59e0b', 'count' => (int) ($counts['hr_attendance'] ?? 0)],
];
?>
<!doctype html>
<html <?= app_html_attrs() ?>>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title><?= app_escape((string) app_config('app_name')) ?> - <?= app_escape($txt('Human Resources', 'الموارد البشرية')) ?></title>
  <link rel="icon" href="assets/favicon.svg" type="image/svg+xml">
  <link rel="shortcut icon" href="assets/favicon.svg" type="image/svg+xml">
  <link rel="stylesheet" href="assets/styles.css">
</head>
<body>
<div class="app-shell has-sidebar">
  <?= app_sidebar_nav_html('hr', null, $companyId) ?>
  <div class="workspace-page">
    <section class="workspace-hero workspace-hero--hr">
      <div>
        <a class="workspace-back" href="home.php<?= $companyId ? '?company_id=' . (int) $companyId : '' ?>"><?= app_escape($txt('Back to Home', 'رجوع للرئيسية')) ?></a>
        <h1><?= app_escape($txt('Human Resources', 'الموارد البشرية')) ?></h1>
        <p><?= app_escape($txt('A separate HR workspace for employees, departments, attendance, leave requests, payroll, and HR reports.', 'مساحة منفصلة للموارد البشرية: الموظفين، الأقسام، الحضور، الإجازات، الرواتب، والتقارير.')) ?></p>
      </div>
      <img src="<?= app_escape(app_logo_path_for_header($company)) ?>" alt="Company Logo">
    </section>

    <section class="workspace-kpis">
      <div class="workspace-kpi"><span><?= app_escape($txt('Employees', 'الموظفون')) ?></span><strong><?= app_escape(number_format((int) ($counts['employees'] ?? 0))) ?></strong></div>
      <div class="workspace-kpi"><span><?= app_escape($txt('Departments', 'الأقسام')) ?></span><strong><?= app_escape(number_format((int) ($counts['hr_departments'] ?? 0))) ?></strong></div>
      <div class="workspace-kpi"><span><?= app_escape($txt('Contracts', 'العقود')) ?></span><strong><?= app_escape(number_format((int) ($counts['hr_employee_contracts'] ?? 0))) ?></strong></div>
      <div class="workspace-kpi"><span><?= app_escape($txt('Payroll Lines', 'بنود الرواتب')) ?></span><strong><?= app_escape(number_format((int) ($counts['hr_payroll_items'] ?? 0))) ?></strong></div>
    </section>

    <section class="workspace-section">
      <div class="workspace-section-head">
        <h2><?= app_escape($txt('HR Reports', 'تقارير الموارد البشرية')) ?></h2>
        <span><?= app_escape($txt('Fast access to HR analytics and payroll reports.', 'وصول سريع لتحليلات الموارد البشرية وتقارير الرواتب.')) ?></span>
      </div>
      <div class="workspace-card-grid">
        <?php foreach ($reportCards as $item): ?>
          <a class="workspace-card" href="<?= app_escape((string) $item['href']) ?>" style="--workspace-accent: <?= app_escape((string) $item['accent']) ?>;">
            <span class="workspace-card-icon"><?= app_escape((string) $item['icon']) ?></span>
            <strong><?= app_escape((string) $item['title']) ?></strong>
            <small><?= app_escape(number_format((int) $item['count'])) ?> <?= app_escape($txt('records', 'سجلات')) ?></small>
          </a>
        <?php endforeach; ?>
      </div>
    </section>

    <section class="workspace-section">
      <div class="workspace-section-head">
        <h2><?= app_escape($txt('Master Data', 'البيانات الأساسية')) ?></h2>
        <span><?= app_escape($txt('Departments, job titles, shifts, leave policies, salary components, contracts, and employee profiles.', 'الأقسام والمسميات والشيفتات وسياسات الإجازات وبنود الرواتب والعقود وملفات الموظفين.')) ?></span>
      </div>
      <div class="workspace-card-grid">
        <?php foreach ($masterCards as $item): ?>
          <a class="workspace-card" href="<?= app_escape((string) $item['href']) ?>" style="--workspace-accent: <?= app_escape((string) $item['accent']) ?>;">
            <span class="workspace-card-icon"><?= app_escape((string) $item['icon']) ?></span>
            <strong><?= app_escape((string) $item['title']) ?></strong>
            <small><?= app_escape(number_format((int) $item['count'])) ?> <?= app_escape($txt('records', 'سجلات')) ?></small>
          </a>
        <?php endforeach; ?>
      </div>
    </section>

    <section class="workspace-section">
      <div class="workspace-section-head">
        <h2><?= app_escape($txt('Operations', 'الحركات')) ?></h2>
        <span><?= app_escape($txt('Attendance, leave requests, payroll runs, and detailed payroll lines.', 'الحضور وطلبات الإجازات وتشغيل الرواتب وبنود الرواتب التفصيلية.')) ?></span>
      </div>
      <div class="workspace-card-grid">
        <?php foreach ($operationCards as $item): ?>
          <a class="workspace-card" href="<?= app_escape((string) $item['href']) ?>" style="--workspace-accent: <?= app_escape((string) $item['accent']) ?>;">
            <span class="workspace-card-icon"><?= app_escape((string) $item['icon']) ?></span>
            <strong><?= app_escape((string) $item['title']) ?></strong>
            <small><?= app_escape(number_format((int) $item['count'])) ?> <?= app_escape($txt('records', 'سجلات')) ?></small>
          </a>
        <?php endforeach; ?>
      </div>
    </section>
  </div>
</div>
<script src="assets/app.js"></script>
</body>
</html>
