#!/usr/bin/env python3
"""
Genera copy completo para todos los shop products del catálogo.
Usa alias-matrix.json como motor de posicionamiento.

Output: database/seeders/products/shop-copy.json
        {slug: {headline, subheadline, description, keywords, modules_pitched}}

Uso: python3 scripts/generate-shop-copy.py [--dry-run]
"""

import json, os, sys

DRY_RUN = "--dry-run" in sys.argv

# ──────────────────────────────────────────────────────────────
# Datos base
# ──────────────────────────────────────────────────────────────

with open("database/seeders/products/catalog.json")       as f: CATALOG = json.load(f)
with open("database/seeders/products/alias-matrix.json")  as f: MATRIX  = json.load(f)

CORE_FILES = {}
for fn in os.listdir("database/seeders/products/core"):
    if fn.endswith(".json") and fn != "seeds":
        core = fn.replace(".json", "")
        with open(f"database/seeders/products/core/{fn}") as f:
            CORE_FILES[core] = json.load(f)

# ──────────────────────────────────────────────────────────────
# Mapeo core → industria principal
# ──────────────────────────────────────────────────────────────
CORE_TO_INDUSTRY = {
    "insurance-advisor":   "health-medical",
    "nutritionist":        "health-medical",
    "law-firm-digital":    "law-corporate",
    "real-estate":         "real-estate",
    "financial-wealth":    "finance",
    "restaurant-bar":      "retail-gastro",
    "concierge":           "retail-gastro",
    "photography":         "art-design",
    "art-design":          "art-design",
    "personal-brand":      "personal",
    "foundations-ong":     "institutional",
    "agency":              "tech-saas",
    "website-reseller":    "tech-saas",
    "business-catalogue":  "retail-gastro",
    "catalogue-ai":        "retail-gastro",
    "construction":        "art-design",    # construction/architecture is creative
    "corporative":         "institutional",
    # Generic cores: industry determined by shop slug keywords
    "standard-website":    None,
    "bp-dinamic":          None,
    "petite-website":      "personal",
    "sitio-web-profesional": None,
}

# For generic cores, keyword → industry
SLUG_KEYWORD_TO_INDUSTRY = {
    # Health
    "dental": "health-medical", "medical": "health-medical",
    "veterinary": "health-medical", "optometry": "health-medical",
    "pharmacy": "health-medical", "physio": "health-medical",
    "therapy": "health-medical", "psychology": "health-medical",
    "dermatology": "health-medical", "health": "health-medical",
    "clinic": "health-medical", "wellness": "health-medical",
    "chiropractor": "health-medical", "naturopath": "health-medical",
    "herbalist": "health-medical",
    # Law
    "law": "law-corporate", "attorney": "law-corporate",
    "legal": "law-corporate", "notary": "law-corporate",
    # Finance
    "financial": "finance", "accounting": "finance",
    "tax": "finance", "wealth": "finance", "invest": "finance",
    "mortgage": "finance", "insurance": "finance",
    "hr-consulting": "finance", "management-consulting": "finance",
    "consulting": "finance", "business-consultant": "finance",
    # Tech
    "it-consulting": "tech-saas", "tech": "tech-saas",
    "digital": "tech-saas", "marketing": "tech-saas",
    "agency": "tech-saas", "seo": "tech-saas", "branding": "tech-saas",
    "communications": "tech-saas", "content": "tech-saas",
    "social-media": "tech-saas", "advertising": "tech-saas",
    "pr-agency": "tech-saas", "creative-agency": "tech-saas",
    # Gastro / Retail
    "restaurant": "retail-gastro", "cafe": "retail-gastro",
    "food": "retail-gastro", "bakery": "retail-gastro",
    "catering": "retail-gastro", "bar": "retail-gastro",
    "clothing": "retail-gastro", "boutique": "retail-gastro",
    "furniture": "retail-gastro", "electronics": "retail-gastro",
    "hardware": "retail-gastro", "retail": "retail-gastro",
    "wholesale": "retail-gastro",
    # Real estate
    "real-estate": "real-estate", "property": "real-estate",
    "luxury-real": "real-estate", "vacation-rental": "real-estate",
    # Art / Design
    "architect": "art-design", "artist": "art-design",
    "tattoo": "art-design", "ceramic": "art-design",
    "jewelry": "art-design", "fashion": "art-design",
    "muralist": "art-design", "calligrapher": "art-design",
    "sculptor": "art-design", "illustrator": "art-design",
    "graphic": "art-design", "interior-design": "art-design",
    "painting": "art-design", "web-designer": "art-design",
    "creative-studio": "art-design", "art-studio": "art-design",
    # Personal
    "freelancer": "personal", "personal-brand": "personal",
    "handyman": "personal", "local-service": "personal",
    "portfolio": "personal", "author": "personal",
    "speaker": "personal", "personal-coach": "personal",
    "photographer": "art-design", "videographer": "art-design",
    "wedding-photo": "art-design", "commercial-photo": "art-design",
    # Institutional / Corporate
    "corporate": "institutional", "brand-website": "institutional",
    "ngo": "institutional", "nonprofit": "institutional",
    "charity": "institutional", "community": "institutional",
    "foundation": "institutional", "environmental": "institutional",
    "cultural": "institutional", "social-foundation": "institutional",
    # Hotel / Tourism
    "hotel": "retail-gastro", "boutique-hotel": "retail-gastro",
    "bed-breakfast": "retail-gastro", "spa": "retail-gastro",
    "travel": "retail-gastro", "event-venue": "retail-gastro",
    "tourism": "retail-gastro",
}

