# -*- coding: utf-8 -*-
"""
apply_header_footer.py
index.html'deki yeni header, menü, CSS stilleri ve footer'ı
diğer tüm .html dosyalarına uygular.
"""

import os
import re

SITE_DIR = r"C:\Users\PC\Drive'ım\websitesi\baybilig"

EXCLUDE = {
    "index.html",
    "google17f93b6655d1aac1.html",
    "yandex_28a4f90621463a9c.html",
    "404.html",
}

# ──────────────────────────────────────────────────────────────
# 1.  YENİ HEADER (pre-header + logo + menü)
# ──────────────────────────────────────────────────────────────
NEW_HEADER = """\
<!DOCTYPE html>
<html lang="tr">
<head>
    {title_tag}
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    {meta_desc}
    {meta_kw}
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=6" />
    {canonical}
    <link rel="stylesheet" href="assets/css/bootstrap.css">
    <link rel="stylesheet" href="assets/css/animate.css">
    <link rel="stylesheet" href="assets/css/font-awesome.css">
    <link rel="stylesheet" href="assets/css/nexus.css">
    <link rel="stylesheet" href="assets/css/responsive.css">
    <link rel="stylesheet" href="assets/css/custom.css">
    <link href="https://fonts.googleapis.com/css2?family=Ubuntu:wght@300;400;700&display=swap" rel="stylesheet">
    <style>
        /* (Şerit Tasarım) */
        @media (max-width: 1199px) {{
            #header {{
                display: none !important;
            }}
            .slicknav_menu {{
                position: -webkit-sticky !important;
                position: sticky !important;
                top: 0;
                z-index: 99999 !important;
                background-color: #ffffff !important;
                border-bottom: 2px solid #337ab7;
                box-shadow: 0 4px 6px rgba(0,0,0,0.1);
                min-height: 70px;
                padding: 15px !important;
            }}
            .slicknav_menu::before {{
                content: "";
                display: block;
                background: url('assets/img/logo.png') no-repeat left center;
                background-size: contain;
                height: 45px;
                width: 180px;
                position: absolute;
                left: 15px;
                top: 12px;
                z-index: 99;
            }}
            .slicknav_btn {{
                background-color: #337ab7 !important;
                margin-top: 2px !important;
                border-radius: 4px;
            }}
            .slicknav_nav {{
                background-color: #337ab7 !important;
                margin-top: 15px;
            }}
            .slicknav_nav a {{
                color: #ffffff !important;
            }}
            .slicknav_nav a:hover {{
                background-color: #1e3a8a !important;
            }}
        }}
    </style>
</head>
<body>
    <div id="body-bg">
        <div id="pre-header" class="background-gray-lighter">
            <div class="container no-padding">
                <div class="row hidden-xs">
                    <div class="col-sm-6 padding-vert-5">
                        <i class="fa fa-whatsapp" style="color:#25d366;"></i>&nbsp;<a href="https://wa.me/+905319882592" target="_blank"><strong>+90 531 988 25 92</strong></a>
                        &nbsp;&nbsp;|&nbsp;&nbsp;
                        <i class="fa fa-telegram" style="color:#0088cc;"></i>&nbsp;<a href="https://t.me/baybilig" target="_blank"><strong>t.me/baybilig</strong></a>
                    </div>
                    <div class="col-sm-6 text-right padding-vert-5">
                        <strong>E-mail: </strong><a href="mailto:01@baybilig.com">01@baybilig.com</a>
                    </div>
                </div>
            </div>
        </div>

        <div id="header" style="padding: 20px 0;">
            <div class="container">
                <div class="row">
                    <div class="col-md-12 text-center">
                        <a href="index.html">
                            <img src="assets/img/logo.png" class="img-responsive" style="max-height:120px; width:auto; display:inline-block; margin: 0 auto;" alt="baybilig logo">
                        </a>
                    </div>
                </div>
            </div>
        </div>

        <div class="clearfix"></div>

        <div id="hornav" class="bottom-border-shadow">
            <div class="container no-padding border-bottom">
                <div class="row">
                    <div class="col-md-12 no-padding text-center">
                        <div class="visible-lg">
                            <ul id="hornavmenu" class="nav navbar-nav" style="display:inline-block; float:none;">
                                <li><a href="index.html"><strong>ANASAYFA</strong></a></li>
                                <li>
                                    <span><strong>HİZMETLERİMİZ</strong></span>
                                    <ul>
                                        <li class="parent">
                                            <a href="#">ŞİRKET DANIŞMANLIĞI</a>
                                            <ul>
                                                <li><a href="calisma-vizesi.html"> Şirket Kuruluş ve Yapılandırma</a></li>
                                                <li><a href="ikamet-izni.html"> E-Ticaret ve Dijitalleşme</a></li>
                                                <li><a href="https://www.baybilim.com" target="_blank"> Teşvik ve Yatırım Danışmanlığı</a></li>
                                                <li><a href="https://www.baybilmed.com" target="_blank"> Kurumsal Bilgi ve Arşiv Yönetimi</a></li>
                                            </ul>
                                        </li>
                                        <li class="parent">
                                            <a href="#">YABANCI UYRUKLU DANIŞMANLIK</a>
                                            <ul>
                                                <li><a href="calisma-vizesi.html">Çalışma İzni ve Vize Süreçleri</a></li>
                                                <li><a href="ikamet-izni.html">İkamet İzni Danışmanlığı</a></li>
                                                <li><a href="https://www.baybilim.com" target="_blank">Yabancı Uyruklu Eğitim</a></li>
                                                <li><a href="https://www.baybilmed.com" target="_blank">Yabancı Uyruklu Sağlık</a></li>
                                            </ul>
                                        </li>
                                        <li class="parent">
                                            <a href="arsiv.html">ARŞİV DANIŞMANLIK</a>
                                            <ul>
                                                <li><a href="ar-dijital.html">Dijital Arşiv</a></li>
                                                <li><a href="ar-fiziki.html">Fiziki Arşiv</a></li>
                                            </ul>
                                        </li>
                                        <li><a href="organizasyon-hizmetleri.html">KURUMSAL ORGANİZASYON</a></li>
                                        <li><a href="tasarim-matbaa.html">TASARIM VE MATBAA</a></li>
                                    </ul>
                                </li>
                                <li><a href="haberler.html"><strong>HABERLER</strong></a></li>
                                <li><a href="iletisim.html"><strong>İLETİŞİM</strong></a></li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>

"""

