#!/usr/bin/env python3
"""Batch import JSONL into ccpc.db (cceup_projects)"""
import sys, json, sqlite3, os

if len(sys.argv) < 2:
    print('Usage: import_ccpc_batch.py <jsonl_path>')
    sys.exit(1)

path = sys.argv[1]
if not os.path.exists(path):
    print(f'File not found: {path}')
    sys.exit(1)

db = sqlite3.connect('/root/ccpc.db')
db.execute('PRAGMA journal_mode=WAL')
db.execute('PRAGMA synchronous=OFF')
db.execute('PRAGMA busy_timeout=5000')

count = 0
with open(path, 'r', encoding='utf-8') as f:
    for line in f:
        line = line.strip()
        if not line:
            continue
        r = json.loads(line)

        project_name = r.get('project_name', '') or ''
        project_id = r.get('project_id', '') or ''
        # dedup: skip if project_id already exists
        if project_id:
            cur = db.execute('SELECT 1 FROM cceup_projects WHERE project_id = ?', (project_id,))
            if cur.fetchone():
                continue

        contacts = r.get('contacts', [])
        contacts_str = json.dumps(contacts, ensure_ascii=False) if contacts else ''
        raw_json = r.get('raw_json', '') or json.dumps(r, ensure_ascii=False)

        db.execute('''
            INSERT INTO cceup_projects (
                project_name, project_type, project_id, industry, field_type,
                province, city, phase, publish_date, version, nature, budget,
                invest_nature, funding, grade, start_time, end_time,
                equipment_source, building_area, land_area, steel_structure,
                building_floors, heating_method, wall_material, decoration,
                has_ac, has_parking, has_elevator, project_address, tags,
                equipment_list, progress, overview, detail, owner_company,
                contacts, raw_json
            ) VALUES (?,?,?,?,?, ?,?,?,?,?, ?,?,?,?,?, ?,?,?,?,?, ?,?,?,?,?, ?,?,?,?,?, ?,?,?,?,?, ?,?)
        ''', (
            project_name, r.get('project_type',''), project_id,
            r.get('industry',''), r.get('field_type',''),
            r.get('province',''), r.get('city',''), r.get('phase',''),
            r.get('publish_date',''), r.get('version',''),
            r.get('nature',''), r.get('budget',''),
            r.get('invest_nature',''), r.get('funding',''),
            r.get('grade',''), r.get('start_time',''),
            r.get('end_time',''),
            r.get('equipment_source',''), r.get('building_area',''),
            r.get('land_area',''), r.get('steel_structure',''),
            r.get('building_floors',''), r.get('heating_method',''),
            r.get('wall_material',''), r.get('decoration',''),
            r.get('has_ac',''), r.get('has_parking',''),
            r.get('has_elevator',''), r.get('project_address',''),
            r.get('tags',''),
            r.get('equipment_list',''), r.get('progress',''),
            r.get('overview',''), r.get('detail',''),
            r.get('owner_company',''),
            contacts_str, raw_json
        ))
        count += 1
        
        # Save contacts to cceup_contacts table
        if isinstance(contacts, list) and len(contacts) > 0:
            # Need project's auto-increment id for cceup_contacts.project_id
            cur = db.execute('SELECT id FROM cceup_projects WHERE project_id=?', (project_id,))
            row = cur.fetchone()
            if row:
                proj_db_id = row[0]
                for ct in contacts:
                    db.execute(
                        "INSERT INTO cceup_contacts (project_id, company, contact_name, phone, landline, address, role) VALUES (?,?,?,?,?,?,?)",
                        (proj_db_id,
                         ct.get('company','') or '',
                         ct.get('contact_name','') or '',
                         ct.get('phone','') or '',
                         ct.get('landline','') or ct.get('phone','') or '',
                         ct.get('address','') or '',
                         ct.get('role','') or '')
                    )

db.commit()
# Count total
total = db.execute('SELECT count(*) FROM cceup_projects').fetchone()[0]
db.close()

print(f'OK:{count}')
print(f'Total in DB: {total}')
