#!/usr/bin/env python3
"""
One-time Gumroad Product Setup.
Creates the 3 core products on Gumroad.
Run once: python scripts/gumroad_setup.py
"""

import os, sys, json
from datetime import datetime

GUMROAD_TOKEN = os.environ.get("GUMROAD_ACCESS_TOKEN", "")

if not GUMROAD_TOKEN:
    print("ERROR: Set GUMROAD_ACCESS_TOKEN environment variable")
    print("Get it from: https://gumroad.com/applications")
    sys.exit(1)

headers = {
    "Authorization": f"Bearer {GUMROAD_TOKEN}",
    "Content-Type": "application/json",
}

BASE_URL = "https://api.gumroad.com/v2"

products = [
    {
        "name": "Islamic UI Kit",
        "price": 1000,  # in cents = $10.00
        "description": """47 Islamic UI components for React, Vue, Svelte, and HTML.

## What's Included
- Prayer time display component
- Qibla compass widget
- Quran reader (Arabic + translation)
- Hijri date picker
- Tasbih counter
- 99 Names explorer
- Dua cards
- Halal badge
- Zakat calculator
- Masjid finder card
- Dark mode + RTL support

## Usage
Copy-paste into any project. Works with React, Vue, Svelte, or plain HTML.

## License
One-time purchase. Lifetime updates. No subscription.""",
        "native_price": 1000,
        "currency": "USD",
        "type": "digital",
    },
    {
        "name": "FalahOS Starter Bundle",
        "price": 4900,  # in cents = $49.00
        "description": """The complete system for building Islamic digital products.

## What's Included
1. **Islamic UI Kit** (47 components, React/Vue/Svelte/HTML) — $10 value
2. **Halal Business Canvas** (7 monetization models, no ads/riba) — $17 value
3. **Quran API Integration Guide** (12 APIs compared, copy-paste ready) — $12 value
4. **Prayer Times Implementation** (3 libraries compared, offline-first) — $10 value
5. **Launch Checklist** (halal hosting, analytics, payments) — $17 value

## Total Value: $66+
## Your Price: $49

## Who This Is For
- Muslim developers building apps for the Ummah
- Islamic startups launching their first product
- Designers wanting to build halal-compliant interfaces
- Entrepreneurs entering the Islamic tech space

## Guarantee
30-day money-back guarantee. If it doesn't save you 10+ hours, refund — no questions.""",
        "native_price": 4900,
        "currency": "USD",
        "type": "bundle",
    },
    {
        "name": "Sadaqah",
        "price": 500,  # in cents = $5.00
        "description": """Voluntary contribution to support FalahOS development.

FalahOS creates free Islamic digital tools for the Muslim community.
Every sadaqah helps us build more, give more, serve more.

May Allah accept your contribution. 🤲

JazakAllah khair.""",
        "native_price": 500,
        "currency": "USD",
        "type": "donation",
    },
]

print(f"[{datetime.now()}] Setting up Gumroad products...")

for product in products:
    print(f"\nCreating: {product['name']} (${product['price']/100:.2f})")

    try:
        r = requests.post(
            f"{BASE_URL}/products",
            headers=headers,
            json=product,
            timeout=20,
        )

        if r.status_code == 200:
            data = r.json()
            print(f"  ✓ Created: {data.get('url', 'unknown URL')}")
        elif r.status_code == 422:
            # Product might already exist
            print(f"  ⚠ Already exists or validation error: {r.text[:200]}")
        else:
            print(f"  ✗ Failed ({r.status_code}): {r.text[:200]}")

    except Exception as e:
        print(f"  ✗ Error: {e}")

print("\nDone. Check https://alfalahtech.gumroad.com to verify.")