import psycopg
import random

# PG connection string from the derivation project
PG_KIND = "postgresql://deriva:deriva_dev_2026@localhost:15400/deriva"

def generate_mock_assets():
    print("--- Phase 6: Expanding Asset Universe (Mocking 500+ Assets) ---")
    
    # Establish connection
    try:
        with psycopg.connect(PG_KIND) as conn:
            with conn.cursor() as cur:
                # We will generate 250 equities and 250 bonds
                
                # Equities
                sectors = ['C', 'G', 'K', 'J', 'D'] # KSIC approximations
                gics_sectors = ['Information Technology', 'Consumer Discretionary', 'Financials', 'Utilities', 'Industrials']
                
                records_to_insert = []
                for i in range(1, 251):
                    code = f"{str(100000 + i)}.KS"
                    name = f"MockCorp_{i}"
                    sec = random.choice(sectors)
                    gics = random.choice(gics_sectors)
                    
                    records_to_insert.append((code, 'Equity', name, 'KRW', 'KR', sec, gics, None))
                
                # Bonds
                ratings = ['AAA', 'AA+', 'AA-', 'A+', 'A-', 'BBB+', 'BBB-']
                for i in range(251, 501):
                    code = f"KR_BND_{i}"
                    name = f"MockBond_{i}"
                    sec = random.choice(sectors)
                    gics = random.choice(gics_sectors)
                    rating = random.choice(ratings)
                    
                    records_to_insert.append((code, 'Bond', name, 'KRW', 'KR', sec, gics, rating))
                
                # Execute batch insert
                cur.executemany("""
                    INSERT INTO asset_masters 
                    (asset_code, asset_type, asset_name, currency, country, sector, gics_sector, credit_rating)
                    VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
                    ON CONFLICT(asset_code) DO NOTHING
                """, records_to_insert)
                
                conn.commit()
                print(f"Successfully inserted {len(records_to_insert)} mock assets into deriving Postgres.")
                
    except Exception as e:
        print(f"[Error] Failed to inject assets: {e}")

if __name__ == '__main__':
    generate_mock_assets()
