from typing import Dict
from langchain.schema.document import Document

from qdrant import QwinQdrantClient
from db import QwinCasinoDao

customer_template = '''Customer info:
- customer registration is confirmed: {}
- customer is valid: {}
- customer is blocked: {}   
- customer is allowed to withdraw: {}
'''

active_bonus_template = '''Customer got a bonus but did not filfull all bonus obligations:
- bonus amount: {} qwinB
- bonus wager coefficient: {}
- customer staked: {} qwinB
- remain to stake: {} qwinB
Customer must place bets for amount {} qwinB. after which the bonus obligations will be fulfilled and withdrawal will be allowed.
'''    

def customer_details(customer: Dict) -> str:
    return customer_template.format(
        customer['reg_confirm'],
        customer['valid'],
        customer['blocked'],
        customer['withdraw_allowed']
    )

def active_bonus_details(bonus: Dict) -> str:
    return active_bonus_template.format(
        bonus['bonus_amount'],
        bonus['wager_factor'],
        bonus['player_staked'],
        bonus['to_stake'],
        bonus['to_stake']
    )


class FinanceInfoProvider:
    def __init__(self, qdrant: QwinQdrantClient, db: QwinCasinoDao):
        self.qdrant = qdrant
        self.db = db
        
    def customer_doc(self, user_id: str):
        customer = self.db.get_customer(user_id)
        return Document(page_content=customer_details(customer))

class DepositInfoProvider(FinanceInfoProvider):
    def __init__(self, qdrant: QwinQdrantClient, db: QwinCasinoDao):
        super().__init__(qdrant, db)
        
    def deposit_info(self, user_id: str, query: Dict) -> [Document]:
        customer = None
        if user_id.isdigit():
            customer = self.db.get_customer(user_id)
            
        if not customer:
            return [Document(page_content='User is not logged. Ask your to login before continuing.')]
        
        return [Document(page_content='Ask user to provide more details: transaction address in blockchain, amount, transaction time')]

class WithdrawalInfoProvider(FinanceInfoProvider):
    def __init__(self, qdrant: QwinQdrantClient, db: QwinCasinoDao):
        super().__init__(qdrant, db)
        
    def withdrawal_info(self, user_id: str, query: Dict) -> [Document]:
        customer = None
        if user_id.isdigit():
            customer = self.db.get_customer(user_id)
            
        documents: Document = []
        
        documents.append(Document(page_content=customer_details(customer)))
        
        if customer and not customer['withdraw_allowed']:
            marketing_docs = self.qdrant.search_marketing_docs()
            documents.extend(marketing_docs)
                        
            active_bonus = self.db.get_customer_active_bonus(user_id)
            if active_bonus:
                documents.append(Document(page_content=active_bonus_details(active_bonus)))    
            
        return documents