<?php
/**
 * Cabinet de Avocat - Site de Prezentare
 * Router principal cu suport pentru două limbi (RO / EN).
 */

// Timezone fix (necesar pentru indicatorul de disponibilitate live)
date_default_timezone_set('Europe/Bucharest');

// === Language resolver ===
// Priority: ?lang= → cookie → default 'ro'
$lang_from_get    = isset($_GET['lang']) ? strtolower($_GET['lang']) : '';
$lang_from_cookie = isset($_COOKIE['lang']) ? strtolower($_COOKIE['lang']) : '';

if (in_array($lang_from_get, ['ro', 'en'], true)) {
    $lang = $lang_from_get;
    // Persist choice for 1 year
    setcookie('lang', $lang, [
        'expires'  => time() + 60 * 60 * 24 * 365,
        'path'     => '/',
        'httponly' => false,
        'samesite' => 'Lax',
    ]);
} elseif (in_array($lang_from_cookie, ['ro', 'en'], true)) {
    $lang = $lang_from_cookie;
} else {
    $lang = 'ro';
}

// Load language strings into $L
require __DIR__ . '/lang/' . $lang . '.php';

// Small helper for building URLs that preserve the current language
function langurl($params = []) {
    global $lang;
    $params['lang'] = $lang;
    return 'index.php?' . http_build_query($params);
}
function langurl_root() {
    global $lang;
    return 'index.php?lang=' . urlencode($lang);
}

$pagina   = isset($_GET['pagina']) ? $_GET['pagina'] : 'acasa';
$serviciu = isset($_GET['serviciu']) ? preg_replace('/[^a-z0-9\-]/', '', $_GET['serviciu']) : '';

// Flag de vizibilitate pentru pagina Articole. Setează la true când sunt gata articole reale.
$articole_vizibile = false;

$pagini_valide = ['acasa', 'profil', 'servicii', 'contact', 'programare'];
if ($articole_vizibile) {
    $pagini_valide[] = 'articole';
}

if (!in_array($pagina, $pagini_valide)) {
    $pagina = 'acasa';
}

$titluri = [
    'acasa'      => $L['title_home'],
    'profil'     => $L['title_expertise'],
    'servicii'   => $L['title_services'],
    'articole'   => $L['title_articles'],
    'contact'    => $L['title_contact'],
    'programare' => $L['title_booking'],
];

// If on the services page with a specific service, build the title from the data
$titlu_pagina = $titluri[$pagina];
if ($pagina === 'servicii' && $serviciu !== '') {
    require_once __DIR__ . '/pagini/servicii-data.php';
    if (isset($servicii[$serviciu])) {
        $titlu_pagina = $servicii[$serviciu]['titlu'] . $L['title_suffix'];
    }
}

// === Indicator de disponibilitate live (bazat pe ora curentă Europe/Bucharest) ===
// Verde (Disponibil acum): 10-11 și 14-15
// Portocaliu (Urgențe): 9-10, 11-12, 15-16
// Roșu (Indisponibil): restul orelor
$current_hour = (int) date('H');
if ($current_hour === 10 || $current_hour === 14) {
    $availability_status = 'available';
} elseif ($current_hour === 9 || $current_hour === 11 || $current_hour === 15) {
    $availability_status = 'emergency';
} else {
    $availability_status = 'unavailable';
}
$availability_label = $L['status_' . $availability_status];

// Build the "other language" URL (for the switcher) — keeps current page/serviciu
$switch_lang       = $lang === 'ro' ? 'en' : 'ro';
$switch_params     = ['lang' => $switch_lang];
if ($pagina !== 'acasa') { $switch_params['pagina'] = $pagina; }
if ($serviciu !== '')    { $switch_params['serviciu'] = $serviciu; }
$switch_url = 'index.php?' . http_build_query($switch_params);

// === SEO: meta description, canonical, hreflang, Open Graph ===
$page_descriptions = [
    'acasa'      => $L['meta_desc_home'],
    'profil'     => $L['meta_desc_expertise'],
    'servicii'   => $L['meta_desc_services'],
    'articole'   => $L['meta_desc_articles'],
    'contact'    => $L['meta_desc_contact'],
    'programare' => $L['meta_desc_booking'],
];
$meta_description = isset($page_descriptions[$pagina]) ? $page_descriptions[$pagina] : $page_descriptions['acasa'];

