from operation import Operation, TypeOperation


class Compte:
    def __init__(self, id: int, banque):
        self.id = id
        self.banque = banque
        self.solde = 0.00
        self.operations = []

    def crediter(self, montant: float):
        if montant <= 0:
            raise ValueError("Le montant doit être positif.")

        self.solde += montant

    def debiter(self, montant: float):
        if montant <= 0:
            raise ValueError("Le montant doit être positif.")

        if montant > self.solde:
            raise ValueError("Solde insuffisant.")

        self.solde -= montant

    def deposer(self, montant: float):
        self.crediter(montant)

        operation = Operation(
            id=self.banque.nouvel_id_operation(),
            date=self.banque.date_actuelle(),
            type=TypeOperation.DEPOT,
            montant=montant,
            compte_destination=self.id
        )
        print("opération deposer:",repr(operation))
        print("opération deposer:",operation)
        self.operations.append(operation)

    def retirer(self, montant: float):
        self.debiter(montant)

        operation = Operation(
            id=self.banque.nouvel_id_operation(),
            date=self.banque.date_actuelle(),
            type=TypeOperation.RETRAIT,
            montant=montant,
            compte_source=self.id
        )
        print("opération retrait:",repr(operation))
        print("opération retrait:",operation)
        self.operations.append(operation)

    def info(self):
        return f" {self.banque.nom} - Compte {self.id} - Solde : {self.solde:.2f} €"