#!/usr/bin/env python3
"""Clean models with year suffixes like 'Model 17-18' or 'Model 2010-2015'."""

import psycopg2, re, sys

MASTER_DSN = "host=localhost dbname=nexus_autoparts user=postgres password=1123517"

def main():
    dry_run = '--execute' not in sys.argv
    conn = psycopg2.connect(MASTER_DSN)
    cur = conn.cursor()

    cur.execute("""
        SELECT m.id_model, m.name_model, m.brand_id, b.name_brand,
               (SELECT COUNT(*) FROM model_year_engine mye WHERE mye.model_id = m.id_model) as mye_count
        FROM models m
        JOIN brands b ON b.id_brand = m.brand_id
        WHERE m.name_model ~ ' [0-9]{2}-[0-9]{2}$' OR m.name_model ~ ' [0-9]{4}-[0-9]{4}$'
        ORDER BY mye_count DESC
    """)
    rows = cur.fetchall()
    print(f"Found {len(rows)} models with year suffix")

    total_myes = 0
    total_models = 0
    total_scc = 0

    for model_id, name_model, brand_id, brand_name, mye_count in rows:
        total_models += 1
        print(f"[{brand_name}] \"{name_model}\" id={model_id}, MYEs={mye_count}")

        if mye_count > 0:
            cur.execute("SELECT id_mye FROM model_year_engine WHERE model_id = %s", (model_id,))
            mye_ids = [r[0] for r in cur.fetchall()]
            total_myes += len(mye_ids)

            # Count supplier_catalog_compat affected
            cur.execute("SELECT COUNT(*) FROM supplier_catalog_compat WHERE model_year_engine_id = ANY(%s)", (mye_ids,))
            scc_count = cur.fetchone()[0]
            total_scc += scc_count
            print(f"  -> {scc_count} supplier_catalog_compat rows will be nulled")

            if not dry_run:
                cur.execute("UPDATE supplier_catalog_compat SET model_year_engine_id = NULL WHERE model_year_engine_id = ANY(%s)", (mye_ids,))
                cur.execute("DELETE FROM vin_cache WHERE model_year_engine_id = ANY(%s)", (mye_ids,))
                cur.execute("DELETE FROM model_year_engine WHERE id_mye = ANY(%s)", (mye_ids,))

        if not dry_run:
            cur.execute("DELETE FROM models WHERE id_model = %s", (model_id,))
            conn.commit()

    print(f"\n{'DRY RUN' if dry_run else 'EXECUTED'}: {total_models} models, {total_myes} MYEs, {total_scc} SCC rows affected")
    cur.close()
    conn.close()

if __name__ == '__main__':
    main()
