#!/usr/bin/env python3
"""Seed and maintain the client to-do list and the questions for Adam and Mark.

Idempotent and authoritative for the seeded rows: an item is matched on (owner, title);
present rows have their detail, due, kind, accepts_files and sort refreshed (status is
left alone unless the seed says `waiting`); titles in REMOVED are deleted; anything the
client or rian added by hand on the page is untouched. Run inside the container:
    docker exec -i dutyfreeprofessor-app python - < main/scripts/seed-todos.py
"""
import sys
sys.path.insert(0, "/srv/app")
from sqlalchemy import select  # noqa: E402

from app.db import SessionLocal  # noqa: E402
from app.models.client import ClientTodo  # noqa: E402

# (owner, title, detail, due, sort, kind, accepts_files, status)
ITEMS = [
    ("mark", "Comments on the site structure proposal",
     "The /structure page. Anything you would change in the page types, the addresses, or the markup plan. Comment on the page itself, or here.",
     "Thu 10 Sep", 10, "todo", False, "open"),
    ("adam", "Articles for the soft launch: final text with images",
     "Ten or more. Hand each article in here as Word, Markdown or text, with the title as the note. Put the images for them in the shared Drive folder, named to match the article.",
     "Tue 8 Sep", 20, "todo", True, "open"),
    ("adam", "The email platform to use for subscribers",
     "Name the service and confirm rian can be given access.",
     "Tue 8 Sep", 40, "todo", False, "open"),
    ("adam", "Official product images from the brands",
     "For the beauty list rian sends you. Put them in the shared Drive folder as they arrive, one file per product, named by brand and product.",
     "Fri 11 Sep", 80, "todo", False, "open"),
    ("adam", "Airport write-ups",
     "The duty free situation at the airport and nothing else, per Mark's review: where the duty free shops are, by terminal; any specialty shops (a dedicated liquor or perfume store, a walk-through); perhaps a map, or a link to the airport's own with the shops marked. Succinct, and nothing about the airport that is not about duty free: no lounges, no transport, no filler. Start with Heathrow, JFK, Hong Kong, Buenos Aires and Paris. One file per airport, Word or Markdown.",
     "Fri 11 Sep", 90, "todo", True, "open"),
    ("adam", "One paragraph per category",
     "200 to 300 words on what the category is and what to look for when buying: whisky, gin, vodka, rum, tequila, cognac, champagne, wine, liqueurs, fragrance, cosmetics. One file, or one per category.",
     "Fri 11 Sep", 100, "todo", True, "open"),
    ("adam", "Subscriber form fields, confirmed",
     "First name, last name, email, home airport, interests. Add or remove in a comment before the form is built.",
     "Fri 11 Sep", 110, "todo", False, "open"),
    ("adam", "Sponsor creative",
     "Not ready for you yet. The banner positions are being designed into the pages now (one per page type, sparse, as you asked). "
     "Once the placements are settled we tell you the exact sizes and you send the content: static images with the link each should open, no scripts.",
     None, 70, "todo", False, "waiting"),
    ("both", "Instagram and YouTube links for the header and footer",
     "The account URLs to link to, in a comment.",
     "Fri 11 Sep", 130, "todo", False, "open"),
    ("adam", "How careful do we stay with the shops whose hosting blocks our reader?",
     "Several shops now block our reader specifically. Not the shops themselves: their published rules say crawlers are welcome. It is the protection services in front of them, Akamai at Dubai and the platform protection at the four Shopify shops, that turn us away, and together those five hold well over half of the launch catalogue. "
     "We identify ourselves honestly and do not try to hide, and my position is to keep that up through the soft launch, take the hit on product counts, and give you a completely clean catalogue to demo. Once you have shown it and have a feel for the reaction, we can make judgment calls, and the conversations you have with the retailers are the real way back in. "
     "One thread worth pulling as you build those relationships: Duty Free Hunter's own material suggests they have some route to retailer data, and it is worth finding out what it is. Tell me if you see this differently.",
     "Before Cannes", 200, "question", False, "open"),
]

REMOVED = [
    ("adam", "The domain to soft launch on"),                      # decided: dutyfreeprofessor.com
    ("adam", "The list of twenty plus airports, in priority order"),  # decided: nineteen
    ("adam", "Priority brands for brand pages, and what belongs on a brand page"),  # covered by the SEO proposal
    ("adam", "Sponsor creative in your standard sizes"),           # replaced by the waiting item
    ("adam", "Open the Dubai Duty Free conversation"),             # folded into the question
]

with SessionLocal() as db:
    added = updated = removed = 0
    for owner, title in REMOVED:
        row = db.scalar(select(ClientTodo).where(ClientTodo.owner == owner, ClientTodo.title == title))
        if row is not None:
            db.delete(row); removed += 1
    for owner, title, detail, due, sort, kind, accepts_files, status in ITEMS:
        row = db.scalar(select(ClientTodo).where(ClientTodo.owner == owner, ClientTodo.title == title))
        if row is None:
            db.add(ClientTodo(owner=owner, title=title, detail=detail, due=due, sort=sort, kind=kind,
                              accepts_files=accepts_files, status=status)); added += 1
        else:
            row.detail, row.due, row.sort, row.kind, row.accepts_files = detail, due, sort, kind, accepts_files
            if status == "waiting" or row.status == "waiting":
                row.status = status
            updated += 1
    db.commit()
    print(f"todos: {added} added, {updated} refreshed, {removed} removed")
