# app/services/client.py

# import uuid
# from sqlalchemy.orm import Session
# from app.models.client import Client
# from app.schemas.client import ClientCreate

# def create_client(db: Session, data: ClientCreate):
    
#     widget_id = f"XRG-{uuid.uuid4().hex[:8]}"

#     new_client = Client( client_widget_id=widget_id, website_url=data.website_url, client_name=data.client_name )

#     db.add(new_client)
#     db.commit()
#     db.refresh(new_client)
    
#     return new_client

# def get_client_by_widget(db: Session, widget_id: str):
#     return db.query(Client).filter(Client.client_widget_id == widget_id).first()

# ------------------------------------------------------------------------------------------------------------------------------

# # app/services/client.py

# import uuid
# from sqlalchemy.orm import Session
# from app.models.client import Client
# from app.schemas.client import ClientCreate

# async def create_client(db: Session, data: ClientCreate):
#     """
#     Create a new client with a unique widget ID.
#     """
#     print(">>>>>>>>>>>>>>>>>>>",data)
#     widget_id = f"XRG-{uuid.uuid4().hex[:8]}"

#     new_client = Client(
#         client_widget_id=widget_id,
#         website_url=data.website_url,
#         client_name=data.client_name,
#     )

#     db.add(new_client)
#     await db.commit()
#     await db.refresh(new_client)
#     return new_client


# def get_client_by_widget(db: Session, widget_id: str):
#     """
#     Fetch client using widget ID (used by widget-loader & visitors).
#     """
#     return db.query(Client).filter(Client.client_widget_id == widget_id).first()


# def get_client_by_id(db: Session, client_id: int):
#     """
#     Fetch client using numeric database ID (used by message + visitors).
#     """
#     return db.query(Client).filter(Client.id == client_id).first()


# ---------------------------------------------------------------------------------------------------------------------------

# app/services/client.py

import uuid
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select
from app.models.client import Client
from app.schemas.client import ClientCreate


async def create_client(db: AsyncSession, data: ClientCreate):
    """
    Create a new client with a unique widget ID.
    Fully async version.
    """
    
    widget_id = f"XRG-{uuid.uuid4().hex[:8]}"

    new_client = Client(
        client_widget_id=widget_id,
        website_url=data.website_url,
        client_name=data.client_name,
    )

    db.add(new_client)
    await db.commit()
    await db.refresh(new_client)
    # Add script tag dynamically
    new_client.script_tag = (
        f'<script src="https://livechat.menuwing.net/widget-loader.js" '
        f'data-client-id="{new_client.client_widget_id}" async></script>'
    )
    # new_client.script_tag = (
    #     f'<script src="http://localhost:5173/widget-loader.js" '
    #     f'data-client-id="{new_client.client_widget_id}" async></script>'
    # )
    return new_client


async def get_client_by_widget(db: AsyncSession, widget_id: str):
    """
    Async: Fetch client using widget ID.
    """
    result = await db.execute(
        select(Client).where(Client.client_widget_id == widget_id)
    )
    return result.scalar_one_or_none()


async def get_client_by_id(db: AsyncSession, client_id: int):
    """
    Async: Fetch client using numeric ID.
    """
    result = await db.execute(
        select(Client).where(Client.id == client_id)
    )
    return result.scalar_one_or_none()
