"""Clear the last Persian text out of the English site.

Two things survived the translation patch because neither is visible on
the page:

  1. products_fitting.meta_description - still the Persian copy, which
     ends "official importer of TUPY in Iran". Invisible on the page,
     but it is exactly what Google prints under the result.
  2. blog_blogcomments - real comments left by Iranian visitors on
     tupybrazil.ir. They came across in the dump.

The comments are written out to comments-removed.json before they go, so
nothing is actually lost; the Persian site still has the originals too.
"""

import json
import os
import re

import django

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tupy.settings")
django.setup()

from blog.models import BlogComments
from products.models import Fitting

APP = "/home/abmtgrou/en.tupyfittings.com"
PERSIAN = re.compile("[\u0600-\u06FF]")
FINISH = {"gal": "hot-dip galvanised", "blk": "black", "both": "galvanised or black"}

report = []

# ---- 1. meta descriptions ------------------------------------------------
n = 0
samples = []
for f in Fitting.objects.all().order_by("order", "figure"):
    if f.category == "pres":
        d = "%s, TUPY Brazil figure %s. TUPYPRES press-fit system, %s. No threading, no welding. Stocked in Dubai for the Middle East." % (
            f.name, f.figure, f.sizes)
    else:
        d = "%s, TUPY Brazil figure %s. Malleable cast iron to %s, %s, %s. Stocked in Dubai for the Middle East." % (
            f.name, f.figure, f.standard, f.sizes, FINISH.get(f.finish, "galvanised or black"))
    f.meta_description = d[:300]
    f.save(update_fields=["meta_description"])
    n += 1
    if len(samples) < 3:
        samples.append("%d chars | %s" % (len(d), d))
report.append("meta descriptions rewritten: %d" % n)
report.extend(samples)

# ---- 2. anything else still Persian --------------------------------------
left = []
for f in Fitting.objects.all():
    for field in ("name", "intro", "description", "sizes", "standard", "thread",
                  "pressure", "seo_title", "meta_description"):
        v = getattr(f, field) or ""
        if PERSIAN.search(str(v)):
            left.append("%s.%s" % (f.slug, field))
report.append("fitting fields still Persian: %d %s" % (len(left), left[:8]))

# ---- 3. comments ---------------------------------------------------------
rows = [
    {"id": c.id, "blog_id": c.blog_id, "name": c.name, "email": c.email,
     "comment": c.comment, "reply": c.replay,
     "created_at": c.created_at.isoformat() if c.created_at else None}
    for c in BlogComments.objects.all()
]
with open(os.path.join(APP, "comments-removed.json"), "w") as fh:
    json.dump(rows, fh, ensure_ascii=False, indent=1)
BlogComments.objects.all().delete()
report.append("comments saved to comments-removed.json and removed: %d" % len(rows))

out = "\n".join(report)
open(os.path.join(APP, "fix-persian.out"), "w").write(out + "\n")
print(out)