#!/usr/bin/env python
import os, asyncio, sys
os.environ['MEMO_DB_PATH'] = ':memory:'

from fastapi.testclient import TestClient
from server import app

# Initialize DB
from common.sqlite_client import init_sqlite
asyncio.run(init_sqlite())

client = TestClient(app, raise_server_exceptions=False)

# Generate JWT token directly
from common.jwt_auth import create_access_token
token = create_access_token(data={"sub": "test_user"})
print(f'Generated token: {token}')

headers = {'Authorization': f'Bearer {token}'}

# Create memo without workspace_id
create1 = client.post('/api/v1/memos', json={'content':'test','visibility':'PRIVATE'}, headers=headers)
print(f'Create (no workspace): {create1.status_code}')
if create1.status_code != 200:
    print(f'Create error: {create1.text}')
else:
    print(f'Created memo: {create1.json()}')

# Create memo with workspace_id
create2 = client.post('/api/v1/memos', json={'content':'test2','visibility':'PRIVATE','workspace_id':'PERSONAL'}, headers=headers)
print(f'Create (with workspace PERSONAL): {create2.status_code}')
if create2.status_code != 200:
    print(f'Create error: {create2.text}')
else:
    print(f'Created memo: {create2.json()}')

# List memos with workspace filter
list_resp = client.get('/api/v1/memos?workspace_id=PERSONAL', headers=headers)
print(f'List PERSONAL: {list_resp.status_code}')
if list_resp.status_code != 200:
    print(f'List error: {list_resp.text}')
else:
    print(f'List count: {len(list_resp.json())}')
    for m in list_resp.json():
        print(f"  - {m['id']}: workspace={m.get('workspace_id')}")

