#!/usr/bin/env python3
"""
performance, positions 테이블 초기 데이터 생성
T+20 모니터링 활성화
"""

import sqlite3
from pathlib import Path
from datetime import datetime

def init_performance_tables():
    """초기 성과 데이터 생성"""

    db_path = Path('src/quant_engine/snapshot_admin.db')
    conn = sqlite3.connect(db_path)
    cursor = conn.cursor()

    # 1. performance 테이블에 샘플 거래 기록 추가
    sample_trades = [
        ('005930', 'Samsung Electronics', '2026-06-01', 70000, 100, 70000, 72000, 2.86, 'COMPLETED', 'T+20'),
        ('000660', 'SK Hynix', '2026-06-05', 120000, 50, 120000, 121500, 1.25, 'ACTIVE', None),
        ('035420', 'NAVER', '2026-06-10', 385000, 10, 385000, 390000, 1.30, 'COMPLETED', 'T+20'),
    ]

    print(f"performance 초기화: {len(sample_trades)}개 거래 추가")
    for ticker, name, entry_date, entry_price, qty, _, current_price, pnl_pct, status, t20_milestone in sample_trades:
        cursor.execute("""
            INSERT INTO performance
              (ticker, name, entry_date, entry_price, quantity, current_price, pnl_pct, status, t20_milestone)
            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
        """, (
            ticker,
            name,
            entry_date,
            entry_price,
            qty,
            current_price,
            pnl_pct,
            status,
            t20_milestone
        ))

    # 2. positions 테이블에 현재 포지션 추가
    sample_positions = [
        ('005930', 'Samsung Electronics', 100, 70000, 72000, 70500, 'IT'),
        ('000660', 'SK Hynix', 50, 120000, 121500, 120750, 'IT'),
        ('035420', 'NAVER', 10, 385000, 390000, 387500, 'Internet'),
    ]

    print(f"positions 초기화: {len(sample_positions)}개 포지션 추가")

    today = datetime.now().isoformat()
    for ticker, name, quantity, entry_price, current_price, avg_cost, sector in sample_positions:
        cursor.execute("""
            INSERT INTO positions
              (ticker, name, quantity, entry_price, current_price, average_cost, sector, updated_at)
            VALUES (?, ?, ?, ?, ?, ?, ?, ?)
        """, (
            ticker,
            name,
            quantity,
            entry_price,
            current_price,
            avg_cost,
            sector,
            today
        ))

    conn.commit()

    # 검증
    cursor.execute("SELECT COUNT(*) FROM performance")
    perf_count = cursor.fetchone()[0]

    cursor.execute("SELECT COUNT(*) FROM positions")
    pos_count = cursor.fetchone()[0]

    print(f"\n[결과]")
    print(f"  performance: {perf_count}개 행")
    print(f"  positions: {pos_count}개 행")

    conn.close()

    print(f"\n[OK] T+20 모니터링 초기화 완료")

if __name__ == "__main__":
    init_performance_tables()