# ──────────────────────────────────────────────────────────────
# 2.  YENİ FOOTER
# ──────────────────────────────────────────────────────────────
NEW_FOOTER = """\

        <!-- === YENİ KURUMSAL FOOTER BAŞLANGICI === -->
        <div id="base" style="background: #222222; color: #cccccc; border-top: 5px solid #337ab7;">
            <div class="container padding-vert-40">
                <div class="row">
                    <!-- Sütun 1: Hakkımızda ve Logo -->
                    <div class="col-md-3 margin-bottom-20">
                        <img src="assets/img/logo.png" style="max-height: 80px; background:#fff; padding:10px; border-radius:5px; margin-bottom: 20px;" alt="Bay Bilig Logo">
                        <p style="font-size: 13px; line-height: 1.8;">Bay Bilig, kurumların en değerli varlığı olan "Bilgi"yi yöneterek sürdürülebilir başarıya dönüştüren profesyonel bir B2B danışmanlık merkezidir.</p>
                    </div>
                    <!-- Sütun 2: Hızlı Menü -->
                    <div class="col-md-3 margin-bottom-20">
                        <h3 class="margin-bottom-20" style="color: #ffffff !important; font-size: 18px; border-bottom: 2px solid #337ab7; padding-bottom: 10px; display: inline-block;">Hızlı Menü</h3>
                        <ul class="list-unstyled" style="line-height: 2.2;">
                            <li><i class="fa fa-angle-right" style="color:#337ab7; margin-right: 5px;"></i> <a href="index.html" style="color: #cccccc; text-decoration: none;">Anasayfa</a></li>
                            <li><i class="fa fa-angle-right" style="color:#337ab7; margin-right: 5px;"></i> <a href="haberler.html" style="color: #cccccc; text-decoration: none;">Haberler</a></li>
                            <li><i class="fa fa-angle-right" style="color:#337ab7; margin-right: 5px;"></i> <a href="arsiv.html" style="color: #cccccc; text-decoration: none;">Kurumsal Arşiv</a></li>
                            <li><i class="fa fa-angle-right" style="color:#337ab7; margin-right: 5px;"></i> <a href="calisma-vizesi.html" style="color: #cccccc; text-decoration: none;">Çalışma İzni</a></li>
                            <li><i class="fa fa-angle-right" style="color:#337ab7; margin-right: 5px;"></i> <a href="organizasyon-hizmetleri.html" style="color: #cccccc; text-decoration: none;">Kurumsal Organizasyon</a></li>
                        </ul>
                    </div>
                    <!-- Sütun 3: İletişim Bilgileri -->
                    <div class="col-md-3 margin-bottom-20">
                        <h3 class="margin-bottom-20" style="color: #ffffff !important; font-size: 18px; border-bottom: 2px solid #337ab7; padding-bottom: 10px; display: inline-block;">İletişim Bilgileri</h3>
                        <ul class="list-unstyled" style="line-height: 2;">
                            <li style="margin-bottom: 10px; font-size: 13px;"><i class="fa fa-map-marker" style="color:#337ab7; font-size: 16px; width: 20px;"></i> Mimar Sinan Mh. Mimar Sinan Blv. No:21 E/Blok Zemin Kat Efeler/Aydın</li>
                            <li style="margin-bottom: 10px;"><i class="fa fa-phone" style="color:#337ab7; font-size: 16px; width: 20px;"></i> <a href="https://wa.me/+905319882592" target="_blank" style="color: #cccccc; text-decoration: none;">+90 531 988 25 92</a></li>
                            <li style="margin-bottom: 10px;"><i class="fa fa-envelope" style="color:#337ab7; font-size: 16px; width: 20px;"></i> <a href="mailto:01@baybilig.com" style="color: #cccccc; text-decoration: none;">01@baybilig.com</a></li>
                        </ul>
                    </div>
                    <!-- Sütun 4: Sosyal Medya -->
                    <div class="col-md-3 margin-bottom-20">
                        <h3 class="margin-bottom-20" style="color: #ffffff !important; font-size: 18px; border-bottom: 2px solid #337ab7; padding-bottom: 10px; display: inline-block;">Sosyal Medya</h3>
                        <p style="font-size: 13px; line-height: 1.8;">Mevzuat değişiklikleri ve güncel gelişmelerden haberdar olmak için bizi takip edin.</p>
                        <div style="display: flex; gap: 10px; margin-top: 15px; align-items: center;">
                            <a href="https://t.me/baybilig" target="_blank" style="background-color: #0088cc !important; color: #ffffff !important; width: 38px; height: 38px; border-radius: 4px; display: flex; align-items: center; justify-content: center; text-decoration: none;">
                                <i class="fa fa-paper-plane" style="color: #ffffff !important; font-size: 20px !important;"></i>
                            </a>
                            <a href="https://www.instagram.com/baybilig" target="_blank" style="background-color: #e1306c !important; color: #ffffff !important; width: 38px; height: 38px; border-radius: 4px; display: flex; align-items: center; justify-content: center; text-decoration: none;">
                                <i class="fa fa-instagram" style="color: #ffffff !important; font-size: 20px !important;"></i>
                            </a>
                            <a href="https://www.facebook.com/baybilig" target="_blank" style="background-color: #3b5998 !important; color: #ffffff !important; width: 38px; height: 38px; border-radius: 4px; display: flex; align-items: center; justify-content: center; text-decoration: none;">
                                <i class="fa fa-facebook" style="color: #ffffff !important; font-size: 20px !important;"></i>
                            </a>
                            <a href="https://vk.com/baybilig" target="_blank" style="background-color: #4a76a8 !important; color: #ffffff !important; width: 38px; height: 38px; border-radius: 4px; display: flex; align-items: center; justify-content: center; text-decoration: none;">
                                <i class="fa fa-vk" style="color: #ffffff !important; font-size: 20px !important;"></i>
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Footer Alt Telif Hakkı -->
        <div id="footer" style="background: #111111; color: #888888; padding: 15px 0;">
            <div class="container">
                <div class="row">
                    <div class="col-md-12 text-center">
                        <p style="margin:0; font-size: 13px;">Tüm Hakları Saklıdır. &copy; 2026 <strong>Bay Bilig</strong> - Bilgi ve Belge Danışmanlık Hizmetleri</p>
                    </div>
                </div>
            </div>
        </div>

    </div>
    <script>
        $(document).ready(function() {
            $(".carousel").on("touchstart", function(event){
                var xClick = event.originalEvent.touches[0].pageX;
                $(this).one("touchmove", function(event){
                    var xMove = event.originalEvent.touches[0].pageX;
                    if( Math.floor(xClick - xMove) > 5 ){ $(this).carousel('next'); }
                    else if( Math.floor(xClick - xMove) < -5 ){ $(this).carousel('prev'); }
                    $(this).off("touchmove");
                });
            });
        });
    </script>
    <script src="assets/js/jquery.min.js"></script>
    <script src="assets/js/bootstrap.min.js"></script>
    <script src="assets/js/scripts.js"></script>
    <script type="text/javascript" src="assets/js/jquery.isotope.js"></script>
    <script type="text/javascript" src="assets/js/jquery.slicknav.js"></script>
    <script type="text/javascript" src="assets/js/jquery.visible.js"></script>
    <script type="text/javascript" src="assets/js/jquery.sticky.js"></script>
    <script type="text/javascript" src="assets/js/slimbox2.js"></script>
    <script src="assets/js/modernizr.custom.js"></script>
</body>
</html>
"""

