#!/usr/bin/env python3
"""License key generator for Meta Ads MCP Server.

This is a seller-side tool for generating license keys.
Not intended for distribution to end users.

Usage:
    meta-ads-keygen --tier pro --email user@example.com --expires 2027-01-01
"""

from __future__ import annotations

import argparse
import sys
from datetime import date, timedelta

from .licensing import generate_license_key, validate_license_key


def main():
    parser = argparse.ArgumentParser(
        description="Generate Meta Ads MCP license keys",
    )
    parser.add_argument(
        "--tier",
        choices=["pro", "enterprise"],
        default="pro",
        help="License tier (default: pro)",
    )
    parser.add_argument(
        "--email",
        required=True,
        help="Licensee email address",
    )
    parser.add_argument(
        "--expires",
        help="Expiry date YYYY-MM-DD (default: 1 year from today)",
    )
    parser.add_argument(
        "--verify",
        help="Verify an existing license key instead of generating one",
    )

    args = parser.parse_args()

    if args.verify:
        info = validate_license_key(args.verify)
        print(f"Tier:    {info.tier}")
        print(f"Email:   {info.email}")
        print(f"Expires: {info.expires}")
        print(f"Valid:   {info.valid}")
        if info.error:
            print(f"Error:   {info.error}")
        sys.exit(0 if info.valid else 1)

    expires = args.expires or (date.today() + timedelta(days=365)).isoformat()

    key = generate_license_key(
        tier=args.tier,
        email=args.email,
        expires=expires,
    )

    print(f"License Key Generated")
    print(f"---------------------")
    print(f"Tier:    {args.tier}")
    print(f"Email:   {args.email}")
    print(f"Expires: {expires}")
    print()
    print(f"LICENSE_KEY={key}")
    print()
    print("Add to MCP client config env:")
    print(f'  "LICENSE_KEY": "{key}"')


if __name__ == "__main__":
    main()
