================================================================================
                        DSJ WEB APP - DEPLOYMENT GUIDE
================================================================================

FOLDER STRUCTURE
================================================================================

  dsj_app/
    app.py              - Flask backend (all routes + API)
    requirements.txt    - Python dependencies
    Procfile            - Railway start command
    railway.json        - Railway config
    templates/
      base.html         - Shared layout
      login.html        - Login page
      register.html     - Register page
      dashboard.html    - User dashboard
      admin.html        - Admin panel

================================================================================
STEP 1 — TEST LOCALLY
================================================================================

  1. Install dependencies:
       pip install flask flask-sqlalchemy flask-login werkzeug stripe gunicorn

  2. Run the app:
       python app.py

  3. Open: http://localhost:5000
  4. Default admin login:
       Username: admin
       Password: admin123
       ⚠️ Change this immediately after first login via the admin panel!

================================================================================
STEP 2 — DEPLOY TO RAILWAY
================================================================================

  1. Go to https://railway.app and sign up (free)
  2. Click "New Project" > "Deploy from GitHub repo"
     OR click "New Project" > "Empty Project" then drag the dsj_app folder

  3. Set these environment variables in Railway dashboard:
       SECRET_KEY         = (any long random string, e.g. abc123xyz789...)
       API_KEY            = (secret key your scripts use, e.g. dsj-my-secret-key)
       STRIPE_SECRET_KEY  = (from Stripe dashboard — leave blank until ready)
       STRIPE_WEBHOOK_SECRET = (from Stripe — leave blank until ready)
       BASE_URL           = https://your-app.up.railway.app
       SCRIPTS_DIR        = C:\\Scripts

  4. Railway will automatically deploy and give you a public URL

================================================================================
STEP 3 — SET UP STRIPE (WHEN READY)
================================================================================

  1. Go to https://stripe.com and create an account
  2. Get your Secret Key from: Developers > API Keys
  3. Add it to Railway environment variables as STRIPE_SECRET_KEY
  4. Set up a webhook in Stripe:
       - Go to Developers > Webhooks > Add Endpoint
       - URL: https://your-app.up.railway.app/stripe/webhook
       - Event: checkout.session.completed
       - Copy the webhook signing secret to STRIPE_WEBHOOK_SECRET in Railway

================================================================================
STEP 4 — UPDATE THE SCRIPTS
================================================================================

  Update automate_login.py and automated_entry.py to pull accounts
  from the web app instead of Google Sheets.

  Add these config variables at the top of each script:

      API_URL = "https://your-app.up.railway.app"
      API_KEY = "your-api-key-here"   # must match API_KEY in Railway

  Replace load_accounts() with:

      import requests

      def load_accounts():
          r = requests.get(f"{API_URL}/api/accounts",
                           headers={"X-API-Key": API_KEY})
          return r.json()["accounts"]

  After each successful run, call deduct endpoint:

      def deduct_token(user_id, phone):
          requests.post(f"{API_URL}/api/deduct",
                        headers={"X-API-Key": API_KEY},
                        json={"user_id": user_id, "phone": phone})

================================================================================
ADMIN PANEL
================================================================================

  URL:  https://your-app.up.railway.app/admin
  Login with your admin credentials.

  From the admin panel you can:
  - View all registered users and their token balances
  - Manually add or remove tokens from any user
  - Add/delete token packages (shown on user dashboard)
  - Delete users

================================================================================
API ENDPOINTS (for scripts)
================================================================================

  GET  /api/accounts
       Header: X-API-Key: your-key
       Returns all active phone numbers for users with tokens > 0

  POST /api/deduct
       Header: X-API-Key: your-key
       Body:   {"user_id": 1, "phone": "8082309112"}
       Deducts 1 token from the user after a successful run

================================================================================
                              END OF DEPLOYMENT GUIDE
================================================================================
