import { connect } from "mongoose";
import EmployeesList from "../../Models/Employees/Employee";
import {
    CreateEmployeeParams,
    FindEmployeesParams,
    IEmployee,
    ValidateEmployeeParams,
    PaginateEmployeesParams,
    PaginateEmployeesResults,
    CompanyEmployeesView,
    FindEmployeesByIdParams,
    UpdateEmployeeRolesParams,
    UpdateEmployeeParams,
    IncompleteCollaboratorView,
} from "../../Models/Employees/Employees.Interface";
import {
    CreateEmployeeServiceParams,
    UpdateEmployeeServiceParams,
    FindEmployeesServicesParams,
    FindCollaboratorServicesParams,
    DeleteEmployeeServiceParams,
    IEmployeeService,
    CollaboratorServiceView,
    UpdateCollaboratorServicesParams,
    FindEmployeesByServiceParams,
} from "../../Models/EmployeesServices/EmployeesServices.Interface";

import EmployeesServicesList from "../../Models/EmployeesServices/EmployeesServices";
import { TextObjectFilterResult } from "../../Models/TextObjectFilter.model";

export class EmployeesService {
    public async createEmployee(data: CreateEmployeeParams): Promise<IEmployee> {
        await connect(`${process.env.DATABASE_CONNECTION}`);

        const newEmployee = await EmployeesList.createEmployee(data);

        return newEmployee;
    }

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

        await EmployeesList.updateEmployeeRoles(data);
    }

    public async findAllServicesByCollaborator(
        data: FindCollaboratorServicesParams
    ): Promise<CollaboratorServiceView> {
        await connect(`${process.env.DATABASE_CONNECTION}`);
        const services = await EmployeesServicesList.findAllServicesByCollaborator(data);
        return services;
    }

    public async findByCompanyId(data: FindEmployeesParams): Promise<CompanyEmployeesView[]> {
        await connect(`${process.env.DATABASE_CONNECTION}`);

        const employees = await EmployeesList.findByCompanyId(data);

        return employees;
    }

    public async findById(data: FindEmployeesByIdParams): Promise<CompanyEmployeesView> {
        await connect(`${process.env.DATABASE_CONNECTION}`);

        const employee = await EmployeesList.findById(data);

        return employee;
    }

    public async paginateEmployees(
        data: PaginateEmployeesParams
    ): Promise<PaginateEmployeesResults> {
        await connect(`${process.env.DATABASE_CONNECTION}`);

        const companies = await EmployeesList.paginate(data);

        return companies;
    }

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

        await EmployeesList.validateGuest(data);
    }

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

        await EmployeesList.rejectGuest(data);
    }

    public async getIncompleteSetup(sessionUser: string): Promise<IncompleteCollaboratorView[]> {
        await connect(`${process.env.DATABASE_CONNECTION}`);

        return await EmployeesList.getIncompleteSetup(sessionUser);
    }

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

        await EmployeesList.validateHost(data);
    }

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

    public async addService(data: CreateEmployeeServiceParams): Promise<IEmployeeService> {
        await connect(`${process.env.DATABASE_CONNECTION}`);

        const newEmployeeService = await EmployeesServicesList.createEmployeeService(data);

        return newEmployeeService;
    }

    public async updateService(data: UpdateEmployeeServiceParams): Promise<void> {
        await connect(`${process.env.DATABASE_CONNECTION}`);
        await EmployeesServicesList.updateEmployeeService(data);
    }

    public async getServices(data: FindEmployeesServicesParams): Promise<IEmployeeService[]> {
        await connect(`${process.env.DATABASE_CONNECTION}`);

        const employeeServices = await EmployeesServicesList.find(data);

        return employeeServices;
    }

    public async deleteServices(data: DeleteEmployeeServiceParams): Promise<void> {
        await connect(`${process.env.DATABASE_CONNECTION}`);
        await EmployeesServicesList.delete(data);
    }

    public async textObjectFilter(data: FindEmployeesParams): Promise<TextObjectFilterResult[]> {
        await connect(`${process.env.DATABASE_CONNECTION}`);

        const clients = await EmployeesList.textObjectFilter(data);

        return clients;
    }

    public async textObjectFilterServicesByCollaborator(
        data: FindEmployeesServicesParams
    ): Promise<TextObjectFilterResult[]> {
        await connect(`${process.env.DATABASE_CONNECTION}`);

        const services = await EmployeesServicesList.textObjectFilter(data);

        return services;
    }

    public async findByService(
        data: FindEmployeesByServiceParams
    ): Promise<TextObjectFilterResult[]> {
        await connect(`${process.env.DATABASE_CONNECTION}`);
        const services = await EmployeesServicesList.findByService(data);
        return services;
    }

    public async updateEmployee(data: UpdateEmployeeParams): Promise<void> {
        await connect(`${process.env.DATABASE_CONNECTION}`);
        await EmployeesList.update(data);
    }
}
