#!/usr/bin/env python3
"""Plain, user-owned export and read-only import plan."""
import csv
import datetime as dt
import io
import json
import os
import re
import subprocess
import sys
import unicodedata
import zipfile
sys.dont_write_bytecode = True
sys.path.insert(0, os.path.dirname(__file__))
from archiveio import Refused, data_files, digest, read_zip, validate_list, bounded_read

CONFIG_KEYS = ('repos_folders', 'repos', 'repos_depth', 'read_sessions', 'read_calendar', 'know_sources', 'memory_code', 'memory_meaning', 'sensitive_domains')
README = b'Your Argoni data\n\nOpen this ZIP with any archive app. Documents are grouped by topic. To bring them back, choose Import in Argoni Settings. Argoni checks the copy and asks before changing settings or personal notes. This is a plain copy; anyone who can read it can read your data.\n\nDot files and dot folders are left out of this plain export. The sealed backup keeps them, except the rebuilt data/.summary cache.\n'

def plain(content):
    try:
        text = content.decode('utf-8')
    except UnicodeDecodeError:
        return content
    if text.startswith('---\n'):
        end = text.find('\n---\n', 4)
        if end != -1:
            fields = dict(re.findall(r'^([a-z_]+):\s*(.+)$', text[4:end], re.M))
            head = ''.join('%s: %s\n' % (k.title(), fields[k]) for k in ('date', 'updated', 'status') if k in fields)
            return (head + '\n' + text[end + 5:].lstrip('\n')).encode('utf-8')
    return content

def dest_name(rel):
    parts = rel.split('/')
    if parts[0] in ('decisions', 'ideas', 'projects', 'log'):
        return rel
    if parts[:2] == ['domains', 'people']:
        return 'people/' + '/'.join(parts[2:])
    return 'notes/' + rel

def settings(data):
    raw = {}
    p = os.path.join(data, 'config.json')
    if os.path.isfile(p) and not os.path.islink(p):
        with open(p, 'r', encoding='utf-8') as f:
            raw = json.load(f)
    result = {k: raw[k] for k in CONFIG_KEYS if k in raw}
    jobs = os.path.join(data, 'jobs.json')
    if os.path.isfile(jobs) and not os.path.islink(jobs):
        with open(jobs, 'r', encoding='utf-8') as f:
            obj = json.load(f)
        # Only the user's own entries; never a merged shipped job list.
        if isinstance(obj, dict):
            result['jobs'] = obj.get('user', obj.get('mine', []))
    return json.dumps(result, ensure_ascii=False, indent=2).encode() + b'\n'

def chat_files(raw):
    if not raw:
        return {}
    obj = json.loads(raw)
    conversations = obj if isinstance(obj, list) else [obj]
    days = {}
    for convo in conversations:
        for msg in convo.get('messages', []):
            date = str(msg.get('date', ''))[:10]
            if not re.fullmatch(r'\d{4}-\d{2}-\d{2}', date):
                continue
            who = 'You' if msg.get('role') == 'user' else 'Argoni'
            days.setdefault(date, []).append('## %s\n\n%s\n' % (who, msg.get('text', '')))
    return {'chat/%s.md' % day: ('# Chat on %s\n\n' % day + '\n'.join(lines)).encode() for day, lines in days.items()}

def spending(data):
    rows = []
    p = os.path.join(data, 'log', 'runs.jsonl')
    if os.path.isfile(p) and not os.path.islink(p):
        with open(p, encoding='utf-8') as f:
            for line in f:
                try:
                    r = json.loads(line)
                except ValueError:
                    continue
                if not isinstance(r, dict):
                    continue
                rows.append([str(r.get('at', r.get('date', '')))[:10], r.get('kind', ''), r.get('project', r.get('folder', '')), r.get('job', ''), r.get('cost', r.get('cost_usd', ''))])
    out = io.StringIO()
    w = csv.writer(out)
    w.writerow(['date', 'kind', 'folder or project', 'job', 'cost'])
    w.writerows(rows)
    return out.getvalue().encode()

