import asyncio
import logging
import grpc
import os
import sys

# Add src to path for easy imports
sys.path.append(os.path.join(os.path.dirname(__file__), 'src'))
sys.path.append(os.path.dirname(__file__))

from api.servicer import BrowserServiceServicer
from protos import browser_pb2_grpc

async def serve():
    servicer = BrowserServiceServicer()
    await servicer.init_playwright()
    
    server = grpc.aio.server()
    browser_pb2_grpc.add_BrowserServiceServicer_to_server(servicer, server)
    server.add_insecure_port('[::]:50052')
    logging.info("Browser Service (Refactored) starting on port 50052...")
    await server.start()
    await server.wait_for_termination()

if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    asyncio.run(serve())
