import { connect } from "mongoose";
import CompaniestList from "../../Models/Companies/Companies";
import {
    CreateCompanyParams,
    UpdateCompanyParams,
    SetCompanyFileParams,
    DeleteCompanyFileParams,
    ICompany,
    PaginateCompaniesParams,
    PaginateCompaniesResults,
    MyOranizationsView,
    MyOranizationsViewParams,
    OranizationsViewByIdParams,
    DeleteCompanyParams,
    SetOrganizationPublishedStatusParams,
    SetNotificationAutoParams,
    FixCompanyPostParams,
    MoveCompanyFixedPostParams,
} from "../../Models/Companies/Companies.Interface";
import { TextObjectFilterResult } from "../../Models/TextObjectFilter.model";

export class CompaniesService {
    public async toggleFixedPost(data: FixCompanyPostParams): Promise<void> {
        await connect(`${process.env.DATABASE_CONNECTION}`);
        await CompaniestList.toggleFixedPost(data);
    }

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

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

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

    public async deleteCompany(data: DeleteCompanyParams): Promise<void> {
        await connect(`${process.env.DATABASE_CONNECTION}`);
        await CompaniestList.deleteCompany(data);
    }
    public async createCompany(data: CreateCompanyParams): Promise<ICompany> {
        await connect(`${process.env.DATABASE_CONNECTION}`);

        const newCompany = await CompaniestList.createCompany(data);

        return newCompany;
    }

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

    public async paginateCompanies(
        data: PaginateCompaniesParams
    ): Promise<PaginateCompaniesResults> {
        await connect(`${process.env.DATABASE_CONNECTION}`);

        const companies = await CompaniestList.paginate(data);

        return companies;
    }

    public async getById(data: OranizationsViewByIdParams): Promise<MyOranizationsView> {
        await connect(`${process.env.DATABASE_CONNECTION}`);
        const company = await CompaniestList.getById(data);
        return company;
    }

    public async getByUserId(data: MyOranizationsViewParams): Promise<MyOranizationsView[]> {
        await connect(`${process.env.DATABASE_CONNECTION}`);

        const companies = await CompaniestList.getByUserId(data);

        return companies;
    }

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

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

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

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

    public async filterAll(data: PaginateCompaniesParams): Promise<TextObjectFilterResult[]> {
        await connect(`${process.env.DATABASE_CONNECTION}`);
        const companies = await CompaniestList.filterAll(data);
        return companies;
    }

    public async checkNameExists(name: string): Promise<boolean> {
        await connect(`${process.env.DATABASE_CONNECTION}`);
        const isValid = await CompaniestList.isValidName({ name });
        return isValid;
    }
}
