import { connect } from "mongoose";
import ClientAccountManager from "../../Models/ClientAccounts/ClientAccount";
import {
    ClientAccountView,
    ClientAccountReport,
    FindClientAccountsParams,
    GetClientAccountReportParams,
    IClientAccount,
} from "../../Models/ClientAccounts/ClientAccounts.Interface";
import {
    CalculateClientAccountBalanceParams,
    CreateClientAccountMovementParams,
    IClientAccountMovement,
    PaginateClientAccountMovementsParams,
    PaginateClientAccountMovementsResults,
} from "../../Models/ClientAccountMovements/ClientAccountMovements.Interface";
import ClientAccountMovements from "../../Models/ClientAccountMovements/ClientAccountMovements";

export class ClientAccountsService {
    public async findById(data: FindClientAccountsParams): Promise<IClientAccount> {
        await connect(`${process.env.DATABASE_CONNECTION}`);

        const account = await ClientAccountManager.findById(data);

        return account;
    }

    public async calculateBalance(data: CalculateClientAccountBalanceParams): Promise<number> {
        await connect(`${process.env.DATABASE_CONNECTION}`);

        const balance = await ClientAccountMovements.calculateBalance(data);

        return balance;
    }

    public async addMovement(
        data: CreateClientAccountMovementParams
    ): Promise<IClientAccountMovement> {
        await connect(`${process.env.DATABASE_CONNECTION}`);

        const movement = await ClientAccountMovements.add(data);

        return movement;
    }

    public async paginateMovements(
        data: PaginateClientAccountMovementsParams
    ): Promise<PaginateClientAccountMovementsResults> {
        await connect(`${process.env.DATABASE_CONNECTION}`);

        const movements = await ClientAccountMovements.paginate(data);

        return movements;
    }

    public async clientAccountView(data: FindClientAccountsParams): Promise<ClientAccountView> {
        await connect(`${process.env.DATABASE_CONNECTION}`);

        const account = await ClientAccountManager.clientAccountView(data);

        return account;
    }

    public async getAccountReport(
        data: GetClientAccountReportParams
    ): Promise<ClientAccountReport> {
        await connect(`${process.env.DATABASE_CONNECTION}`);

        const report = await ClientAccountManager.getAccountReport(data);

        return report;
    }
}
