from fastapi import HTTPException, status
from sqlalchemy.orm import Session
from src.marketing.apps.Account import service
from src.marketing.apps.Account import schema
from typing import Optional
from src.utils.response import APIResponse



# --- Master Account Controllers ---
def create_master_account_controller(data: schema.MasterAccountCreate, db: Session) -> schema.MasterAccountOut:
    try:
        return service.create_master_account(db, data)
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

# Get a master account by ID
def get_master_account_controller(id: int, db: Session):
    try:
        account = service.get_master_account(db, id)
        if not account:
            return HTTPException(
                status_code=404,
                detail={
                    "success": False,
                    "message": "Master account not found",
                    "data": []
                }
            )
        # return schema.MasterAccountOut.model_validate(account)
        return account
    except HTTPException as e:
        raise e
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))
    
# Get all master accounts
def get_all_master_accounts_controller( db: Session):
    try:
        result =  service.get_all_master_accounts(db)
        if not result:
            raise HTTPException(
                status_code=404,
                detail={
                    "success": False,
                    "message": "No master accounts found",
                    "data": []
                }
            )
        return result
    except HTTPException as e:
        raise e
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

# Update a master account BY ID
def update_master_account_controller(id: int, data: schema.MasterAccountUpdate, db: Session):
    return service.update_master_account(db, id, data)

# Delete a master account by ID
def delete_master_account_controller(id: int, db: Session):
    service.delete_master_account(db, id)
    return {"detail": "Deleted successfully"}



#------------Connected Accounts Controllers -----------------------------------

def create_connedted_account_controller(db:Session, data:schema.ConnectedAccountCreate):
    try:
        return service.create_connected_account(db,data)
    
    except HTTPException as e:
        raise HTTPException(status_code =500, detail=str(e))
    


# def create_connedted_account_controller(db: Session, data: schema.ConnectedAccountCreate):
#     try:
#         result = service.create_connected_account(db, data)
#         if result is None:
#             raise HTTPException(
#                 status_code=status.HTTP_203_NON_AUTHORITATIVE_INFORMATION,
#                 detail={
#                     "success": False,
#                     "message": f"This branch_id: {data.branch_id} has already connected with social media",
#                     "data": None
#                 }
#             )
        
#         return APIResponse(  
#             success=True,
#             message="Connected account created successfully",
#             data=result
#         )

#     except HTTPException as e:
#         raise e
#     except Exception as e:
#         raise HTTPException(
#             status_code=500,
#             detail={
#                 "success": False,
#                 "message": "Internal server error",
#                 "data": str(e)
#             }
#         )




def get_connected_account_by_branch_id_controller(db: Session, branch_id:int):
    try:
        connected_account = service.get_connected_account_by_brach_id(db, branch_id)
        if not connected_account:
            raise HTTPException(
                status_code=404,
                detail={
                    "success": False,
                    "message": "No Account found for this branch",
                    "data": []
                }
            )
    
        return [schema.ConnectedAccountOut.model_validate(acc) for acc in connected_account]
    except HTTPException as e:
        raise e
    except Exception as e:
        raise HTTPException(
            status_code=500,
            detail={
                "success": False,
                "message": "Internal server error",
                "data": str(e)
            }
        )
    

def get_all_connected_account_controller(db, id:int):
     
    # if not connect_account_id:
    #     raise HTTPException(status_code=404, detail="Connected account not found")
    # return 
    try:
        connect_account_id = service.get_connect_accounts_by_id(db,id)
        if not connect_account_id:
            raise HTTPException(status_code=404, detail={
                "success": False,
                "message": "Connected account not found",
                "data": []
            })
        return schema.ConnectedAccountOut.model_validate(connect_account_id)
    except HTTPException as e:
        raise e
    except Exception as e:
        raise HTTPException(
            status_code=500,
            detail={
                "success": False,
                "message": "Internal server error",
                "data": str(e)
            }
        )
    

def updated_connected_account_controller(db:Session, id:int,data:schema.ConnectedAccountUpdate):
    return service.update_connected_account(db,id,data)

def deleted_connected_account_controller(db: Session, id: int):
    try:
        return service.delete_account(db, id)
    except HTTPException as e:
        raise e
    except Exception as e:
        raise HTTPException(
            status_code=500,
            detail={
                "success": False,
                "message": "Internal server error",
                "data": str(e)
            }
        )
    
    







