import {
    FindSchedulesEnabledParams,
    ISchedulesEnabledAdapter,
    ISchedulesEnabled,
    CreateSchedulesEnabledParams,
} from "./SchedulesEnabled.Interface";
import { Document, Model, Schema, model } from "mongoose";

export interface ISchedulesEnabledDocument extends Omit<ISchedulesEnabled, "id">, Document {}

export class SchedulesEnabledAdapterMongoose implements ISchedulesEnabledAdapter {
    schema: Schema;
    schedulesEnabledList: Model<ISchedulesEnabledDocument>;

    constructor() {
        this.schema = new Schema({
            employeeId: { type: Schema.Types.ObjectId, required: true, ref: "Employee" },
            companyId: { type: Schema.Types.ObjectId, required: true, ref: "Companie" },
            startDate: { type: Date, required: true },
            endDate: { type: Date, required: true },
            creationDate: { type: Date, required: true, default: Date.now },
        });

        this.schedulesEnabledList = model<ISchedulesEnabledDocument>(
            "SchedulesEnabled",
            this.schema
        );
    }

    public async create(data: CreateSchedulesEnabledParams): Promise<ISchedulesEnabled> {
        return await this.schedulesEnabledList.create(data);
    }

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

    public async findOne(
        filters: FindSchedulesEnabledParams
    ): Promise<ISchedulesEnabledDocument | null> {
        return this.schedulesEnabledList.findOne(filters).exec();
    }
}
