import requests
from scripts.init_db import run_script
from scripts.create_order_example import create_multiple_orders

API_URL = "http://127.0.0.1:8000"

# Elimina todas las órdenes
def delete_all_orders():
    resp = requests.get(f"{API_URL}/orders")
    if resp.status_code == 200:
        orders = resp.json()
        for order in orders:
            order_id = order['id']
            del_resp = requests.delete(f"{API_URL}/orders/{order_id}/")
            if del_resp.status_code == 200:
                print(f"Orden {order_id} eliminada.")
            else:
                print(f"Error eliminando orden {order_id}: {del_resp.status_code} {del_resp.text}")
    else:
        print("Error obteniendo órdenes para eliminar:", resp.status_code, resp.text)

# Elimina todos los productos
def delete_all_products():
    resp = requests.get(f"{API_URL}/products")
    if resp.status_code == 200:
        products = resp.json()
        for product in products:
            product_id = product['id']
            del_resp = requests.delete(f"{API_URL}/products/{product_id}/")
            if del_resp.status_code == 200:
                print(f"Producto {product_id} eliminado.")
            else:
                print(f"Error eliminando producto {product_id}: {del_resp.status_code} {del_resp.text}")
    else:
        print("Error obteniendo productos para eliminar:", resp.status_code, resp.text)

# Elimina todas las mesas
def delete_all_tables():
    resp = requests.get(f"{API_URL}/tables")
    if resp.status_code == 200:
        tables = resp.json()
        for table in tables:
            table_id = table['id']
            del_resp = requests.delete(f"{API_URL}/tables/{table_id}/")
            if del_resp.status_code == 200:
                print(f"Mesa {table_id} eliminada.")
            else:
                print(f"Error eliminando mesa {table_id}: {del_resp.status_code} {del_resp.text}")
    else:
        print("Error obteniendo mesas para eliminar:", resp.status_code, resp.text)

# Elimina todos los usuarios
def delete_all_users():
    resp = requests.get(f"{API_URL}/users")
    if resp.status_code == 200:
        users = resp.json()
        for user in users:
            user_id = user['id']
            del_resp = requests.delete(f"{API_URL}/users/{user_id}/")
            if del_resp.status_code == 200:
                print(f"Usuario {user_id} eliminado.")
            else:
                print(f"Error eliminando usuario {user_id}: {del_resp.status_code} {del_resp.text}")
    else:
        print("Error obteniendo usuarios para eliminar:", resp.status_code, resp.text)

# Elimina todos los camareros
def delete_all_waiters():
    resp = requests.get(f"{API_URL}/waiters")
    if resp.status_code == 200:
        waiters = resp.json()
        for waiter in waiters:
            waiter_id = waiter['id']
            del_resp = requests.delete(f"{API_URL}/waiters/{waiter_id}")
            if del_resp.status_code == 200:
                print(f"Camarero {waiter_id} eliminado.")
            else:
                print(f"Error eliminando camarero {waiter_id}: {del_resp.status_code} {del_resp.text}")
    else:
        print("Error obteniendo camareros para eliminar:", resp.status_code, resp.text)

def remake_database(algorithm):
    print("Vaciando base de datos...")
    delete_all_orders()
    delete_all_waiters()
    print("Base de datos vaciada.")
    print("Creating new data")
    run_script("simulation/scripts/create_waiters.py")
    # Si quieres generar órdenes aleatorias, descomenta la siguiente línea:
    create_multiple_orders(user_id=1, n_orders=100, algorithm=algorithm)
    print("Base de datos recreada.")