// Pe paginile de detaliu serviciu, folosim subtitlul/intro ca descriere
if ($pagina === 'servicii' && $serviciu !== '') {
    if (!isset($servicii)) { require_once __DIR__ . '/pagini/servicii-data.php'; }
    if (isset($servicii[$serviciu])) {
        $candidate = $servicii[$serviciu]['subtitlu'] ?? '';
        if ($candidate === '' && isset($servicii[$serviciu]['intro'])) {
            $candidate = $servicii[$serviciu]['intro'];
        }
        if ($candidate !== '') {
            // Trim la ~160 caractere ca să se încadreze în limita SERP.
            // Folosim mb_substr dacă mbstring e instalat; altfel substr (acceptabil — descrierile sunt deja scurte).
            $clean = strip_tags($candidate);
            $meta_description = function_exists('mb_substr')
                ? mb_substr($clean, 0, 160, 'UTF-8')
                : substr($clean, 0, 160);
        }
    }
}

// Originea site-ului (folosit pentru canonical absolut)
$site_origin = (
    (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ||
    (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https')
    ? 'https://' : 'http://'
) . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'petcupartners.ro');

// Helper care construiește un URL canonical pentru o limbă dată
function build_canonical_url($lang_code, $pagina, $serviciu, $site_origin) {
    if ($pagina === 'acasa' && $serviciu === '') {
        return $site_origin . '/?lang=' . urlencode($lang_code);
    }
    $params = ['pagina' => $pagina, 'lang' => $lang_code];
    if ($serviciu !== '') { $params['serviciu'] = $serviciu; }
    return $site_origin . '/?' . http_build_query($params);
}
$canonical_url = build_canonical_url($lang,   $pagina, $serviciu, $site_origin);
$alternate_ro  = build_canonical_url('ro',    $pagina, $serviciu, $site_origin);
$alternate_en  = build_canonical_url('en',    $pagina, $serviciu, $site_origin);

$og_locale     = $L['og_locale'];
$og_locale_alt = $L['og_locale_alt'];
$og_image      = $site_origin . '/assets/img/logo.png';
?>
<!DOCTYPE html>
<html lang="<?php echo $lang; ?>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $titlu_pagina; ?></title>

    <!-- ============================================================
         Google Search Console — verificare prin meta tag.
         Pași: search.google.com/search-console → Add property → URL prefix
               → metoda "HTML tag" → copiază valoarea content="..."
               → înlocuiește mai jos "INSEREAZĂ-AICI-CODUL-DE-VERIFICARE".
         După deploy, click "Verify" în Search Console.
         (Alternativ, lasă comentat și folosește verificarea DNS.)
    ============================================================ -->
    <meta name="google-site-verification" content="Y6ABCvcpW28LxF4Mg4MwerBcQA7NQxzQQtfKvvQfB_I">

    <!-- SEO: meta primare -->
    <meta name="description" content="<?php echo htmlspecialchars($meta_description, ENT_QUOTES, 'UTF-8'); ?>">
    <meta name="keywords" content="<?php echo htmlspecialchars($L['meta_keywords'], ENT_QUOTES, 'UTF-8'); ?>">
    <meta name="author" content="Petcu Partners">
    <meta name="robots" content="index, follow, max-image-preview:large">
    <meta name="theme-color" content="#1a2332">

    <!-- Canonical + alternate (hreflang) — bilingual SEO -->
    <link rel="canonical" href="<?php echo htmlspecialchars($canonical_url, ENT_QUOTES, 'UTF-8'); ?>">
    <link rel="alternate" hreflang="ro" href="<?php echo htmlspecialchars($alternate_ro, ENT_QUOTES, 'UTF-8'); ?>">
    <link rel="alternate" hreflang="en" href="<?php echo htmlspecialchars($alternate_en, ENT_QUOTES, 'UTF-8'); ?>">
    <link rel="alternate" hreflang="x-default" href="<?php echo htmlspecialchars($alternate_ro, ENT_QUOTES, 'UTF-8'); ?>">

    <!-- Open Graph (Facebook, LinkedIn, WhatsApp) -->
    <meta property="og:type" content="website">
    <meta property="og:site_name" content="<?php echo htmlspecialchars($L['og_site_name'], ENT_QUOTES, 'UTF-8'); ?>">
    <meta property="og:title" content="<?php echo htmlspecialchars($titlu_pagina, ENT_QUOTES, 'UTF-8'); ?>">
    <meta property="og:description" content="<?php echo htmlspecialchars($meta_description, ENT_QUOTES, 'UTF-8'); ?>">
    <meta property="og:url" content="<?php echo htmlspecialchars($canonical_url, ENT_QUOTES, 'UTF-8'); ?>">
    <meta property="og:image" content="<?php echo htmlspecialchars($og_image, ENT_QUOTES, 'UTF-8'); ?>">
    <meta property="og:locale" content="<?php echo $og_locale; ?>">
    <meta property="og:locale:alternate" content="<?php echo $og_locale_alt; ?>">

    <!-- Twitter Card -->
    <meta name="twitter:card" content="summary_large_image">
    <meta name="twitter:title" content="<?php echo htmlspecialchars($titlu_pagina, ENT_QUOTES, 'UTF-8'); ?>">
    <meta name="twitter:description" content="<?php echo htmlspecialchars($meta_description, ENT_QUOTES, 'UTF-8'); ?>">
    <meta name="twitter:image" content="<?php echo htmlspecialchars($og_image, ENT_QUOTES, 'UTF-8'); ?>">

    <!-- Favicon (logo existent) -->
    <link rel="icon" type="image/png" href="assets/img/logo.png">
    <link rel="apple-touch-icon" href="assets/img/logo.png">

    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Cormorant+Garamond:ital,wght@0,400;0,500;0,600;0,700;1,400;1,500&family=Inter:wght@300;400;500;600&display=swap" rel="stylesheet">
    <?php $cssPath = __DIR__ . '/assets/css/style.css'; $cssVer = file_exists($cssPath) ? filemtime($cssPath) : time(); ?>
    <link rel="stylesheet" href="assets/css/style.css?v=<?php echo $cssVer; ?>">

    <!-- JSON-LD structured data: LegalService -->
    <script type="application/ld+json">
    {
        "@context": "https://schema.org",
        "@type": "LegalService",
        "name": "Petcu Partners — Cabinet de Avocat Petcu Mihail",
        "alternateName": "Petcu Partners",
        "description": <?php echo json_encode($L['meta_desc_home'], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); ?>,
        "url": <?php echo json_encode($site_origin . '/', JSON_UNESCAPED_SLASHES); ?>,
        "logo": <?php echo json_encode($og_image, JSON_UNESCAPED_SLASHES); ?>,
        "image": <?php echo json_encode($og_image, JSON_UNESCAPED_SLASHES); ?>,
        "telephone": "+40747333165",
        "email": "office@petcupartners.ro",
        "address": {
            "@type": "PostalAddress",
            "addressCountry": "RO",
            "addressLocality": "București"
        },
        "areaServed": [
            { "@type": "Country", "name": "Romania" },
            { "@type": "AdministrativeArea", "name": "European Union" }
        ],
        "knowsLanguage": ["ro", "en"],
        "priceRange": "$$",
        "founder": {
            "@type": "Person",
            "name": "Petcu Mihail",
            "jobTitle": "Avocat / Attorney"
        },
        "serviceType": [
            "Tax law",
            "Business law",
            "Commercial litigation",
            "Customs law",
            "Mergers and acquisitions",
            "Employment law",
            "Administrative court litigation"
        ],
        "openingHoursSpecification": [
            {
                "@type": "OpeningHoursSpecification",
                "dayOfWeek": ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"],
                "opens": "09:00",
                "closes": "17:00"
            }
        ]
    }
    </script>
</head>
<body>

    <!-- Header -->
    <header class="header" id="header">
        <div class="container header-inner">
            <a href="<?php echo langurl_root(); ?>" class="logo" aria-label="<?php echo htmlspecialchars($L['logo_alt'], ENT_QUOTES, 'UTF-8'); ?>">
                <img src="assets/img/logo.png" alt="<?php echo htmlspecialchars($L['logo_title'], ENT_QUOTES, 'UTF-8'); ?>" class="logo-image" width="1264" height="341">
            </a>

            <button class="menu-toggle" id="menuToggle" aria-label="<?php echo htmlspecialchars($L['menu_label'], ENT_QUOTES, 'UTF-8'); ?>">
                <span></span><span></span><span></span>
            </button>

            <nav class="nav" id="nav">
                <a href="<?php echo langurl_root(); ?>" class="nav-link <?php echo $pagina === 'acasa' ? 'active' : ''; ?>"><?php echo $L['nav_home']; ?></a>
                <a href="<?php echo langurl(['pagina' => 'profil']); ?>" class="nav-link <?php echo $pagina === 'profil' ? 'active' : ''; ?>"><?php echo $L['nav_expertise']; ?></a>
                <a href="<?php echo langurl(['pagina' => 'servicii']); ?>" class="nav-link <?php echo $pagina === 'servicii' ? 'active' : ''; ?>"><?php echo $L['nav_services']; ?></a>
                <?php if ($articole_vizibile): ?>
                <a href="<?php echo langurl(['pagina' => 'articole']); ?>" class="nav-link <?php echo $pagina === 'articole' ? 'active' : ''; ?>"><?php echo $L['nav_articles']; ?></a>
                <?php endif; ?>
                <a href="<?php echo langurl(['pagina' => 'contact']); ?>" class="nav-link <?php echo $pagina === 'contact' ? 'active' : ''; ?>"><?php echo $L['nav_contact']; ?></a>

                <!-- Language switcher -->
                <div class="lang-switch" role="group" aria-label="<?php echo htmlspecialchars($L['lang_switch_label'], ENT_QUOTES, 'UTF-8'); ?>">
                    <span class="lang-switch-current" aria-current="true"><?php echo strtoupper($lang); ?></span>
                    <span class="lang-switch-sep" aria-hidden="true">/</span>
                    <a href="<?php echo htmlspecialchars($switch_url, ENT_QUOTES, 'UTF-8'); ?>" class="lang-switch-other" hreflang="<?php echo $switch_lang; ?>" rel="alternate" title="<?php echo htmlspecialchars($switch_lang === 'en' ? $L['lang_en_full'] : $L['lang_ro_full'], ENT_QUOTES, 'UTF-8'); ?>"><?php echo strtoupper($switch_lang); ?></a>
                </div>
            </nav>

            <?php if ($pagina === 'acasa'): ?>
            <div class="availability-indicator availability-<?php echo $availability_status; ?>" role="status" aria-live="polite" title="<?php echo htmlspecialchars($availability_label, ENT_QUOTES, 'UTF-8'); ?>">
                <span class="availability-dot" aria-hidden="true"></span>
                <span class="availability-text"><?php echo htmlspecialchars($availability_label, ENT_QUOTES, 'UTF-8'); ?></span>
            </div>
            <?php endif; ?>
        </div>
    </header>

    <!-- Conținut principal -->
    <main>
        <?php
        if ($pagina === 'servicii' && $serviciu !== '') {
            include __DIR__ . '/pagini/serviciu-detaliu.php';
        } else {
            include __DIR__ . "/pagini/{$pagina}.php";
        }
        ?>
    </main>

    <!-- Footer -->
    <footer class="footer">
        <div class="container">
            <div class="footer-grid">
                <div class="footer-col">
                    <div class="logo logo-footer">
                        <div class="logo-text">
                            <span class="logo-title"><?php echo $L['logo_title']; ?></span>
                            <span class="logo-subtitle"><?php echo $L['logo_subtitle']; ?></span>
                        </div>
                    </div>
                    <p class="footer-desc"><?php echo $L['footer_desc']; ?></p>
                </div>
                <div class="footer-col">
                    <h4><?php echo $L['footer_nav_h']; ?></h4>
                    <ul>
                        <li><a href="<?php echo langurl_root(); ?>"><?php echo $L['nav_home']; ?></a></li>
                        <li><a href="<?php echo langurl(['pagina' => 'profil']); ?>"><?php echo $L['nav_expertise']; ?></a></li>
                        <li><a href="<?php echo langurl(['pagina' => 'servicii']); ?>"><?php echo $L['nav_services']; ?></a></li>
                        <?php if ($articole_vizibile): ?>
                        <li><a href="<?php echo langurl(['pagina' => 'articole']); ?>"><?php echo $L['nav_articles']; ?></a></li>
                        <?php endif; ?>
                        <li><a href="<?php echo langurl(['pagina' => 'contact']); ?>"><?php echo $L['nav_contact']; ?></a></li>
                    </ul>
                </div>
                <div class="footer-col">
                    <h4><?php echo $L['footer_contact_h']; ?></h4>
                    <ul>
                        <li><?php echo $L['footer_contact_phone']; ?> <a href="tel:+40747333165">+40 747.333.165</a></li>
                        <li><?php echo $L['footer_contact_email']; ?> <a href="mailto:office@petcupartners.ro">office@petcupartners.ro</a></li>
                        <li style="margin-top:0.75rem; font-style:italic; opacity:0.75;"><?php echo $L['footer_meetings']; ?></li>
                    </ul>
                </div>
                <div class="footer-col">
                    <h4><?php echo $L['footer_program_h']; ?></h4>
                    <ul>
                        <li><?php echo $L['footer_program_1']; ?></li>
                        <li><?php echo $L['footer_program_2']; ?></li>
                    </ul>
                </div>
            </div>
            <div class="footer-bottom">
                <p>&copy; <?php echo date('Y'); ?> <?php echo $L['footer_copyright']; ?></p>
                <p><?php echo $L['footer_bar']; ?></p>
            </div>
        </div>
    </footer>

    <script src="assets/js/main.js"></script>
</body>
</html>
