#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
TOOLS.py - Main utility script for the website application.

This script provides a command-line interface to all utilities 
for database management, user management, and server administration.

Usage:
  python3 TOOLS.py [command] [options]

Available commands:
  - db:fix           Fix database schema
  - db:rebuild       Completely rebuild the database
  - db:test          Test database connection and models
  - db:stats         Show database statistics
  
  - user:list        List all users
  - user:create      Create a new user
  - user:admin       Create admin user (username: admin, password: admin)
  - user:reset-pw    Reset user password
  - user:delete      Delete a user
  
  - server:run       Run the development server

Examples:
  python3 TOOLS.py db:rebuild
  python3 TOOLS.py user:admin
  python3 TOOLS.py server:run --port 8080
"""

import os
import sys
import argparse
from utils import (
    fix_database_schema, rebuild_database, run_all_tests, print_database_stats,
    list_users, create_user, reset_password, delete_user, create_admin_user,
    run_development_server
)

def parse_args():
    parser = argparse.ArgumentParser(
        description='Website Administration Tools',
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog=__doc__
    )
    
    # Main command argument
    parser.add_argument('command', help='Command to execute')
    
    # Additional arguments
    parser.add_argument('--username', '-u', help='Username for user commands')
    parser.add_argument('--email', '-e', help='Email for user creation')
    parser.add_argument('--password', '-p', help='Password for user creation/reset')
    parser.add_argument('--admin', '-a', action='store_true', help='Make user an admin')
    parser.add_argument('--host', help='Host for server (default: 127.0.0.1)')
    parser.add_argument('--port', type=int, help='Port for server (default: 5000)')
    parser.add_argument('--no-debug', action='store_true', help='Disable debug mode for server')
    
    return parser.parse_args()

def main():
    args = parse_args()
    
    # Database commands
    if args.command == 'db:fix':
        fix_database_schema()
    
    elif args.command == 'db:rebuild':
        print("WARNING: This will delete all data in the database!")
        confirm = input("Are you sure you want to continue? (y/n): ").lower()
        if confirm == 'y':
            rebuild_database()
        else:
            print("Aborted.")
    
    elif args.command == 'db:test':
        run_all_tests()
    
    elif args.command == 'db:stats':
        print_database_stats()
    
    # User commands
    elif args.command == 'user:list':
        list_users()
    
    elif args.command == 'user:create':
        if not args.username or not args.email or not args.password:
            print("Error: Username, email, and password are required.")
            print("Example: python3 TOOLS.py user:create -u username -e email -p password [-a]")
            sys.exit(1)
        create_user(args.username, args.email, args.password, args.admin)
    
    elif args.command == 'user:admin':
        create_admin_user()
    
    elif args.command == 'user:reset-pw':
        if not args.username or not args.password:
            print("Error: Username and password are required.")
            print("Example: python3 TOOLS.py user:reset-pw -u username -p new_password")
            sys.exit(1)
        reset_password(args.username, args.password)
    
    elif args.command == 'user:delete':
        if not args.username:
            print("Error: Username is required.")
            print("Example: python3 TOOLS.py user:delete -u username")
            sys.exit(1)
        delete_user(args.username)
    
    # Server commands
    elif args.command == 'server:run':
        host = args.host or '127.0.0.1'
        port = args.port or 5000
        debug = not args.no_debug
        run_development_server(host=host, port=port, debug=debug)
    
    else:
        print(f"Unknown command: {args.command}")
        print("Run 'python3 TOOLS.py -h' for usage information")
        sys.exit(1)

if __name__ == '__main__':
    main() 