#!/usr/bin/env python3
"""Update pages to use the full template with image section."""

import os
import re
import glob

# Image mapping for each product
IMAGE_MAP = {
    'พุกต่างๆ': 'ANCHOR_cover_01.jpg',
    'pipe': 'ppr-pipe_000C.jpg',
    'เม็กกรู๊ฟ-คับปลิ้ง': 'MECH_001.jpg',
    'อุปกรณ์ดับเพลิง': 'extinguishers_000C.jpg',
    'ฉนวนหุ้มท่อ-pipe-insulation': 'Thermobreak_-Raised-Floor-Insulation_0.png',
    'หัวจ่ายแอร์-ball-jet': 'balljet-cover_000C-768x1024.jpg',
    'น็อต-แหวน-สกรู-hex-nut-flat-washer-hexagon-head-screw': 'NUT_FLATWASHER_SCREW_cover_01.jpg',
    'สตัดเกลียวตลอด-เหล็ก-threaded-rod': 'aaa_aaa_aaaaea_ayauacaaa_a_-male-thread-connector.jpg',
    'ระบบรั้ว': 'Banner-vineman.jpg',  # Will use banner as fallback
    'แคล้มประกับ-ชุบรุ้ง-ชุบ': 'aayaaa_aaaaaaaaa-low-pipe-clamp.jpg',
    'dukelarrsen': 'dukelarrsen-01.jpg',
    'pp-r-pp-rct-poloplast': 'POLOPLAST_001.png',
    'ท่อพีพีอาร์': 'ppr-pipe_000C.jpg',
    'ยูโบลท์เหล็กแผ่น-ยูแบน-strap': 'STRAP_U-BOLT_cover_01.jpg',
    'เควิสแฮงเกอร์': 'ADJUSTABLE_CLEVIS_HANGER_cover_01.jpg',
    'รั้วเทวดา': 'Banner-TDV2.jpg',
    'แฮงเกอร์-แคล้ม-โบลท์-แหว': 'ADJUSTABLE_SPLIT_RING_HANGER_cover_01.jpg',
}