INDUSTRY_LABELS = {
    "law-corporate":  "despacho de abogados",
    "health-medical": "clínica y salud",
    "real-estate":    "inmobiliaria",
    "retail-gastro":  "gastronomía y retail",
    "tech-saas":      "agencia digital",
    "art-design":     "arte y diseño",
    "institutional":  "organización y empresa",
    "finance":        "finanzas e inversiones",
    "personal":       "profesional independiente",
}

INDUSTRY_TARGET = {
    "law-corporate":  "para abogados y firmas legales",
    "health-medical": "para clínicas y profesionales de la salud",
    "real-estate":    "para inmobiliarias y agentes",
    "retail-gastro":  "para restaurantes y negocios gastronómicos",
    "tech-saas":      "para agencias y empresas tech",
    "art-design":     "para artistas y creativos",
    "institutional":  "para empresas e instituciones",
    "finance":        "para asesores financieros e inversores",
    "personal":       "para freelancers y profesionales",
}

# Headline templates by industry (for Spanish copy)
HEADLINE_TEMPLATES = {
    "law-corporate":  "Tu estudio jurídico con presencia digital profesional — listo en 48hs",
    "health-medical": "Tu clínica o consultorio con sitio web profesional — listo en 48hs",
    "real-estate":    "Tu inmobiliaria con presencia digital completa — listo en 48hs",
    "retail-gastro":  "Tu negocio gastronómico con sitio web profesional — listo en 48hs",
    "tech-saas":      "Tu agencia o empresa tech con web profesional — listo en 48hs",
    "art-design":     "Tu proyecto creativo con sitio web profesional — listo en 48hs",
    "institutional":  "Tu empresa o institución con presencia digital — listo en 48hs",
    "finance":        "Tu servicio financiero con sitio web profesional — listo en 48hs",
    "personal":       "Tu perfil profesional con sitio web propio — listo en 48hs",
}

def get_industry(shop_slug, core_slug):
    industry = CORE_TO_INDUSTRY.get(core_slug)
    if industry:
        return industry
    # Fallback: keyword matching on shop slug
    slug_lower = shop_slug.lower()
    for kw, ind in SLUG_KEYWORD_TO_INDUSTRY.items():
        if kw in slug_lower:
            return ind
    return "institutional"  # safe default

def slugify_name(slug):
    """Convert shop slug to display name."""
    return slug.replace("-", " ").title()

def get_module_aliases(modules, industry):
    """Get alias names for each module in a core preset for the given industry."""
    aliases = []
    for mod in modules:
        mod_data = MATRIX.get(mod, {})
        ind_data = mod_data.get(industry, {})
        alias = ind_data.get("alias")
        if alias:
            aliases.append(alias)
    return aliases

