#!/usr/bin/env python3
"""Fill Data Safety CSV for Roomfit Studio."""
import csv
from pathlib import Path

SRC = Path(__file__).parent / "data-safety-sample.csv"
DST = Path(__file__).parent / "data-safety-roomfit-studio.csv"

TYPE_PURPOSES = {
    "PSL_NAME": ["PSL_APP_FUNCTIONALITY", "PSL_ACCOUNT_MANAGEMENT"],
    "PSL_EMAIL": ["PSL_APP_FUNCTIONALITY", "PSL_ACCOUNT_MANAGEMENT"],
    "PSL_USER_ACCOUNT": ["PSL_APP_FUNCTIONALITY", "PSL_ACCOUNT_MANAGEMENT"],
    "PSL_PHONE": ["PSL_APP_FUNCTIONALITY", "PSL_ACCOUNT_MANAGEMENT"],
    "PSL_FITNESS": ["PSL_APP_FUNCTIONALITY"],
    "PSL_USER_INTERACTION": ["PSL_ANALYTICS"],
    "PSL_CRASH_LOGS": ["PSL_ANALYTICS", "PSL_FRAUD_PREVENTION_SECURITY"],
    "PSL_PERFORMANCE_DIAGNOSTICS": ["PSL_ANALYTICS"],
}
ENABLED = set(TYPE_PURPOSES.keys())
DISABLE_FROM_SAMPLE = {"PSL_APPROX_LOCATION"}

TOP_LEVEL = {
    ("PSL_DATA_COLLECTION_COLLECTS_PERSONAL_DATA", ""): "true",
    ("PSL_DATA_COLLECTION_ENCRYPTED_IN_TRANSIT", ""): "true",
    ("PSL_SUPPORTED_ACCOUNT_CREATION_METHODS", "PSL_ACM_USER_ID_PASSWORD"): "",
    ("PSL_SUPPORTED_ACCOUNT_CREATION_METHODS", "PSL_ACM_USER_ID_OTHER_AUTH"): "true",
    ("PSL_SUPPORTED_ACCOUNT_CREATION_METHODS", "PSL_ACM_USER_ID_PASSWORD_OTHER_AUTH"): "",
    ("PSL_SUPPORTED_ACCOUNT_CREATION_METHODS", "PSL_ACM_OAUTH"): "",
    ("PSL_SUPPORTED_ACCOUNT_CREATION_METHODS", "PSL_ACM_OTHER"): "",
    ("PSL_SUPPORTED_ACCOUNT_CREATION_METHODS", "PSL_ACM_NONE"): "",
    ("PSL_SUPPORT_DATA_DELETION_BY_USER", "DATA_DELETION_YES"): "",
    ("PSL_SUPPORT_DATA_DELETION_BY_USER", "DATA_DELETION_NO"): "true",
    ("PSL_SUPPORT_DATA_DELETION_BY_USER", "DATA_DELETION_NO_AUTO_DELETED"): "",
    ("PSL_ACCOUNT_DELETION_URL", ""): "https://wespion.notion.site/delete-account",
    ("PSL_DATA_DELETION_URL", ""): "",
    ("PSL_ACM_SPECIFY", ""): "",
}

DATA_TYPE_Q_PREFIXES = (
    "PSL_DATA_TYPES_",
)

def usage_value(data_type: str, q_id: str, r_id: str) -> str:
    """Return the Response value for a usage row of an enabled data type."""
    if q_id.endswith("PSL_DATA_USAGE_COLLECTION_AND_SHARING"):
        return "true" if r_id == "PSL_DATA_USAGE_ONLY_COLLECTED" else ""
    if q_id.endswith("PSL_DATA_USAGE_EPHEMERAL"):
        return "false"
    if q_id.endswith("DATA_USAGE_USER_CONTROL"):
        return "true" if r_id == "PSL_DATA_USAGE_USER_CONTROL_REQUIRED" else ""
    if q_id.endswith("DATA_USAGE_COLLECTION_PURPOSE"):
        return "true" if r_id in TYPE_PURPOSES[data_type] else ""
    if q_id.endswith("DATA_USAGE_SHARING_PURPOSE"):
        return ""
    return ""

def process():
    with SRC.open(encoding="utf-8", newline="") as f:
        reader = csv.reader(f)
        rows = list(reader)

    header, *data = rows
    out = [header]
    changed = 0

    for row in data:
        q_id, r_id, val, req, label = (row + ["", "", "", "", ""])[:5]
        new_val = val

        # Top-level overrides
        if (q_id, r_id) in TOP_LEVEL:
            new_val = TOP_LEVEL[(q_id, r_id)]
        # Data type declarations
        elif q_id.startswith("PSL_DATA_TYPES_"):
            if r_id in ENABLED:
                new_val = "true"
            elif r_id in DISABLE_FROM_SAMPLE:
                new_val = ""
            # Others keep sample default (usually empty)
        # Usage rows for enabled types
        elif q_id.startswith("PSL_DATA_USAGE_RESPONSES:"):
            parts = q_id.split(":")
            if len(parts) >= 3:
                dtype = parts[1]
                if dtype in ENABLED:
                    new_val = usage_value(dtype, q_id, r_id)
                else:
                    # Clear usage rows for disabled types (e.g., APPROX_LOCATION sample had NAME usage set)
                    new_val = ""

        if new_val != val:
            changed += 1
        out.append([q_id, r_id, new_val, req, label])

    with DST.open("w", encoding="utf-8", newline="") as f:
        writer = csv.writer(f)
        writer.writerows(out)

    print(f"Wrote {DST} ({len(out)-1} rows, {changed} modified)")

if __name__ == "__main__":
    process()