# Template for two-column layout
TEMPLATE_WITH_IMAGE = '''---
import BaseLayout from '@/layouts/BaseLayout.astro';
import Header from '@/components/common/Header.astro';
import Footer from '@/components/common/Footer.astro';
---

<BaseLayout title="{title}" description="{description}">
  <Header slot="header" />
  
  <main class="px-4 sm:px-6 lg:px-8 bg-gradient-to-br from-primary-50 via-white to-accent-50 min-h-screen">
    <section class="section">
      <div class="container-custom">
        <div class="grid lg:grid-cols-2 gap-12 items-start">
          <div class="sticky top-24">
            <div class="card p-8 bg-white">
              <img src="/images/products-misc/{image}" alt="{title}" class="w-full h-auto rounded-xl" onerror="this.src='/images/logo/dealplustech-logo.png'" />
            </div>
          </div>
          
          <div>
            <h1 class="text-4xl md:text-5xl font-bold text-secondary-900 mb-6">{title}</h1>
            <p class="text-xl text-secondary-600 mb-8 leading-relaxed">
              {description}
            </p>
            
            
    <div class="flex flex-wrap gap-4 mt-8">
      <a href="https://line.me/ti/p/~dealplustech" target="_blank" class="inline-flex items-center space-x-2 bg-[#06C755] hover:bg-[#05b34d] text-white px-6 py-3 rounded-xl font-medium transition-all hover:shadow-lg active:scale-95">
        <svg class="w-6 h-6" fill="currentColor" viewBox="0 0 24 24"><path d="M19.368 9.131c.896.541 1.532 1.394 1.532 2.516 0 2.059-1.944 3.274-4.268 3.274-.284 0-.554-.027-.824-.054l-1.56.793c-.868.441-1.179.274-1.179-.58v-.71c-2.892-.985-4.89-3.316-4.89-6.29 0-.653.068-1.279.191-1.891H4.796c-2.297 0-4.132 1.757-4.132 4.081 0 2.708 2.114 4.849 5.202 5.615l.027 1.333c0 .626.488.871.976.625l2.351-1.203c.642.177 1.311.273 2.007.273 3.351 0 5.966-1.73 5.966-4.356 0-.428-.068-.83-.19-1.218l2.365-2.204z"/></svg>
        <span>แชท Line</span>
      </a>
      <a href="tel:0905551415" class="inline-flex items-center space-x-2 bg-green-600 hover:bg-green-700 text-white px-6 py-3 rounded-xl font-medium transition-all hover:shadow-lg active:scale-95">
        <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 5a2 2 0 012-2h3.28a1 1 0 01.948.684l1.498 4.493a1 1 0 01-.502 1.21l-2.257 1.13a11.042 11.042 0 005.516 5.516l1.13-2.257a1 1 0 011.21-.502l4.493 1.498a1 1 0 01.684.949V19a2 2 0 01-2 2h-1C9.716 21 3 14.284 3 6V5z"/></svg>
        <span>090-555-1415</span>
      </a>
    </div>
    
            
            <div class="mt-12 space-y-4">
              <div class="flex items-start space-x-3">
                <svg class="w-6 h-6 text-green-600 mt-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" />
                </svg>
                <span class="text-base text-secondary-700">คุณภาพสูง มาตรฐานอุตสาหกรรม</span>
              </div>
              <div class="flex items-start space-x-3">
                <svg class="w-6 h-6 text-green-600 mt-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" />
                </svg>
                <span class="text-base text-secondary-700">ราคาโรงงาน คุ้มค่า</span>
              </div>
              <div class="flex items-start space-x-3">
                <svg class="w-6 h-6 text-green-600 mt-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" />
                </svg>
                <span class="text-base text-secondary-700">จัดส่งรวดเร็ว ส่งฟรี กทม. ปริมณฑล</span>
              </div>
              <div class="flex items-start space-x-3">
                <svg class="w-6 h-6 text-green-600 mt-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" />
                </svg>
                <span class="text-base text-secondary-700">รับประกันสินค้า</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>
    
    <section class="section gradient-bg">
      <div class="container-custom text-center">
        <h2 class="text-3xl md:text-4xl font-bold text-secondary-900 mb-6">สนใจสินค้าของเรา?</h2>
        <p class="text-xl text-secondary-600 mb-8 max-w-2xl mx-auto">
          ติดต่อเราวันนี้เพื่อรับคำแนะนำและราคาพิเศษ
        </p>
        <div class="flex flex-wrap justify-center gap-4">
          <a href="tel:0905551415" class="btn-primary text-lg px-8 py-4">โทรเลย: 090-555-1415</a>
          <a href="https://line.me/ti/p/~dealplustech" target="_blank" class="inline-flex items-center space-x-2 bg-[#06C755] hover:bg-[#05b34d] text-white px-8 py-4 rounded-xl font-medium transition-all hover:shadow-lg active:scale-95 text-lg">
            <span>แอด Line</span>
          </a>
        </div>
      </div>
    </section>
  </main>
  
  <Footer slot="footer" />
</BaseLayout>
'''

def get_title_and_desc(content):
    """Extract title and description from page."""
    # Get title from <title> tag
    title_match = re.search(r'<BaseLayout title="([^"]+)"', content)
    title = title_match.group(1) if title_match else "สินค้า"
    
    # Get description
    desc_match = re.search(r'description="([^"]+)"', content)
    description = desc_match.group(1) if desc_match else f"จำหน่าย {title} คุณภาพสูง ราคาโรงงาน ส่งฟรี กทม. ปริมณฑล"
    
    return title, description

def update_page(page_path, image_name):
    """Update a single page with the full template."""
    with open(page_path, 'r', encoding='utf-8') as f:
        content = f.read()
    
    title, description = get_title_and_desc(content)
    
    # Create new content with template
    new_content = TEMPLATE_WITH_IMAGE.format(
        title=title,
        description=description,
        image=image_name
    )
    
    with open(page_path, 'w', encoding='utf-8') as f:
        f.write(new_content)
    
    print(f"Updated: {page_path} -> {image_name}")

def main():
    base_dir = '/Users/kunthawatgreethong/Gitea/dealplustech-new/dealplustech-astro/src/pages'
    
    for slug, image in IMAGE_MAP.items():
        page_path = os.path.join(base_dir, slug, 'index.astro')
        if os.path.exists(page_path):
            update_page(page_path, image)
        else:
            print(f"NOT FOUND: {page_path}")

if __name__ == '__main__':
    main()
