from datetime import datetime
from compte import Compte
from decimal import Decimal

class Banque:
    def __init__(self, id: int, nom: str):
        self.id = id
        self.nom = nom
        self.comptes = {}

        self._prochain_id_compte = 1
        self._prochain_id_operation = 1

    def creer_compte(self) -> Compte:
        id_compte = self._prochain_id_compte
        self._prochain_id_compte += 1

        compte = Compte(id_compte, self)
        self.comptes[id_compte] = compte

        return compte

    def rechercher_compte(self, id_compte: int) -> Compte:
        if id_compte not in self.comptes:
            raise ValueError(f"Le compte {id_compte} n'existe pas.")

        return self.comptes[id_compte]

    def nouvel_id_operation(self) -> int:
        id_operation = self._prochain_id_operation
        self._prochain_id_operation += 1

        return id_operation

    def date_actuelle(self):
        return datetime.now()

    def virer(
        self,
        id_compte_source: int,
        banque_destination,
        id_compte_destination: int,
        montant : Decimal
    ):
        compte_source = self.rechercher_compte(id_compte_source)
        compte_destination = banque_destination.rechercher_compte(
            id_compte_destination
        )

        compte_source.debiter(montant)
        compte_destination.crediter(montant)

    def __str__(self):
        return f"Banque {self.id} - {self.nom}"