"""
Stylist Engine — Pre-computes fashion matching for all products.
Reads fashion_rules.json → scores pairs → writes ai_matches to Postgres.

Usage:
  from worker.stylist_engine import StylistEngine
  engine = StylistEngine()
  result = engine.run_for_code("5TS25S021-SR079")   # 1 product
  engine.run_batch(limit=None)                        # all products
"""

import json
import logging
import os
import random
import sqlite3
import time
from typing import Optional

from common.constants import KID_SAFE_KEYWORDS, SQLITE_DB_PATH, SQLITE_GENDER_MAP, TABLE_OUTFIT_RULES, TABLE_PRODUCTS, normalize_gender

logger = logging.getLogger(__name__)

RULES_PATH = os.path.join(os.path.dirname(__file__), "fashion_rules.json")


# ═══ Batch state (used by API for polling) ═══
BATCH_STATE = {
    "is_running": False,
    "total": 0,
    "done": 0,
    "errors": 0,
    "current_code": None,
    "started_at": None,
    "elapsed_s": None,
}


class StylistEngine:
    """Core fashion matching engine."""

    def __init__(self):
        with open(RULES_PATH, "r", encoding="utf-8") as f:
            self.rules = json.load(f)
        self._catalog: list[dict] = []  # cached product catalog

    # ──────────────────────────────────────────
    # Public API
    # ──────────────────────────────────────────

    def run_for_code(self, code: str) -> dict:
        """Compute ai_matches for a single product code and save to DB."""
        catalog = self._get_catalog()
        source = next((p for p in catalog if p["code"] == code), None)
        if not source:
            logger.warning("[Stylist] Product not found in catalog: %s", code)
            return {}

        matches = self.compute_dynamic_rule_matches(code)
        self._save_matches(code, matches)
        logger.info("[Stylist] ✅ %s → %d matches across occasions", code, sum(
            len(items) for occ in matches.values() for items in occ.values()
        ))
        return matches

    def run_batch(self, codes: Optional[list[str]] = None) -> int:
        """Batch run for list of codes (or all if None). Updates BATCH_STATE."""
        global BATCH_STATE
        catalog = self._get_catalog(force_reload=True)

        targets = codes or [p["code"] for p in catalog]
        BATCH_STATE.update({
            "is_running": True,
            "total": len(targets),
            "done": 0,
            "errors": 0,
            "current_code": None,
            "started_at": time.time(),
            "elapsed_s": None,
        })

        count = 0
        for code in targets:
            BATCH_STATE["current_code"] = code
            try:
                matches = self.compute_dynamic_rule_matches(code)
                self._save_matches(code, matches)
                count += 1
                BATCH_STATE["done"] += 1
            except Exception as e:
                logger.error("[Stylist] Error for %s: %s", code, e)
                BATCH_STATE["errors"] += 1

        elapsed = round(time.time() - BATCH_STATE["started_at"], 1)
        BATCH_STATE.update({
            "is_running": False,
            "elapsed_s": elapsed,
            "current_code": None,
        })
        logger.info("[Stylist] ✅ Batch done: %d/%d in %.1fs", count, len(targets), elapsed)
        return count

    # ──────────────────────────────────────────
    # Core Matching Logic
    # ──────────────────────────────────────────

    def _compute_matches(self, source: dict, catalog: list[dict]) -> dict:
        """Compute matches for one source product across all occasions + roles based on DB rules.
        Falls back to fallback_occasion_rules in JSON when DB table is empty.
        NOTE: This method does NOT include rule_id (use compute_dynamic_rule_matches for DB saves).
        """
        rules = self.rules
        roles = rules["roles"]
        max_items = rules["role_max_items"]
        anchor_cat = source.get("product_line", "")
        gender = source.get("gender", "")

        # Fetch allowed mappings from DB
        db_rules = self._fetch_db_rules(anchor_cat, gender)
        allowed_by_occ = {}
        for r in db_rules:
            occ = r["occ"]
            tgt = r["target_cat"].lower()
            if occ not in allowed_by_occ:
                allowed_by_occ[occ] = set()
            allowed_by_occ[occ].add(tgt)

        if not allowed_by_occ:
            logger.warning("[Stylist] No rules for anchor '%s'", anchor_cat)
            return {}

        # Initialize buckets only for occasions that have rules
        buckets: dict[str, dict[str, list[dict]]] = {
            occ: {role: [] for role in roles} for occ in allowed_by_occ.keys()
        }

        for target in catalog:
            if target["code"] == source["code"]:
                continue
            if not self._pass_hard_filter(source, target):
                continue
            role = self._get_role(target)
            if not role:
                continue
            tgt_pl = target.get("product_line", "")
            if not tgt_pl:
                continue
            tgt_pl_lower = tgt_pl.lower()

            # Check each occasion that has rules for this anchor
            for occ, allowed_targets in allowed_by_occ.items():
                if tgt_pl_lower in allowed_targets:
                    score = self._score(source, target, occ)
                    if score < rules.get("min_score", 35):
                        continue
                    reason = self._build_reason(source, target, occ, score)
                    buckets[occ][role].append({
                        "code": target["code"],
                        "name": target["name"],
                        "color": target.get("color", ""),
                        "product_line": target.get("product_line", ""),
                        "image": target.get("image", ""),
                        "score": score,
                        "reason": reason,
                        "occasion": occ,
                        "rule_id": None,
                    })

        # Pick top N per occasion+role, sort by score
        result: dict[str, dict] = {}
        for occ, occ_data in buckets.items():
            final_occ_data = {}
            for role, items in occ_data.items():
                top = sorted(items, key=lambda x: -x["score"])
                top = self._deduplicate(top, max_items.get(role, 3))
                if top:
                    final_occ_data[role] = top
            if final_occ_data:
                result[occ] = final_occ_data

        return result







    def compute_dynamic_rule_matches(self, code: str) -> dict:
        """Dynamically compute matches based on chatbot_fashion_rules (DB) with color+material+role scoring."""
        catalog = self._get_catalog()
        source = next((p for p in catalog if p["code"] == code), None)
        if not source:
            logger.warning("[Stylist] Product not found in catalog: %s", code)
            return {}

        anchor_cat = source.get("product_line", "")
        gender = source.get("gender", "")

        # 1. Fetch Rules from DB: list of {occ, role, target_cat, ai_reason}
        db_rules = self._fetch_db_rules(anchor_cat, gender)
        if not db_rules:
            logger.info("[Stylist] No DB rules for %s, falling back to full compute", anchor_cat)
            return self._compute_matches(source, catalog)

        # Group by occasion for easier lookup
        rules_by_occ: dict[str, list[dict]] = {}
        for r in db_rules:
            if r["occ"] not in rules_by_occ:
                rules_by_occ[r["occ"]] = []
            rules_by_occ[r["occ"]].append(r)

        max_items = self.rules.get("role_max_items", {})
        buckets: dict[str, dict[str, list[dict]]] = {}

        # 2. Filter targets by DB rules and score
        for target in catalog:
            if target["code"] == source["code"]:
                continue
            if not self._pass_hard_filter(source, target):
                continue
            tgt_pl = target.get("product_line", "")
            if not tgt_pl:
                continue
            tgt_role = self._get_role(target)
            if not tgt_role:
                continue

            # Check each occasion's rules
            for occ, rules_list in rules_by_occ.items():
                for r in rules_list:
                    if tgt_pl.lower() == r["target_cat"].lower() and tgt_role == r["role"]:
                        # Compute score using new simplified scoring
                        score = self._score(source, target, occ)
                        if score < self.rules.get("min_score", 35):
                            continue

                        # Build reason: combine algorithm reason + DB ai_reason
                        algo_reason = self._build_reason(source, target, occ, score)
                        db_reason = r["ai_reason"] or f"Theo định hướng ({occ})"
                        combined_reason = f"{algo_reason} | {db_reason}"

                        breakdown = self._score_breakdown(source, target, occ)

                        if occ not in buckets:
                            buckets[occ] = {}
                        if tgt_role not in buckets[occ]:
                            buckets[occ][tgt_role] = []
                        buckets[occ][tgt_role].append({
                            "code": target["code"],
                            "name": target["name"],
                            "color": target.get("color", ""),
                            "product_line": target.get("product_line", ""),
                            "image": target.get("image", ""),
                            "score": score,
                            "reason": combined_reason,
                            "breakdown": breakdown,
                            "occasion": occ,
                            "rule_id": r["rule_id"],
                            "role": tgt_role,
                        })
                        break  # Found matching rule for this occasion, move to next occasion

        # 3. Sort and Deduplicate
        result: dict[str, dict] = {}
        for occ, occ_data in buckets.items():
            final_occ_data = {}
            for role, items in occ_data.items():
                top = sorted(items, key=lambda x: -x["score"])
                top = self._deduplicate(top, max_items.get(role, 3))
                if top:
                    final_occ_data[role] = top
            if final_occ_data:
                result[occ] = final_occ_data

        return result

    def _fetch_db_rules(self, anchor_cat: str, gender: str = "") -> list[dict]:
        """Fetch full rules including occasion, match_role, target_category, ai_reason, id from SQLite."""
        conn = None
        try:
            conn = sqlite3.connect(SQLITE_DB_PATH)
            cur = conn.cursor()

            gender_key = normalize_gender(gender)
            target_gender = SQLITE_GENDER_MAP.get(gender_key, "unisex")

            cur.execute(
                f"""SELECT id, occasion_tag as occasion, match_role, target_category, ai_reason
                   FROM {TABLE_OUTFIT_RULES}
                   WHERE UPPER(anchor_category) = UPPER(?)
                     AND (gender_target = ? OR gender_target = 'unisex' OR gender_target = 'all')""",
                (anchor_cat, target_gender)
            )

            rules = []
            for row in cur.fetchall():
                rules.append({
                    "rule_id": row[0],
                    "occ": row[1],
                    "role": row[2],
                    "target_cat": row[3],
                    "ai_reason": row[4] or "",
                })
            cur.close()
            return rules
        except Exception as e:
            logger.error("[Stylist] DB rule fetch error: %s", e)
            return []
        finally:
            if conn:
                conn.close()

    def compute_super_classifications_sql(self, code: str) -> dict:
        """
        Dynamically fetches top matches directly from DB using custom SQL queries 
        based on the super classification matrix (Color, Occasion, Material).
        """
        catalog = self._get_catalog()
        source = next((p for p in catalog if p["code"] == code), None)
        if not source:
            logger.warning("[Stylist] Product not found for SQL grouping: %s", code)
            return {}

        gender = source.get("gender", "")
        if not gender or gender.lower() == "unisex":
            gender = "women" # Default fallback for classification demo

        age_group = source.get("age_group", "").lower()

        # Smart target category filtering based on anchor category to avoid Top-Top or Bottom-Bottom match
        target_cats = set(["sơ mi", "chân váy", "quần âu", "quần jean", "áo phông", "quần soóc"])
        src_pl = source.get("product_line", "").lower()
        
        if "quần" in src_pl or "váy" in src_pl or "bộ" in src_pl:
            target_cats -= set(["chân váy", "quần âu", "quần jean", "quần soóc"])
        elif "áo" in src_pl:
            target_cats -= set(["sơ mi", "áo phông"])
        
        if not target_cats:
            target_cats = set(["áo phông", "quần soóc"]) # Fallback

        like_cond = " OR ".join([f"LOWER(product_line_vn) LIKE '%{c}%'" for c in target_cats])
        
        sr_db = get_db_connection()
        SR_TABLE = "shared_source.magento_product_dimension_with_text_embedding"
        
        # Color Group Queries
        color_groups = {
            "neutral": "LOWER(master_color) LIKE '%trắng%' OR LOWER(master_color) LIKE '%white%' OR LOWER(master_color) LIKE '%đen%' OR LOWER(master_color) LIKE '%black%' OR LOWER(master_color) LIKE '%xám%' OR LOWER(master_color) LIKE '%grey%' OR LOWER(master_color) LIKE '%be%' OR LOWER(master_color) LIKE '%beige%'",
            "light": "LOWER(master_color) LIKE '%vàng%' OR LOWER(master_color) LIKE '%hồng%' OR LOWER(master_color) LIKE '%xanh da trời%'",
            "dark": "LOWER(master_color) LIKE '%đỏ%' OR LOWER(master_color) LIKE '%cam%' OR LOWER(master_color) LIKE '%xanh lá cây%' OR LOWER(master_color) LIKE '%xanh jeans%' OR LOWER(master_color) LIKE '%xanh than%'"
        }
        
        # Material Season Queries
        mat_groups = {
            "summer": "LOWER(description_text_full) LIKE '%cotton%' OR LOWER(description_text_full) LIKE '%linen%' OR LOWER(description_text_full) LIKE '%mát%' OR LOWER(description_text_full) LIKE '%hè%'",
            "winter": "LOWER(description_text_full) LIKE '%nỉ%' OR LOWER(description_text_full) LIKE '%len%' OR LOWER(description_text_full) LIKE '%phao%' OR LOWER(description_text_full) LIKE '%chần bông%' OR LOWER(description_text_full) LIKE '%đông%'"
        }

        # Occasion Queries 
        # (Assuming Đi làm: sơ mi/quần âu/váy; Đi chơi: váy/áo phông/soóc)
        occ_groups = {
            "di_lam": "LOWER(product_line_vn) LIKE '%sơ mi%' OR LOWER(product_line_vn) LIKE '%chân váy%' OR LOWER(product_line_vn) LIKE '%quần âu%'",
            "di_choi": "LOWER(product_line_vn) LIKE '%váy%' OR LOWER(product_line_vn) LIKE '%áo phông%' OR LOWER(product_line_vn) LIKE '%quần soóc%'",
            "o_nha": "LOWER(product_line_vn) LIKE '%bộ mặc nhà%' OR LOWER(product_line_vn) LIKE '%quần lót%' OR LOWER(product_line_vn) LIKE '%áo lót%'"
        }

        results = {"color": {}, "occasion": {}, "material": {}}
        
        # Determine if source is kid
        src_name = source.get("name", "").lower()

        def check_is_kid():
            ag = age_group.lower()
            if "người lớn" in ag or "nguoi_lon" in ag or "nguoi lon" in ag: return False
            if "trẻ em" in ag or "kid" in ag or "tre_em" in ag: return True
            if any(k in gender.lower() for k in KID_SAFE_KEYWORDS): return True
            if any(k in src_name for k in KID_SAFE_KEYWORDS): return True
            return False

        is_kid = check_is_kid()
        
        # Build age filter - STRENGHTENED: Check both system tags and product name
        if is_kid:
            age_filter = f"(LOWER(age_by_product) LIKE '%trẻ em%' OR LOWER(age_by_product) LIKE '%kid%' OR LOWER(gender_by_product) LIKE '%bé%' OR LOWER(product_name) LIKE '%bé%')"
        else:
            age_filter = f"(LOWER(age_by_product) NOT LIKE '%trẻ em%' AND LOWER(age_by_product) NOT LIKE '%kid%' AND LOWER(gender_by_product) NOT LIKE '%bé%' AND LOWER(product_name) NOT LIKE '%bé%' AND LOWER(product_name) NOT LIKE '%kid%')"
            
        # Helper inner function to run SQL with deduplication
        def fetch_top_sql(where_sql_extra: str, limit=5):
            # Map source gender to SQL filter
            target_gender_sql = gender.lower().replace("women", "nữ").replace("men", "nam")
            
            query = f"""
                SELECT magento_ref_code, product_name, master_color, product_line_vn, product_image_url_thumbnail, gender_by_product
                FROM {SR_TABLE}
                WHERE (LOWER(gender_by_product) LIKE '%{target_gender_sql}%' OR LOWER(gender_by_product) LIKE '%unisex%')
                  AND ({age_filter})
                  AND ({where_sql_extra})
                ORDER BY is_new_product DESC, quantity_sold DESC
                LIMIT {limit * 4}
            """
            try:
                res = sr_db.execute_query(query)
                out = []
                seen_codes = set()
                for r in res:
                    code = r["magento_ref_code"]
                    if code in seen_codes:
                        continue
                    seen_codes.add(code)
                    out.append({
                        "code": code,
                        "magento_ref_code": code, # Compatibility for eval_stylist.py
                        "name": r["product_name"] or "",
                        "color": r["master_color"] or "",
                        "product_line": r["product_line_vn"] or "",
                        "image": r["product_image_url_thumbnail"] or "",
                        "gender_by_product": r["gender_by_product"] or "" # Compatibility for eval_stylist.py
                    })
                    if len(out) >= limit:
                        break
                return out
            except Exception as e:
                logger.error("[Stylist] Dynamic SQL failed: %s", e)
                return []

        # 1. Fetch Color
        for k, cond in color_groups.items():
            results["color"][k] = fetch_top_sql(f"({like_cond}) AND ({cond})")
            
        # 2. Fetch Occasion
        for k, cond in occ_groups.items():
            results["occasion"][k] = fetch_top_sql(cond)
            
        # 3. Fetch Material
        for k, cond in mat_groups.items():
            results["material"][k] = fetch_top_sql(f"({like_cond}) AND ({cond})")

        return results

    # ──────────────────────────────────────────
    # Scoring Sub-functions
    # ──────────────────────────────────────────

    def _pass_hard_filter(self, src: dict, tgt: dict) -> bool:
        """Return False if the pair should be excluded."""
        rules = self.rules

        # Exclude blacklisted product lines
        excluded = [pl.lower() for pl in rules.get("exclude_product_lines", [])]
        if tgt.get("product_line", "").lower() in excluded:
            return False

        # ── Gender filter ───────────────────────────────────────────────────
        # "unisex" can pair with anything; kids-only "unisex trẻ em" is handled
        # by age_group below. Here we only check adult-gender mismatch.
        sg = (src.get("gender") or "").lower().strip()
        tg = (tgt.get("gender") or "").lower().strip()

        def _is_unisex(g: str) -> bool:
            return "unisex" in g

        # Both have explicit genders, neither is unisex → must match
        if sg and tg and not _is_unisex(sg) and not _is_unisex(tg) and sg != tg:
            return False

        # ── Age group filter (adult/kids) ────────────────────────────────────
        sa = (src.get("age_group") or "").lower()
        ta = (tgt.get("age_group") or "").lower()
        
        # Also detect kids' items from gender field (unisex trẻ em, bé trai, bé gái …)
        # AND from product NAME in case StarRocks age_group field has wrong data
        src_name = (src.get("name") or "").lower()
        tgt_name = (tgt.get("name") or "").lower()

        def is_kid(age_str: str, gender_str: str, name_str: str) -> bool:
            # Explicit adult markers
            if any(k in age_str for k in ["người lớn", "nguoi_lon", "nguoi lon", "adult"]):
                return False
            # Explicit kid markers in name (High priority)
            if any(k in name_str for k in ["bé ", " bé", "kid", "trẻ em"]):
                return True
            # Explicit kid markers in age field
            if any(k in age_str for k in ["trẻ em", "kid", "tre_em"]):
                return True
            # Explicit kid markers in gender field
            if any(k in gender_str for k in KID_SAFE_KEYWORDS):
                return True
            return False

        src_kid = is_kid(sa, sg, src_name)
        tgt_kid = is_kid(ta, tg, tgt_name)
        
        if src_kid != tgt_kid:
            return False

        return True

    def _get_role(self, product: dict) -> Optional[str]:
        """Determine which role this product plays in an outfit."""
        pl = product.get("product_line", "")
        return self.rules["product_line_to_role"].get(pl)

    def _score(self, src: dict, tgt: dict, occasion: str) -> int:
        """Compute compatibility score (0-100) for src+tgt pair using only Color, Material, and Role."""
        rules = self.rules
        weights = rules["score_weights"]
        total = 0

        # 1. Color score (0-30)
        color_score = self._color_score(src.get("color", ""), tgt.get("color", ""), tgt.get("gender", ""))
        total += int(color_score * weights["color"] / 30)

        # 2. Style score - REMOVED (NLP tags)

        # 3. Occasion score (0-20) - Reactivated to use NLP tags
        if "occasion" in weights:
            occ_score = self._occasion_score(src.get("occasion_tags", []), tgt.get("occasion_tags", []), occasion)
            total += int(occ_score * weights["occasion"] / 20)

        # 4. Role bonus — Bottom gets priority over accessories (0-15)
        role = self._get_role(tgt)
        role_priority = rules.get("role_priority", {})
        role_bonus = max(0, 16 - role_priority.get(role, 3) * 5)
        total += int(role_bonus * weights["role"] / 15)

        # 5. Material score (0-10) - New dimension based on season compatibility
        if "material" in weights:
            material_score = self._material_score(src, tgt)
            total += int(material_score * weights["material"] / 10)

        return min(total, 100)

    def _color_score(self, src_color: str, tgt_color: str, gender: str = "") -> int:
        """Score color compatibility using group matrix (0-30), fallback to exact key matrix.
        Applies logic boosting/penalizing based on demographic."""
        rules = self.rules
        color_keys = rules["color_keys"]
        
        src_key = self._detect_color_key(src_color, color_keys)
        tgt_key = self._detect_color_key(tgt_color, color_keys)

        # Retrieve groups if available (Neutral, Light, Dark)
        color_groups = rules.get("color_groups", {})
        src_group = color_groups.get(src_key) if src_key else None
        tgt_group = color_groups.get(tgt_key) if tgt_key else None

        base_score = rules.get("default_color_score", 12)

        # Primary logic: Framework Excel styling (Neutral vs Light vs Dark)
        if src_group and tgt_group:
            group_matrix = rules.get("color_group_matrix", {})
            base_score = group_matrix.get(src_group, {}).get(tgt_group, base_score)
        # Fallback: specific color matching (matrix legacy)
        elif src_key and tgt_key:
            matrix = rules["color_matrix"]
            row = matrix.get(src_key, {})
            base_score = row.get(tgt_key, base_score)

        # Demographic specific color boost
        demo_logic = rules.get("demographic_color_logic", {})
        
        # Determine demographic key based on gender
        gl = gender.lower()
        demo_key = "unisex_nguoi_lon"
        if gl in ["nu", "nữ", "women", "female"]:
            demo_key = "nu"
        elif gl in ["nam", "men", "male"]:
            demo_key = "nam"
        elif any(k in gl for k in ["bé gái", "be_gai", "girl"]):
            demo_key = "be_gai"
        elif any(k in gl for k in ["bé trai", "be_trai", "boy"]):
            demo_key = "be_trai"
        elif any(k in gl for k in ["bé", "kid"]):
            demo_key = "unisex_tre_em"
            
        demo_rules = demo_logic.get(demo_key, {})
        
        # Apply boosts
        for b in demo_rules.get("boosts", []):
            if tgt_group == b["group"]:
                base_score += b["points"]
        
        # Apply penalties
        for p in demo_rules.get("penalties", []):
            if tgt_group == p["group"]:
                base_score += p["points"]

        return min(base_score, 30)

    def _detect_color_key(self, color_str: str, color_keys: dict) -> Optional[str]:
        """Map a master_color string to a canonical color key."""
        c = color_str.lower()
        for key, variants in color_keys.items():
            if any(v in c for v in variants):
                return key
        return None

    def _material_score(self, src: dict, tgt: dict) -> int:
        """Score material based on target's materials matching the required season (0-10)."""
        rules = self.rules
        tgt_mat = tgt.get("material_tags", [])
        tgt_season = tgt.get("season_tags", [])
        src_season = src.get("season_tags", [])
        
        # Determine the target season. Combine source season and target season tags for a broader match.
        target_seasons = set(s.lower() for s in tgt_season + src_season)
        
        # If no seasons are provided, default to medium positive compat
        if not target_seasons and not tgt_mat:
            return 5 # Neutral score if missing

        score = 0
        mat_season_matrix = rules.get("material_season_compat", {})
        
        for m in tgt_mat:
            # Map material string to a canonical material key
            matched_key = None
            for key in mat_season_matrix.keys():
                if key in m.lower():
                    matched_key = key
                    break
            
            if matched_key:
                compat_seasons = mat_season_matrix[matched_key]
                # If target season matches compatibility, full points (10/10)
                if any(s in compat_seasons for s in target_seasons):
                    score = max(score, 10)
                else:
                    # Found material but season doesn't ideally match, partial points
                    score = max(score, 4)
            else:
                score = max(score, 5) # Unrecognized material
                
        # If we loop and didn't find any actionable material score, default to 5
        return score if score > 0 else 5

    def _style_score(self, src_styles: list, tgt_styles: list, occasion: str) -> int:
        """Score style compatibility (0-25) with occasion boost."""
        rules = self.rules
        compat = rules["style_compat"]
        boost_styles = rules.get("occasion_style_boost", {}).get(occasion, [])
        best = rules.get("default_style_score", 10)

        for ss in (src_styles or ["Basic"]):
            row = compat.get(ss, {})
            for ts in (tgt_styles or ["Basic"]):
                v = row.get(ts, rules.get("default_style_score", 10))
                if ts in boost_styles:
                    v = min(v + 5, 25)
                best = max(best, v)
        return best

    def _occasion_score(self, src_occ: list, tgt_occ: list, occasion: str) -> int:
        """Score occasion overlap (0-20)."""
        keywords = self.rules.get("occasion_keywords", {}).get(occasion, [])
        src_text = " ".join(src_occ).lower()
        tgt_text = " ".join(tgt_occ).lower()
        both_match = any(kw in src_text for kw in keywords) and any(kw in tgt_text for kw in keywords)
        either_match = any(kw in src_text for kw in keywords) or any(kw in tgt_text for kw in keywords)
        if both_match:
            return 20
        if either_match:
            return 12
        return 6

    def _build_reason(self, src: dict, tgt: dict, occasion: str, score: int) -> str:
        """Generate varied Vietnamese styling reason — stylist tone, not template."""


        sc = self._detect_color_key(src.get("color", ""), self.rules["color_keys"])
        tc = self._detect_color_key(tgt.get("color", ""), self.rules["color_keys"])
        role = self._get_role(tgt)
        occ_label = self.rules.get("occasion_labels", {}).get(occasion, occasion)
        src_pl = src.get("product_line", "")
        tgt_pl = tgt.get("product_line", "")

        # ── Color phrasing library ──
        color_phrases = []
        if sc and tc:
            if sc == tc:
                color_phrases = [
                    f"tone {sc.lower()} đồng điệu tạo set đồ liền mạch",
                    f"cùng gam {sc.lower()} cho diện mạo thống nhất",
                    f"giữ nguyên tone {sc.lower()} – thanh lịch, không rối mắt",
                ]
            else:
                color_phrases = [
                    f"phối {sc.lower()} + {tc.lower()} cân bằng sáng – tối",
                    f"gam {sc.lower()} kết hợp {tc.lower()} tạo điểm nhấn",
                    f"tông {tc.lower()} bắt sáng trên nền {sc.lower()}",
                    f"mix {sc.lower()} và {tc.lower()} – clean nhưng không nhàm",
                ]

        # ── Role-specific phrasing ──
        role_phrases = {
            "top": [
                f"áo phối chuẩn tỉ lệ với {src_pl}",
                f"dáng áo ôm nhẹ balance với {src_pl}",
                f"form áo gọn – tôn dáng khi mặc cùng {src_pl}",
            ],
            "bottom": [
                f"quần/váy giữ silhouette cân đối",
                f"phom dáng dưới hợp dáng trên – tạo tỉ lệ đẹp",
                f"item dưới chuẩn form – không bị chìm khi phối",
            ],
            "outerwear": [
                f"layer ngoài hoàn thiện set – thêm chiều sâu",
                f"khoác nhẹ nâng tầm outfit, che khuyết điểm tốt",
                f"layer phủ ngoài giúp set đồ có điểm nhấn",
            ],
            "accessory": [
                f"phụ kiện điểm nhấn – nâng outfit lên level mới",
                f"chi tiết nhỏ nhưng tạo khác biệt rõ rệt",
                f"phụ kiện giúp set đồ trông hoàn chỉnh hơn",
            ],
        }

        # ── Occasion-specific tips ──
        occ_tips = {
            "di_lam": ["gọn gàng, chuyên nghiệp", "thanh lịch nơi công sở", "lịch sự mà không quá cứng nhắc"],
            "di_choi": ["thoải mái, trẻ trung", "năng động mà vẫn sành điệu", "cuốn hút khi dạo phố"],
            "hang_ngay": ["dễ mặc, dễ phối", "đơn giản nhưng không đơn điệu", "tiện lợi cho mọi hoạt động"],
            "du_lich": ["gọn nhẹ, dễ di chuyển", "thoáng mát, tiện cho chuyến đi", "phong cách du lịch thoải mái"],
            "the_thao": ["thoáng khí, co giãn tốt", "năng động, thoải mái vận động", "sporty nhưng vẫn có gu"],
            "di_tiec": ["sang trọng, nổi bật", "thanh lịch cho sự kiện", "tạo ấn tượng mạnh"],
            "mac_nha": ["mềm mại, thoải mái", "dễ chịu khi ở nhà", "relax tối đa"],
        }

        # ── Build final reason ──
        parts = []
        if color_phrases:
            parts.append(random.choice(color_phrases))

        if role and role in role_phrases:
            parts.append(random.choice(role_phrases[role]))
        
        if occ_label and occasion in occ_tips:
            parts.append(random.choice(occ_tips[occasion]))

        if not parts:
            return f"Phù hợp phối cùng – điểm tương thích {score}/100"

        return ". ".join(parts[:2]).capitalize()


    def _deduplicate(self, items: list[dict], max_n: int) -> list[dict]:
        """Remove near-duplicates logic (Enforce product diversity + color diversity)."""
        seen_lines: set[str] = set()
        seen_colors: set[str] = set()
        result = []
        for item in items:
            pl = item.get("product_line", item["code"])
            # Canonicalize color slightly for filtering
            color_val = self._detect_color_key(item.get("color", ""), self.rules["color_keys"]) or item.get("color", "")
            
            # Prioritize inserting if we haven't seen this product line OR color yet
            if pl not in seen_lines or color_val not in seen_colors or len(result) < max_n // 2:
                result.append(item)
                seen_lines.add(pl)
                if color_val:
                    seen_colors.add(color_val)
                    
            if len(result) >= max_n:
                break
        return result

    # ──────────────────────────────────────────
    # Data Loading
    # ──────────────────────────────────────────

    def _get_catalog(self, force_reload: bool = False) -> list[dict]:
        """Load product catalog from SQLite only (offline mode)."""
        if self._catalog and not force_reload:
            return self._catalog

        logger.info("[Stylist] Loading catalog from SQLite...")
        conn = sqlite3.connect(SQLITE_DB_PATH)
        conn.row_factory = sqlite3.Row
        cur = conn.cursor()

        # Fetch product basics from SQLite
        query = f"""
            SELECT magento_ref_code as code,
                   product_name as name,
                   master_color as color,
                   product_line_vn as product_line,
                   gender_by_product as gender,
                   age_by_product as age_group,
                   product_image_url_thumbnail as image
            FROM {TABLE_PRODUCTS}
            WHERE magento_ref_code IS NOT NULL
              AND product_line_vn IS NOT NULL
        """
        cur.execute(query)
        rows = cur.fetchall()
        cur.close()
        conn.close()

        # Build catalog (no style/occasion tags from ultra_descriptions to avoid Postgres)
        catalog = []
        for row in rows:
            catalog.append({
                "code": row["code"],
                "name": row["name"] or "",
                "color": row["color"] or "",
                "product_line": row["product_line"] or "",
                "gender": row["gender"] or "",
                "age_group": row["age_group"] or "",
                "image": row["image"] or "",
                "style_tags": [],
                "occasion_tags": [],
                "material_tags": [],
                "season_tags": [],
            })

        self._catalog = catalog
        logger.info("[Stylist] Catalog loaded: %d products", len(catalog))
        return catalog

    def _extract_tags(self, *fields: str) -> list[str]:
        """Parse comma/dot/pipe separated style tags."""
        tags = []
        for field in fields:
            if field:
                for sep in [",", "·", "|", ";"]:
                    field = field.replace(sep, ",")
                tags.extend([t.strip() for t in field.split(",") if t.strip()])
        return tags

    def _extract_occasion(self, *fields: str) -> list[str]:
        """Parse occasion strings."""
        return self._extract_tags(*fields)

    # ──────────────────────────────────────────
    # DB Write
    # ──────────────────────────────────────────

    def _save_matches(self, code: str, matches: dict) -> bool:
        """Save matches to ai_outfit_product_matches table."""
        conn = None
        try:
            conn = sqlite3.connect(SQLITE_DB_PATH)
            cur = conn.cursor()

            # Get full catalog to look up target details (gender, age)
            catalog = self._get_catalog()
            source = next((p for p in catalog if p["code"] == code), None)
            if not source:
                logger.error("[Stylist] Anchor product not found: %s", code)
                return False

            # Build flat list of rows to insert
            rows = []
            for occ, occ_data in matches.items():
                for role, items in occ_data.items():
                    for item in items:
                        # Skip items without rule_id (fallback path)
                        if item.get("rule_id") is None:
                            continue
                        # Find target product in catalog to get gender and age
                        target = next((p for p in catalog if p["code"] == item["code"]), None)
                        if not target:
                            continue
                        rows.append((
                            item["rule_id"],
                            code,  # anchor_product_code
                            item["code"],  # match_product_code
                            item["name"],  # match_product_name
                            target.get("gender", ""),  # match_gender
                            target.get("age_group", ""),  # match_age
                            item["role"],  # match_role
                            item["score"],
                            item["reason"],
                            item["occasion"],  # occasion (NOT occasion_tag)
                        ))

            if rows:
                cur.executemany('''
                    INSERT INTO pg__dashboard_canifa__ai_outfit_product_matches
                    (rule_id, anchor_product_code, match_product_code, match_product_name, match_gender, match_age, match_role, score, ai_reason, occasion)
                    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
                ''', rows)
                conn.commit()
                logger.info("[Stylist] Saved %d matches for %s", len(rows), code)
            else:
                logger.debug("[Stylist] No matches to save for %s", code)
            return True
        except Exception as e:
            logger.error("[Stylist] Save matches error: %s", e)
            if conn:
                conn.rollback()
            return False
        finally:
            if conn:
                conn.close()

    # ── Public aliases for score-test API ───────
    def _load_catalog(self) -> list[dict]:
        """Alias for _get_catalog (used by score-test API)."""
        return self._get_catalog()

    def _score_breakdown(self, src: dict, tgt: dict, occ_target: str = None) -> dict:
        """Return per-criterion score breakdown for a source+target pair (used by DB-based matching)."""
        rules = self.rules
        weights = rules["score_weights"]

        # Color
        raw_color = self._color_score(src.get("color", ""), tgt.get("color", ""), tgt.get("gender", ""))
        color_pts = int(raw_color * weights["color"] / 30)

        # Occasion (Re-enabled)
        occ_pts = 0
        raw_occ = 0
        if "occasion" in weights:
            raw_occ = self._occasion_score(src.get("occasion_tags", []), tgt.get("occasion_tags", []), occ_target)
            occ_pts = int(raw_occ * weights["occasion"] / 20)

        # Role
        role = self._get_role(tgt)
        role_priority = rules.get("role_priority", {})
        role_bonus = max(0, 16 - role_priority.get(role, 3) * 5)
        role_pts = int(role_bonus * weights["role"] / 15)

        # Material
        material_pts = 0
        raw_material = 0
        if "material" in weights:
            raw_material = self._material_score(src, tgt)
            material_pts = int(raw_material * weights["material"] / 10)

        sc = self._detect_color_key(src.get("color", ""), rules["color_keys"])
        tc = self._detect_color_key(tgt.get("color", ""), rules["color_keys"])

        cgroups = rules.get("color_groups", {})
        sg = cgroups.get(sc, "?") if sc else "?"
        tg = cgroups.get(tc, "?") if tc else "?"

        # Excel Formulas determination
        formulas = rules.get("formulas", {})
        cong_thuc_name = ""
        cong_thuc_tip = ""

        if sg == "neutral" and tg == "neutral":
            fm = formulas.get("neutral_neutral", {})
            cong_thuc_name = fm.get("name", 'Công thức [An Toàn]')
            cong_thuc_tip = fm.get("tip", "")
        elif (sg == "neutral" and tg in ("light", "dark")) or (tg == "neutral" and sg in ("light", "dark")):
            fm = formulas.get("neutral_light", {})
            if (sg == "dark" or tg == "dark"):
                fm = formulas.get("neutral_dark", fm)
            cong_thuc_name = fm.get("name", 'Công thức [Điểm Nhấn]')
            cong_thuc_tip = fm.get("tip", "")
        elif (sg == "dark" and tg == "dark"):
            cong_thuc_name = 'Công thức [Tone-Sur-Tone Trầm]'
            cong_thuc_tip = "MẸO: Tổng thể sẽ bí ẩn và có chiều sâu, nên pick phụ kiện kim loại."
        elif (sg == "light" and tg == "light"):
            cong_thuc_name = 'Công thức [Tone-Sur-Tone Nhạt]'
            cong_thuc_tip = "MẸO: Nên phối với giày độn đế Trắng hoặc phụ kiện sáng để không bị nhòa."

        # Mùa / Chất liệu
        tgt_mat_raw = " ".join(tgt.get("material_tags", [])) + " " + tgt.get("name_origin", "")
        tgt_mat_raw = tgt_mat_raw.lower()

        tgt_mat_season = "Không rõ mùa / dòng"
        if any(w in tgt_mat_raw for w in ["cotton", "linen", "hè"]):
            tgt_mat_season = "Hợp mùa hè (thoáng/mát)"
        elif any(w in tgt_mat_raw for w in ["nỉ", "len", "chần bông", "dạ", "phao", "đông"]):
            tgt_mat_season = "Hợp mùa đông (giữ nhiệt)"
        elif any(w in tgt_mat_raw for w in ["gió", "kaki", "jean", "denim", "thun"]):
            tgt_mat_season = "Linh hoạt cả Xuân/Thu"

        tgt_materials = tgt.get("material_tags", [])
        tgt_styles = tgt.get("style_tags", [])

        # Color breakdown with Formula tip injected
        color_reason_text = ""
        group_lbl = {
            "neutral": "Trắng/Đen/Trung tính",
            "light": "Nhạt/Pastel",
            "dark": "Đậm/Nổi"
        }
        if cong_thuc_name:
            color_reason_text = f"🪧 {cong_thuc_name}: {group_lbl.get(sg, sg)} ➔ {group_lbl.get(tg, tg)}. {cong_thuc_tip}"
        else:
            color_reason_text = f"Màu {sc} đi với {tc} khá hài hòa."

        color_reason_text += f" [Hệ {tgt.get('gender', 'Unisex')}]"

        occ_label = rules.get("occasion_labels", {}).get(occ_target, occ_target)
        occ_reason = f"Độ tương hợp cho dịp {occ_label}" if raw_occ >= 12 else f"Cân nhắc khi dùng cho {occ_label}"

        return {
            "🎨 Mix Màu":       {"score": color_pts,    "max": weights["color"],            "reason": color_reason_text},
            "👔 Style":         {"score": 0,            "max": 0,                           "reason": "Đã bỏ NLP tags"},
            "🛍️ Dịp Mặc":      {"score": occ_pts,      "max": weights.get("occasion", 0),  "reason": occ_reason},
            "🧵 Dòng Hàng/Mùa": {"score": material_pts, "max": weights.get("material", 10), "reason": f"{tgt_mat_season} (Vải {', '.join(tgt_materials[:2]) if tgt_materials else 'chính hãng'})"},
            "🎯 Nhóm SP":       {"score": role_pts,     "max": weights["role"],             "reason": f"Dáng {tgt.get('product_line', 'Tiêu chuẩn')} tối ưu tỷ lệ"}
        }