def get_module_pitches(modules, industry):
    """Get pitches for primary modules."""
    pitches = []
    for mod in modules[:3]:  # top 3 modules
        mod_data = MATRIX.get(mod, {})
        ind_data = mod_data.get(industry, {})
        pitch = ind_data.get("pitch")
        if pitch:
            pitches.append(pitch)
    return pitches

def generate_keywords(shop_slug, core_slug, industry, modules):
    """Generate SEO keywords for a shop product."""
    product_name = slugify_name(shop_slug)
    industry_label = INDUSTRY_LABELS.get(industry, industry)
    core = CORE_FILES.get(core_slug, {})
    demo = core.get("demo", "")

    base_keywords = [
        f"sitio web {product_name.lower()}",
        f"página web {product_name.lower()}",
        f"web profesional {industry_label}",
        f"diseño web {product_name.lower()}",
        f"{product_name.lower()} online",
        f"presencia digital {product_name.lower()}",
        "bewpro",
        f"web {product_name.lower()} argentina",
    ]
    return ", ".join(base_keywords[:6])

def generate_copy(shop_slug, entry):
    core_slug = entry["core"]
    product_name = entry.get("name", slugify_name(shop_slug))
    core_data = CORE_FILES.get(core_slug, {})
    modules = core_data.get("modules", [])
    industry = get_industry(shop_slug, core_slug)
    target = INDUSTRY_TARGET.get(industry, "para profesionales")

    aliases = get_module_aliases(modules, industry)
    pitches = get_module_pitches(modules, industry)

    # Headline: industry-specific with product name
    base_headline = HEADLINE_TEMPLATES.get(industry, "Sitio web profesional — listo en 48hs")
    headline = f"{product_name} — {base_headline.split('—')[1].strip()}"

    # Subheadline: value stack with module aliases
    if aliases:
        modules_text = " · ".join(aliases[:4])
        subheadline = f"Incluye: {modules_text}. Sin código, sin servidores, sin complicaciones."
    else:
        subheadline = f"Presencia digital completa {target}. Sin código, sin complicaciones."

    # Description: pitch of the top module + context
    if pitches:
        description = pitches[0]
    else:
        description = f"Sitio web diseñado específicamente {target}. Sistema base + módulos funcionales listos para publicar."

    keywords = generate_keywords(shop_slug, core_slug, industry, modules)

    return {
        "headline": headline,
        "subheadline": subheadline,
        "description": description,
        "keywords": keywords,
        "industry": industry,
        "modules": modules,
        "modules_aliased": aliases,
        "demo": core_data.get("demo", ""),
    }

# ──────────────────────────────────────────────────────────────
# Generar para todos los shop slugs
# ──────────────────────────────────────────────────────────────

results = {}
for slug, entry in CATALOG.items():
    if slug.startswith("_"):
        continue
    copy = generate_copy(slug, entry)
    results[slug] = copy

if DRY_RUN:
    # Show sample
    sample = list(results.items())[:5]
    for slug, data in sample:
        print(f"\n{'='*60}")
        print(f"SLUG: {slug} (core: {CATALOG[slug]['core']}, industry: {data['industry']})")
        print(f"H1:  {data['headline']}")
        print(f"H2:  {data['subheadline']}")
        print(f"KW:  {data['keywords']}")
        print(f"MOD: {' · '.join(data['modules_aliased'])}")
    print(f"\nTotal: {len(results)} shop products")
    sys.exit(0)

out_path = "database/seeders/products/shop-copy.json"
with open(out_path, "w", encoding="utf-8") as f:
    json.dump(results, f, ensure_ascii=False, indent=2)

# Stats
industries_dist = {}
for data in results.values():
    ind = data["industry"]
    industries_dist[ind] = industries_dist.get(ind, 0) + 1

print(f"\n✅ shop-copy.json generado: {len(results)} productos")
print("\nDistribución por industria:")
for ind, count in sorted(industries_dist.items(), key=lambda x: -x[1]):
    print(f"  {ind:20s}: {count:3d} productos")
print(f"\n  → {out_path}")
