import { connect } from "mongoose";
import RepeatsList from "../../Models/Repeats/Repeats";
import {
    AddRepeatDisabledDateParams,
    CreateRepeatsParams,
    DeleteRepeatsParams,
    FindRepeatsParams,
    IRepeat,
    RepeatView,
    GetPendingRepeatsParams,
    PendingRepeatView,
} from "../../Models/Repeats/Repeats.Interface";
import { validateSessionUser, validatePermissionsByCompany } from "../../helpers/check";

export class RepeatsService {
    public async createRepeat(data: CreateRepeatsParams): Promise<IRepeat> {
        await connect(`${process.env.DATABASE_CONNECTION}`);

        const newRepeat = await RepeatsList.createRepeat(data);

        return newRepeat;
    }

    public async deleteRepeat(data: DeleteRepeatsParams): Promise<void> {
        await connect(`${process.env.DATABASE_CONNECTION}`);

        await RepeatsList.deleteRepeat(data);
    }

    public async getRepeatsByClient(data: FindRepeatsParams): Promise<RepeatView[]> {
        await connect(`${process.env.DATABASE_CONNECTION}`);

        return await RepeatsList.getRepeatsByClient(data);
    }

    public async addRepeatDisabledDate(data: AddRepeatDisabledDateParams): Promise<void> {
        await connect(`${process.env.DATABASE_CONNECTION}`);

        await RepeatsList.addDisabledDate(data);
    }

    public async getPendingRepeatsCurrentWeek(data: GetPendingRepeatsParams): Promise<PendingRepeatView[]> {
        await connect(`${process.env.DATABASE_CONNECTION}`);

        await validateSessionUser({ sessionUser: data.sessionUser });
        if (data.companyId) {
            await validatePermissionsByCompany({ companyId: data.companyId, sessionUser: data.sessionUser });
        }

        return await RepeatsList.getPendingRepeatsCurrentWeek(data);
    }
}
