"""One-time template migration for the admin/content redesign."""
from pathlib import Path
import re

root=Path(__file__).parent/'templates'
for path in root.glob('*.html'):
    text=path.read_text(encoding='utf-8')
    text=re.sub(r'src="{{\s*([\w.\[\]]+(?:\.cover_image|\.image)?)\s*}}"',r'src="{{ local_image(\1) }}"',text)
    path.write_text(text,encoding='utf-8')

def edit(name,pairs):
    path=root/name
    text=path.read_text(encoding='utf-8')
    for old,new in pairs:
        if old not in text: raise ValueError(f'Missing template text in {name}: {old[:70]}')
        text=text.replace(old,new)
    path.write_text(text,encoding='utf-8')

edit('base.html',[
 ('    <link rel="preconnect" href="https://fonts.googleapis.com">\n',''),
 ('    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>\n',''),
 ('    <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700;800&family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">\n',''),
 ('</head>','    <link rel="stylesheet" href="{{ url_for(\'static\',filename=\'admin.css\') }}">\n</head>'),
 ('Car<span>Compare</span>','{{ site.site_name }}'),
 ('For the everyday drive. And the roads less travelled. Find a car or bike that feels like you.','{{ site.footer_text }}'),
 ('© {{ current_year }} CarCompare · Made for your next move. Car data: CarDekho. Bike directory: BikeWale.','© {{ current_year }} {{ site.site_name }} · Made for your next move.'),
])
edit('index.html',[
 ('THE NEXT CHAPTER STARTS HERE','{{ site.home_eyebrow }}'),
 ('Life moves.<br>Find <em>your drive.</em>','{{ site.home_title }}<br><em>{{ site.home_accent }}</em>'),
 ('Big plans. Everyday journeys. Discover the car or bike<br class="desktop-break"> that fits your life — and your budget.','{{ site.home_intro }}'),
 ('top_cars and top_cars[0].image','spotlight and spotlight.image'),
 ('top_cars[0]','spotlight'),
 ('A favourite brand? Start here.','{{ site.brand_heading }}'),
 ('THE SHORTLIST','{{ site.picks_eyebrow }}'),
 ('Rated highly. Worth a look.','{{ site.picks_heading }}'),
 ('MORE OF A TWO-WHEEL PERSON?','{{ site.bike_banner_eyebrow }}'),
 ('Same curiosity. A different ride.','{{ site.bike_banner_title }}'),
 ('Discover commuters, cruisers, street bikes, and electric scooters.','{{ site.bike_banner_text }}'),
 ('THE CARCOMPARE JOURNAL','{{ site.journal_eyebrow }}'),
 ('Good reads. Better decisions.','{{ site.journal_teaser }}'),
])
edit('blog_list.html',[
 ('THE CARCOMPARE BLOG','{{ site.journal_eyebrow }}'),
 ('The open-road <em>journal.</em>','{{ site.journal_title }} <em>{{ site.journal_accent }}</em>'),
 ('Fresh perspectives. Practical guides. A little inspiration for your next move.','{{ site.journal_intro }}'),
])
edit('bikes.html',[
 ('TWO WHEELS. ENDLESS POSSIBILITIES.','{{ site.bike_eyebrow }}'),
 ('Find your kind<br>of <em>freedom.</em>','{{ site.bike_title }}<br><em>{{ site.bike_accent }}</em>'),
 ('Daily commutes, city escapes, and everything in between.<br>Start with the way you want to ride.','{{ site.bike_intro }}'),
 ('{{ bike.description }}','{{ bike.description[:160] }}{{ "…" if bike.description|length > 160 }}'),
 ('<a class="btn ghost" href="{{ bike.source }}" target="_blank" rel="noopener noreferrer">Specs & current price ↗</a>', '<div class="price">{{ bike.price or "Price unavailable" }}</div><a class="btn ghost" href="{{ url_for(\'bike_detail\',model_no=bike.model_no) }}">View specifications →</a>'),
 ('An editorial selection, with specifications and current prices on BikeWale. Source links checked 25 September 2026.','Prices are indicative ex-showroom prices. Check each model for its last import date.'),
 ('READ BEFORE YOU RIDE','{{ site.journal_banner_eyebrow }}'),
 ('A little research. A better decision.','{{ site.journal_banner_title }}'),
 ('Explore our buying guides and ownership explainers.','{{ site.journal_banner_text }}'),
])
edit('car.html',[
 ('{% if d.brochure_url %}<a class="btn ghost" href="{{ d.brochure_url }}" target="_blank" rel="noopener">📄 Brochure</a>{% endif %}','<a class="btn ghost" href="#specs">View specifications ↓</a>'),
 ('*Ex-showroom price, New Delhi','Indicative ex-showroom price. Prices vary by city and variant.'),
 ('Data source: CarDekho · Last updated','Last imported'),
 ('On-road (Delhi)','On-road (if available)'),
])
edit('admin_post_form.html',[
 ("{% extends 'base.html' %}","{% extends 'admin_base.html' %}"),
 ('{% block content %}','{% block admin_content %}'),
 ('<form method="POST" class="panel form-grid">','<form method="POST" enctype="multipart/form-data" class="admin-panel form-grid"><input type="hidden" name="_csrf" value="{{ csrf_token() }}">'),
 ('<label>Cover image URL\n            <input type="text" name="cover_image" value="{{ post.cover_image if post else \'\' }}" placeholder="https://...">\n        </label>', '<label>Cover image (upload a JPG, PNG or WebP)<input type="file" name="cover_upload" accept="image/jpeg,image/png,image/webp"></label><p class="muted small">Leave blank to keep the existing image.</p>'),
 ("{{ url_for('admin') }}#blog", "{{ url_for('admin',tab='blog') }}"),
])
