import os
import re

template_dir = r"f:\htdocs\maraton\_template_backup"
views_dir = r"f:\htdocs\maraton\laravel_app\resources\views"
layouts_dir = os.path.join(views_dir, "layouts")
os.makedirs(layouts_dir, exist_ok=True)

with open(os.path.join(template_dir, "index.html"), "r", encoding="utf-8") as f:
    html = f.read()

# Helper function to wrap paths with Laravel's {{ asset() }}
def replace_asset(m):
    prefix = m.group(1)
    path = m.group(2)
    suffix = m.group(3)
    if path.startswith('http') or path.startswith('#'):
        return m.group(0)
    # Strip quotes if they exist in url()
    path = path.strip('\'"')
    return f"{prefix}{{{{ asset('{path}') }}}}{suffix}"

# Replace asset URLs
html = re.sub(r'(href=")(css/[^"]+)(")', replace_asset, html)
html = re.sub(r'(src=")(js/[^"]+)(")', replace_asset, html)
html = re.sub(r'(src=")(images/[^"]+)(")', replace_asset, html)
html = re.sub(r'(url\()([^)]+)(\))', replace_asset, html)

# Split into header, content, footer
content_start = html.find('<section class="home-slider')
content_end = html.find('<footer class="ftco-footer ftco-section img">')

head_nav = html[:content_start]
content = html[content_start:content_end]
footer_scripts = html[content_end:]

app_layout = head_nav + "\n    @yield('content')\n\n    " + footer_scripts
index_view = "@extends('layouts.app')\n\n@section('content')\n" + content + "\n@endsection\n"

with open(os.path.join(layouts_dir, "app.blade.php"), "w", encoding="utf-8") as f:
    f.write(app_layout)

with open(os.path.join(views_dir, "index.blade.php"), "w", encoding="utf-8") as f:
    f.write(index_view)

print("Blade templates created successfully!")
