import {
    FindClientAccountsParams,
    IClientAccountsAdapter,
    IClientAccount,
    CreateClientAccountParams,
} from "./ClientAccounts.Interface";
import { Document, Model, Schema, model } from "mongoose";

export interface IClientAccountDocument
    extends Omit<IClientAccount, "id">,
        Document {}

export class ClientAccountsAdapterMongoose implements IClientAccountsAdapter {
    schema: Schema;
    clientAccountList: Model<IClientAccountDocument>;

    constructor() {
        this.schema = new Schema({
            clientId: {
                type: Schema.Types.ObjectId,
                required: true,
                ref: "Client",
            },
            amount: { type: Number, required: true, default: 0 },
            creationDate: { type: Date, required: true, default: Date.now },
        });

        this.clientAccountList = model<IClientAccountDocument>(
            "ClientAccount",
            this.schema
        );
    }

    public async create(
        data: CreateClientAccountParams
    ): Promise<IClientAccount> {
        return await this.clientAccountList.create(data);
    }

    public async delete(id: string): Promise<void> {
        await this.clientAccountList.deleteOne({ _id: id });
    }

    public async find(
        filters: FindClientAccountsParams
    ): Promise<IClientAccount[]> {
        return this.clientAccountList.find(filters).exec();
    }

    public async findOne(
        filters: Omit<FindClientAccountsParams, "sessionUser">
    ): Promise<IClientAccountDocument | null> {
        return this.clientAccountList.findOne(filters).exec();
    }
}
