from __future__ import annotations

import json
import os
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
if str(ROOT) not in sys.path:
    sys.path.insert(0, str(ROOT))

from scripts.db import DB_PATH, init_db, fetch_one
from scripts.repository import create_project, create_scene, create_task, mark_task_submitted


def main():
    if DB_PATH.exists():
        DB_PATH.unlink()
    wal = Path(str(DB_PATH) + '-wal')
    shm = Path(str(DB_PATH) + '-shm')
    if wal.exists(): wal.unlink()
    if shm.exists(): shm.unlink()
    init_db()
    create_project('proj_demo', 'Demo Project', 'Kling pipeline smoke test')
    create_scene({
        'scene_id': 'scene_demo_001',
        'project_id': 'proj_demo',
        'ordering': 1,
        'narration': 'Demo narration',
        'visual_prompt': 'A cinematic sunrise over Seoul',
        'target_duration': 5,
        'scene_type': 'text',
    })
    task_id = create_task('scene_demo_001', 'text2video', {'prompt': 'demo', 'duration': '5'})
    mark_task_submitted(task_id, 'kling_demo_task_001', 'req_demo_001', {'code': 0, 'request_id': 'req_demo_001', 'data': {'task_id': 'kling_demo_task_001'}})
    row = fetch_one('SELECT task_id_internal, scene_id, endpoint_type, kling_task_id, request_id, task_status FROM tasks WHERE task_id_internal=?', (task_id,))
    print(json.dumps({'ok': True, 'task': row}, ensure_ascii=False, indent=2))


if __name__ == '__main__':
    main()