# ──────────────────────────────────────────────────────────────
# YARDIMCI FONKSİYONLAR
# ──────────────────────────────────────────────────────────────

def extract_tag(html, pattern):
    """Verilen regex'le eşleşen ilk grubu döndürür, bulamazsa ''."""
    m = re.search(pattern, html, re.IGNORECASE | re.DOTALL)
    return m.group(0).strip() if m else ""

def extract_content_block(html):
    """
    <div id="content"...> ile başlayıp
    <div id="base"> veya <div id="footer"> başlamadan önceki
    içerik bloğunu döndürür.
    """
    # İçerik bloğu: <div id="content" ile footer/base'e kadar
    pattern = r'(<div\s+id=["\']content["\'][^>]*>.*?)(?=\s*<div\s+id=["\'](?:base|footer)["\']|\s*<!--\s*===?\s*(?:BEGIN\s+FOOTER|END\s+CONTENT))'
    m = re.search(pattern, html, re.IGNORECASE | re.DOTALL)
    if m:
        return m.group(1)
    # Alternatif: END CONTENT yorumundan önce
    pattern2 = r'(<div\s+id=["\']content["\'][^>]*>.*?)(?=<!--\s*===?\s*END\s+CONTENT)'
    m2 = re.search(pattern2, html, re.IGNORECASE | re.DOTALL)
    if m2:
        return m2.group(1)
    return None

