from sqlalchemy.orm import Session
from fastapi import HTTPException, status
from src.marketing.apps.Account import model, schema
from typing import Optional 
from sqlalchemy.orm import joinedload


# --- Master Account Services ---
def create_master_account(db: Session, data: schema.MasterAccountCreate):
    new_account = model.MasterAccount(**data.model_dump())
    db.add(new_account)
    db.commit()
    db.refresh(new_account)
    return new_account

def get_master_account(db: Session, id: int):
    return db.query(model.MasterAccount).filter(model.MasterAccount.id == id, model.MasterAccount.is_deleted == False).first()

def get_all_master_accounts(db: Session):
    query = db.query(model.MasterAccount).filter( model.MasterAccount.is_deleted == False)
    return query.all()

def update_master_account(db: Session, id: int, data: schema.MasterAccountUpdate):
    account = get_master_account(db, id)
    if not account:
        raise HTTPException(status_code=404, detail="Account not found")
    for field, value in data.dict(exclude_unset=True).items():
        setattr(account, field, value)
    db.commit()
    db.refresh(account)
    return account

def delete_master_account(db: Session, id: int):
    account = get_master_account(db, id)
    if not account:
        raise HTTPException(status_code=404, detail="Account not found")
    account.is_deleted = True
    db.commit()


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

# Create connected account
def create_connected_account(db:Session,data:schema.ConnectedAccountCreate):
    account = model.ConnectedAccount(**data.model_dump())
    db.add(account)
    db.commit()
    db.refresh(account)
    return account



# def create_connected_account(db: Session, data: schema.ConnectedAccountCreate):
#     existing_account = db.query(model.ConnectedAccount).filter_by(branch_id=data.branch_id).first()
#     if existing_account:
#         return None

#     account = model.ConnectedAccount(**data.model_dump())
#     db.add(account)
#     db.commit()
#     db.refresh(account)
#     return account




#get connected account by branch_id
def get_connected_account_by_brach_id(db: Session, branch_id: int):
    account_brach_id = db.query(model.ConnectedAccount).options(
        joinedload(model.ConnectedAccount.master_account)
    ).filter(
        model.ConnectedAccount.branch_id == branch_id,
        model.ConnectedAccount.is_deleted == False
    ).all()
    return account_brach_id

    


#get all master accounts by  id
def get_connect_accounts_by_id(db:Session, id:int):
    account_id = db.query(model.ConnectedAccount).filter(model.ConnectedAccount.id == id,model.ConnectedAccount.is_deleted==False).first()
    return account_id
    

#Update Connected account by id
def update_connected_account(db: Session, id: int, data: schema.ConnectedAccountUpdate):
    account = get_connect_accounts_by_id(db, id)
    if not account:
        raise HTTPException(status_code=404, detail="Connected account not found")
    
    update_data = data.model_dump(exclude_unset=True)  
    for field, value in update_data.items():
        setattr(account, field, value)

    db.commit()
    db.refresh(account)
    return account
    


#Delete Connected Account
def delete_account(db:Session, id:int):
    account_id = get_connect_accounts_by_id(db,id)
    if not account_id:
        return None
    # Soft delete the account by setting is_deleted to True
    # account_id.is_deleted = True
    db.delete(account_id)
    db.commit()
    return account_id
    
