# from fastapi import HTTPException
# from app.core.database import user_collection
# from app.core.security import hash_password, verify_password, create_access_token

# async def register_user(phone: str, password: str): 
#     existing = await user_collection.find_one({"phone": phone})
  
#     if not phone or not password: 
#         raise HTTPException(status_code=400, detail="Phone number and password are required")

#     if existing: 
#         raise HTTPException(status_code=400, detail="Phone number already registered")
    
#     hashed = hash_password(password)

#     user = {
#         "phone": phone,   
#         "password": hashed
#     }

#     await user_collection.insert_one(user)

#     return {"status_code": 201, "message": "User registered successfully"}

# async def login_user(phone: str, password: str): 
#     user = await user_collection.find_one({"phone": phone})
     
#     if not phone or not password: 
#         raise HTTPException(status_code=400, detail="Phone number and password are required")

#     if not user: 
#         raise HTTPException(status_code=400, detail="Invalid phone number or password")
    
#     if not verify_password(password, user["password"]): 
#         raise HTTPException(status_code=400, detail="Invalid phone number or password")
    
#     # token_data = {"sub": user["phone"]}
#     token = create_access_token({"phone": phone})

#     return {"access_token": token, "token_type": "bearer"}


from fastapi import HTTPException

import re

from app.core.database import (
    user_collection
)

from app.core.security import (
    hash_password,
    verify_password,
    create_access_token
)


phone_pattern = r"^[6-9]\d{9}$"


async def register_user(
    phone: str,
    password: str
):

    if not phone or not password:

        raise HTTPException(
            status_code=400,
            detail="Phone and password required"
        )

    if not re.match(
        phone_pattern,
        phone
    ):

        raise HTTPException(
            status_code=400,
            detail="Invalid phone number"
        )

    if len(password) < 6:

        raise HTTPException(
            status_code=400,
            detail="Password must be at least 6 characters"
        )

    existing = await user_collection.find_one({
        "phone": phone
    })

    if existing:

        raise HTTPException(
            status_code=400,
            detail="Phone already registered"
        )

    hashed = hash_password(password)

    user = {
        "phone": phone,
        "password": hashed
    }

    await user_collection.insert_one(
        user
    )

    return {
        "message": "User registered successfully"
    }


async def login_user(
    phone: str,
    password: str
):

    user = await user_collection.find_one({
        "phone": phone
    })

    if not user:

        raise HTTPException(
            status_code=400,
            detail="Invalid phone or password"
        )

    if not verify_password(
        password,
        user["password"]
    ):

        raise HTTPException(
            status_code=400,
            detail="Invalid phone or password"
        )

    token = create_access_token({
        "phone": phone
    })

    return {
        "access_token": token,
        "token_type": "bearer",
        "expires_in": 86400
    }