# ──────────────────────────────────────────────────────────────
# ANA DÖNGÜ
# ──────────────────────────────────────────────────────────────

html_files = [
    f for f in os.listdir(SITE_DIR)
    if f.endswith(".html") and f not in EXCLUDE
]

print(f"Toplam {len(html_files)} dosya güncellenecek...")

for fname in sorted(html_files):
    fpath = os.path.join(SITE_DIR, fname)
    with open(fpath, "r", encoding="utf-8", errors="replace") as fh:
        original = fh.read()

    # -- Meta verileri çıkar --
    title_tag  = extract_tag(original, r'<title>.*?</title>')
    meta_desc  = extract_tag(original, r'<meta\s+name=["\']description["\'][^>]*/?>') 
    meta_kw    = extract_tag(original, r'<meta\s+name=["\']keywords["\'][^>]*/?>') 
    canonical  = extract_tag(original, r'<link\s+rel=["\']canonical["\'][^>]*/?>') 

    if not title_tag:
        title_tag = "<title>Bay Bilig - Bilgi ve Belge Danışmanlık Hizmetleri</title>"

    # -- İçerik bloğunu çıkar --
    content_block = extract_content_block(original)

    if not content_block:
        print(f"  ⚠ İçerik bloğu bulunamadı, atlanıyor: {fname}")
        continue

    # -- Yeni HTML oluştur --
    header = NEW_HEADER.format(
        title_tag  = title_tag,
        meta_desc  = meta_desc  or "",
        meta_kw    = meta_kw    or "",
        canonical  = canonical  or "",
    )

    new_html = header + content_block + NEW_FOOTER

    with open(fpath, "w", encoding="utf-8") as fh:
        fh.write(new_html)

    print(f"  ✓ Güncellendi: {fname}")

print("\nTüm dosyalar başarıyla güncellendi!")
