from __future__ import annotations

import base64
import json
from datetime import date
from pathlib import Path

from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey

PRIVATE_KEY_PEM = """-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEICaRWg+kLnLUvtzWdDJVYMj+bBDJVbquIxxGuD4gp5qV
-----END PRIVATE KEY-----
"""


def generate_full_license(customer_name: str, company_name: str, device_code: str, out_path: str) -> None:
    private_key = serialization.load_pem_private_key(PRIVATE_KEY_PEM.encode('utf-8'), password=None)
    assert isinstance(private_key, Ed25519PrivateKey)
    payload = {
        'product': 'Mi100 Studio Manager',
        'license_version': 1,
        'license_type': 'full',
        'customer_name': customer_name,
        'company_name': company_name,
        'device_code': device_code,
        'issued_at': date.today().isoformat(),
        'expires_at': None,
        'features': {
            'projects': True, 'clients': True, 'actors': True, 'documents': True, 'reports': True, 'email': True, 'backup': True,
        },
    }
    message = json.dumps(payload, ensure_ascii=False, sort_keys=True, separators=(',', ':')).encode('utf-8')
    signature = private_key.sign(message)
    payload['signature'] = base64.b64encode(signature).decode('ascii')
    Path(out_path).write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding='utf-8')


if __name__ == '__main__':
    print('Генератор повної ліцензії Mi100 Studio Manager')
    customer_name = input("Ім'я клієнта: ").strip()
    company_name = input('Компанія: ').strip()
    device_code = input('Код пристрою: ').strip()
    out_path = input('Куди зберегти файл [license.dat]: ').strip() or 'license.dat'
    generate_full_license(customer_name, company_name, device_code, out_path)
    print(f'Ліцензію збережено у: {out_path}')
