#!/usr/bin/env python3
"""
G3 — de-gender the Desire Sync pool.

Desire Sync uses the binary (yes/no) questions in the `sexual_preferences`
category. They ship as paired female/male versions ("Do you want him to…" /
"Do you want her to…"), which is unusable for same-sex couples and alienating
generally. This collapses each pair into ONE neutral question:

  * female binary  -> text de-gendered to "your partner" phrasing, sex='neutral'
  * male   binary  -> deleted (now redundant)

It edits BOTH the source JSON (seed/questions/sexual_preferences.json) and the
shipped asset DB (app/src/main/assets/database/app.db) so they stay in sync.
It only touches row data — never the schema — so Room's identity hash is safe.
build_db.py is NOT run.

This is a one-off migration kept in the repo for traceability.
"""
import json
import os
import sqlite3

HERE = os.path.dirname(os.path.abspath(__file__))
JSON_PATH = os.path.join(HERE, "questions", "sexual_preferences.json")
DB_PATH = os.path.join(HERE, "..", "app", "src", "main", "assets", "database", "app.db")

# The 41 female binary questions that carry gendered pronouns, rewritten neutrally.
# (The other 59 binary questions are already gender-neutral and only need sex='neutral'.)
REWRITES = {
    # yes/no questions
    "sexual_preferences_female_001": "Do you want your partner to initiate sex more often?",
    "sexual_preferences_female_007": "Do you like being teased before your partner touches you directly?",
    "sexual_preferences_female_016": "Do you like dirty talk from your partner?",
    "sexual_preferences_female_019": "Do you want more oral sex from your partner?",
    "sexual_preferences_female_025": "Do you have a fantasy you want to tell your partner?",
    "sexual_preferences_female_031": "Do you want your partner to go down on you more often?",
    "sexual_preferences_female_034": "Do you like your partner using their fingers inside you during foreplay?",
    "sexual_preferences_female_043": "Do you want your partner to be more dominant in bed?",
    "sexual_preferences_female_052": "Do you like your partner making you wait before giving you what you want?",
    "sexual_preferences_female_055": "Do you want your partner to care more about your orgasm?",
    "sexual_preferences_female_058": "Do you want to use sex toys with your partner?",
    "sexual_preferences_female_067": "Do you want to try roleplay with your partner?",
    "sexual_preferences_female_070": "Do you like wearing lingerie to tease your partner?",
    "sexual_preferences_female_073": "Do you like receiving dirty texts from your partner?",
    "sexual_preferences_female_085": "Do you want your partner to ask about your fantasies directly?",
    "sexual_preferences_female_088": "Do you like your partner watching you touch yourself?",
    "sexual_preferences_female_097": "Do you want to talk openly about where your partner finishes?",
    "sexual_preferences_female_106": "Do you want your partner to stop immediately if sex hurts, even a little?",
    "sexual_preferences_female_121": "Do you want your partner to spend more time on clitoral stimulation?",
    "sexual_preferences_female_133": "Do you want your partner to compliment specific body parts more?",
    "sexual_preferences_female_136": "Do you want your partner to be more direct when they want sex?",
    "sexual_preferences_female_139": "Do you want your partner to handle rejection without sulking?",
    "sexual_preferences_female_145": "Do you trust your partner with your sexual boundaries?",
    "sexual_preferences_female_148": "Do you want your partner to ask you exactly what you want tonight?",
    # true/false (agree/disagree) statements
    "sexual_preferences_female_002": "I get turned on faster when my partner touches me before trying to have sex.",
    "sexual_preferences_female_005": "I want my partner to ask what feels good instead of guessing.",
    "sexual_preferences_female_008": "I like when my partner tells me exactly what they want to do to me.",
    "sexual_preferences_female_020": "I want to guide my partner's hands or mouth without them getting offended.",
    "sexual_preferences_female_023": "I like when my partner makes me feel chased and wanted.",
    "sexual_preferences_female_026": "A clear yes from me should matter more than my partner's assumptions.",
    "sexual_preferences_female_032": "I want my partner to focus on my clitoris more directly.",
    "sexual_preferences_female_035": "I want my partner to ask before changing speed or pressure.",
    "sexual_preferences_female_038": "I like when my partner starts shallow before going deeper.",
    "sexual_preferences_female_050": "I get turned on when my partner says I am doing a good job.",
    "sexual_preferences_female_053": "Teasing is hotter when my partner still checks that I am enjoying it.",
    "sexual_preferences_female_056": "I would rather my partner ask what helps me finish than pretend they know.",
    "sexual_preferences_female_071": "I want my partner to make me feel sexy before expecting me to perform.",
    "sexual_preferences_female_086": "I have at least one fantasy I have not fully explained to my partner.",
    "sexual_preferences_female_098": "Where my partner finishes should be discussed before the moment.",
    "sexual_preferences_female_119": "I want my partner to ask how sensitive my breasts or nipples are that day.",
    "sexual_preferences_female_149": "I would rather be asked bluntly than have my partner guess badly.",
}


def is_binary(answer_config):
    """True if the answer config is exactly a yes/no choice."""
    opts = (answer_config or {}).get("options") or []
    ids = {o.get("id") for o in opts}
    return ids == {"yes", "no"} or ids == {"true", "false"}


def neutral_tags(tags):
    """Drop the sex:* tag; the question is no longer gendered."""
    return [t for t in (tags or []) if not t.startswith("sex:")]


def migrate_json():
    with open(JSON_PATH) as f:
        data = json.load(f)
    items = data["questions"]

    kept = []
    female_updated = male_deleted = 0
    for q in items:
        binary = q.get("type") == "single_choice" and is_binary(q.get("answer_config"))
        sex = q.get("sex")
        if binary and sex == "male":
            male_deleted += 1
            continue  # drop the redundant gendered duplicate
        if binary and sex == "female":
            q["text"] = REWRITES.get(q["id"], q["text"])
            q["sex"] = "neutral"
            q["tags"] = neutral_tags(q.get("tags"))
            female_updated += 1
        kept.append(q)

    data["questions"] = kept
    with open(JSON_PATH, "w") as f:
        json.dump(data, f, indent=2, ensure_ascii=False)
        f.write("\n")
    print(f"JSON: {female_updated} female->neutral, {male_deleted} male deleted, {len(kept)} total")


def migrate_db():
    con = sqlite3.connect(DB_PATH)
    cur = con.cursor()
    rows = cur.execute(
        "SELECT id, sex, answer_config FROM question WHERE category_id='sexual_preferences'"
    ).fetchall()

    female_updated = male_deleted = 0
    for qid, sex, cfg_text in rows:
        try:
            cfg = json.loads(cfg_text)
        except (json.JSONDecodeError, TypeError):
            continue
        # DB answer_config is wrapped: {"type":..., "config": {"options": [...]}}
        inner = cfg.get("config", cfg)
        if not is_binary(inner):
            continue
        if sex == "male":
            cur.execute("DELETE FROM question WHERE id=?", (qid,))
            male_deleted += 1
        elif sex == "female":
            new_text = REWRITES.get(qid)
            if new_text is not None:
                cur.execute("UPDATE question SET text=?, sex='neutral' WHERE id=?", (new_text, qid))
            else:
                cur.execute("UPDATE question SET sex='neutral' WHERE id=?", (qid,))
            female_updated += 1
    con.commit()
    con.close()
    print(f"DB:   {female_updated} female->neutral, {male_deleted} male deleted")


if __name__ == "__main__":
    migrate_json()
    migrate_db()
