import json
from pathlib import Path
import requests

base = Path('/app')

def read_env(path: Path):
    data = {}
    for line in path.read_text(encoding='utf-8').splitlines():
        line = line.strip()
        if not line or line.startswith('#') or '=' not in line:
            continue
        k, v = line.split('=', 1)
        data[k.strip()] = v.strip()
    return data

env = read_env(base / '.env')
token = (env.get('WB_API_TOKEN') or '').strip()
if not token:
    raise SystemExit('WB_API_TOKEN not found')

url = 'https://feedbacks-api.wildberries.ru/api/v1/feedbacks'
headers = {'Authorization': f'Bearer {token}'}
params = {'isAnswered': 'true', 'skip': 0, 'take': 100}
r = requests.get(url, headers=headers, params=params, timeout=20)
r.raise_for_status()
chunk = (r.json().get('data') or {}).get('feedbacks') or []

rows = []
for item in chunk:
    rating = item.get('productValuation') or 0
    if rating not in (4, 5):
        continue
    ans = item.get('answer')
    if isinstance(ans, dict):
        ans_text = (ans.get('text') or '').strip()
    else:
        ans_text = (ans or '').strip()
    if not ans_text:
        continue
    rows.append({
        'createdDate': item.get('createdDate') or '',
        'id': item.get('id') or '',
        'rating': rating,
        'productName': ((item.get('productDetails') or {}).get('productName') or ''),
        'userName': item.get('userName') or '',
        'answer': ans_text,
    })

rows.sort(key=lambda x: x['createdDate'], reverse=True)
out_json = base / 'last_100_answers_4_5.json'
out_txt = base / 'last_100_answers_4_5.txt'
out_json.write_text(json.dumps(rows, ensure_ascii=False, indent=2), encoding='utf-8')
lines = []
for i, row in enumerate(rows, 1):
    lines.append(f"{i}. {row['createdDate']} | {row['rating']}★ | {row['productName']} | {row['userName']} | {row['answer']}")
out_txt.write_text('\n'.join(lines), encoding='utf-8')
print(f'exported={len(rows)}')
print('/app/last_100_answers_4_5.json')
print('/app/last_100_answers_4_5.txt')
