import { connect } from "mongoose";
import UsersList from "../../Models/Users/Users";
import {
    SetUserFileParams,
    UpdateUserParams,
    FindUsersParams,
    PaginateUsersParams,
    PaginateUsersResults,
    IUserView,
    CollaboratorView,
    ChangePasswordParams,
    FindCollaboratorByEmailParams,
    DeleteUserParams,
    SavePayerEmailParams,
    GetPayerEmailParams,
    PayerEmailResult,
    CheckUserPhoneParams,
    UserAssistanceInfo,
} from "../../Models/Users/Users.Interface";

export class UsersService {
    public async checkUserPhone(data: CheckUserPhoneParams): Promise<boolean> {
        await connect(`${process.env.DATABASE_CONNECTION}`);
        return await UsersList.checkUserPhone(data);
    }

    public async setPayerEmail(data: SavePayerEmailParams): Promise<void> {
        await connect(`${process.env.DATABASE_CONNECTION}`);
        await UsersList.savePayerEmail(data);
    }

    public async getPayerEmail(data: GetPayerEmailParams): Promise<PayerEmailResult> {
        await connect(`${process.env.DATABASE_CONNECTION}`);
        const result = await UsersList.getPayerEmail(data);
        return result;
    }

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

    public async paginateUsers(data: PaginateUsersParams): Promise<PaginateUsersResults> {
        await connect(`${process.env.DATABASE_CONNECTION}`);

        const users = await UsersList.paginate(data);

        return users;
    }

    public async changeUserPassword(data: ChangePasswordParams): Promise<void> {
        await connect(`${process.env.DATABASE_CONNECTION}`);
        await UsersList.changePassword(data);
    }

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

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

    public async getUserById(data: FindUsersParams): Promise<IUserView> {
        await connect(`${process.env.DATABASE_CONNECTION}`);
        const user = await UsersList.findOne(data);
        return user;
    }

    public async findByEmail(
        data: FindCollaboratorByEmailParams
    ): Promise<CollaboratorView | null> {
        await connect(`${process.env.DATABASE_CONNECTION}`);
        const user = await UsersList.findByEmail(data);
        return user;
    }

    public async getUserAssistanceInfo(data: FindUsersParams): Promise<UserAssistanceInfo> {
        await connect(`${process.env.DATABASE_CONNECTION}`);
        const info = await UsersList.getUserAssistanceInfo(data);
        return info;
    }
}