def make(folder, out, history=False, chat=None):
    data = os.path.join(folder, 'data')
    files = {'README.txt': README, 'settings.json': settings(data), 'spending.csv': spending(data)}
    for rel, path in data_files(data):
        name = dest_name(rel)
        if name in files:
            raise Refused('two files would have one export name')
        with open(path, 'rb') as f:
            content = bounded_read(f)
            files[name] = plain(content) if rel.endswith(('.md', '.txt')) else content
    files.update(chat_files(chat))
    if history:
        # Bundle only the data branch of a history kept inside data/. No hooks or filters run.
        if os.path.isdir(os.path.join(data, '.git')):
            import tempfile
            with tempfile.TemporaryDirectory() as temp:
                bundle = os.path.join(temp, 'data.bundle')
                p = subprocess.run(['git', '-C', data, '-c', 'core.hooksPath=/dev/null', 'bundle', 'create', bundle, '--all'], capture_output=True)
                if p.returncode == 0 and os.path.isfile(bundle):
                    with open(bundle, 'rb') as f:
                        files['history/data.bundle'] = f.read()
                else:
                    files['history/README.txt'] = b'No data history was available.\n'
        else:
            files['history/README.txt'] = b'No data history was available.\n'
    validate_list(files)
    manifest = {'shape': 1, 'date': dt.date.today().isoformat(), 'files': [{'path': n, 'sha256': digest(b), 'size': len(b)} for n, b in sorted(files.items())]}
    files['manifest.json'] = json.dumps(manifest, indent=2).encode() + b'\n'
    fd = os.open(out, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
    try:
        with os.fdopen(fd, 'wb') as f, zipfile.ZipFile(f, 'w', zipfile.ZIP_DEFLATED) as z:
            for n, b in sorted(files.items()):
                z.writestr(n, b)
    except Exception:
        # Preserve a failed partial file for inspection; never replace it.
        raise

def plan(path, folder):
    files = read_zip(path)
    raw = files.pop('manifest.json', None)
    if raw is None:
        raise Refused('manifest is missing')
    try:
        manifest = json.loads(raw)
        entries = manifest['files']
        expected = {e['path']: e for e in entries}
        if manifest['shape'] != 1 or len(entries) != len(expected) or set(expected) != set(files) or not isinstance(manifest.get('date'), str):
            raise ValueError('file list differs')
        for name, content in files.items():
            if expected[name]['sha256'] != digest(content) or expected[name]['size'] != len(content):
                raise ValueError('manifest hash or size differs')
    except (ValueError, KeyError, TypeError) as e:
        raise Refused('manifest mismatch: %s' % e)
    added, aside, proposals = [], [], []
    data = os.path.join(folder, 'data')
    stamp = dt.date.today().isoformat()
    for name, content in sorted(files.items()):
        folded = unicodedata.normalize('NFC', name).casefold()
        if folded in ('readme.txt', 'spending.csv') or folded.startswith(('history/', 'chat/')):
            continue
        if folded == 'settings.json':
            proposals.append(name)
            continue
        if folded.startswith('people/'):
            rel = 'domains/people/' + name[7:]
        elif folded.startswith('notes/'):
            rel = name[6:]
        elif folded.startswith(('decisions/', 'ideas/', 'projects/', 'log/')):
            rel = name
        else:
            raise Refused('unknown export file')
        parts = unicodedata.normalize('NFC', rel).casefold().split('/')
        if any(part.startswith('.') for part in parts):
            raise Refused('import targets a hidden or internal place')
        if parts[0] in ('me', 'directives', 'imported', 'restored') or len(parts) == 1 and parts[0] in ('jobs.json', 'config.json'):
            proposals.append(name)
            continue
        target = os.path.join(data, *rel.split('/'))
        probe = data
        for part in rel.split('/'):
            probe = os.path.join(probe, part)
            if os.path.islink(probe):
                raise Refused('destination has a link')
        if os.path.exists(target):
            with open(target, 'rb') as f:
                if f.read() != content:
                    aside.append('data/imported/%s/%s' % (stamp, rel))
        else:
            added.append('data/' + rel)
    return {'add': added, 'imported': aside, 'proposals': proposals, 'source_date': manifest['date']}

def main(args):
    try:
        if len(args) == 3 and args[0] == '--check':
            print(json.dumps(plan(args[1], args[2]), ensure_ascii=False))
        elif len(args) >= 2:
            folder, out, *flags = args
            if not out.lower().endswith('.zip'):
                raise Refused('output must end in .zip')
            history = '--history' in flags
            chat = None
            if '--chat-json' in flags:
                i = flags.index('--chat-json')
                if i + 1 >= len(flags) or flags[i+1] != '-':
                    raise Refused('use --chat-json -')
                chat = sys.stdin.read(8 * 1024 * 1024 + 1)
                if len(chat) > 8 * 1024 * 1024:
                    raise Refused('chat input exceeds 8 MiB')
            make(folder, out, history, chat)
            print(out)
        else:
            raise Refused('usage: export FOLDER OUT.zip [--history] [--chat-json -] | export --check IN.zip FOLDER')
        return 0
    except (Refused, OSError, ValueError, KeyError, TypeError, AttributeError) as e:
        print('REFUSED %s' % e, file=sys.stderr)
        return 3
if __name__ == '__main__':
    sys.exit(main(sys.argv[1:]))
