# -*- coding: utf-8 -*-
"""
Massive Flat Files Client — S3兼容对象存储
数据源: https://files.massive.com
"""

import os
import json
import boto3
from datetime import datetime
from botocore.client import Config as BotoConfig
from botocore.exceptions import ClientError

from config import (
    MASSIVE_ACCESS_KEY, MASSIVE_SECRET_KEY,
    MASSIVE_ENDPOINT, MASSIVE_BUCKET,
)


class MassiveClient:
    """S3兼容客户端，用于读取Massive flat files"""

    def __init__(self):
        self.client = boto3.client(
            's3',
            endpoint_url=MASSIVE_ENDPOINT,
            aws_access_key_id=MASSIVE_ACCESS_KEY,
            aws_secret_access_key=MASSIVE_SECRET_KEY,
            config=BotoConfig(signature_version='s3v4'),
        )
        self.bucket = MASSIVE_BUCKET

    def list_files(self, prefix=""):
        """列出文件，返回 [{key, size, last_modified}]"""
        try:
            resp = self.client.list_objects_v2(Bucket=self.bucket, Prefix=prefix)
            files = []
            for obj in resp.get('Contents', []):
                files.append({
                    'key': obj['Key'],
                    'size': obj['Size'],
                    'last_modified': obj['LastModified'],
                })
            return sorted(files, key=lambda x: x['last_modified'], reverse=True)
        except ClientError as e:
            raise RuntimeError(f"Massive list error: {e}")

    def get_json(self, key):
        """读取JSON文件并解析"""
        data = self.get_bytes(key)
        return json.loads(data.decode('utf-8'))

    def get_bytes(self, key):
        """读取文件原始字节"""
        try:
            resp = self.client.get_object(Bucket=self.bucket, Key=key)
            return resp['Body'].read()
        except ClientError as e:
            raise RuntimeError(f"Massive get '{key}' error: {e}")

    def get_text(self, key, encoding='utf-8'):
        """读取文本文件"""
        return self.get_bytes(key).decode(encoding)

    def download(self, key, local_path):
        """下载文件到本地"""
        data = self.get_bytes(key)
        os.makedirs(os.path.dirname(local_path) or '.', exist_ok=True)
        with open(local_path, 'wb') as f:
            f.write(data)
        return local_path

    def list_latest(self, prefix="", n=10):
        """列出最新的n个文件"""
        files = self.list_files(prefix)
        return files[:n]


# ── 模块级快捷函数 ──

_client = None


def _get_client():
    global _client
    if _client is None:
        _client = MassiveClient()
    return _client


def list_files(prefix=""):
    return _get_client().list_files(prefix)


def get_json(key):
    return _get_client().get_json(key)


def get_text(key):
    return _get_client().get_text(key)


def list_latest(prefix="", n=10):
    return _get_client().list_latest(prefix, n)


def download(key, local_path):
    return _get_client().download(key, local_path)


if __name__ == '__main__':
    print(f"Massive client ready")
    print(f"Endpoint: {MASSIVE_ENDPOINT}")
    print(f"Bucket: {MASSIVE_BUCKET}")
    files = list_latest()
    print(f"\nRecent files ({len(files)}):")
    for f in files[:20]:
        print(f"  {f['key']:<60} {f['size']:>10,}  {f['last_modified']}")
