import { AxiosProgressEvent, AxiosRequestConfig } from "axios";
import axiosInstance from "../http/axiosInstance";
import { IBank, ICompany, IDepartment, IDocument, IDocumentFolder, IOrganization, IOrganizationBank } from "../interfaces/documents";
import { BASE_URL } from "../constants";

const config: AxiosRequestConfig = {
    baseURL: BASE_URL.info,
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
}

export default class DocumentService {
    // Get Main
    static async getMain() {
        return await axiosInstance.get(`/info/`, config)
    }

    // Get Companies
    static async getCompanies(limit?: number, offset?: number) {
        return await axiosInstance.get(`/info/companies`, {
            params: {
                limit: limit || 10,
                offset: offset || 0
            },
            ...config
        })
    }

    // Create Company
    static async createCompany(data: ICompany) {
        return await axiosInstance.post(`/info/companies/`, data, config)
    }

    // Delete Company
    static async deleteCompany(company_id: number) {
        return await axiosInstance.delete(`/info/companies/${company_id}`, config)
    }

    // Update Company
    static async updateCompany(company_id: number) {
        return await axiosInstance.patch(`/info/companies/${company_id}`, config)
    }

    // Get Departments
    static async getDepartments(limit?: number, offset?: number) {
        return await axiosInstance.get(`/info/departments/`, {
            params: {
                limit: limit || 10,
                offset: offset || 0
            },
            ...config
        })
    }

    // Get Department
    static async getDepartment(department_id: number) {
        return await axiosInstance.get(`/info/departments/${department_id}`, config)
    }


    // Delete Department
    static async deleteDepartment(department_id: number) {
        return await axiosInstance.delete(`/info/departments/${department_id}`, config)
    }

    // Update Department
    static async updateDepartment(department_id: number, data: IDepartment) {
        return await axiosInstance.patch(`/info/departments/${department_id}`, data, config)
    }

    // Create Department
    static async createDepartment(data: IDepartment) {
        return await axiosInstance.post(`/info/department/`, data, config)
    }

    // Get Documents
    static async getDocuments(limit?: number, offset?: number) {
        return await axiosInstance.get(`/info/document_folder/`, {
            params: {
                limit: limit || 10,
                offset: offset || 0
            },
            ...config
        })
    }

    // Create Documentfolder
    static async createDocumentFolder(data: IDocumentFolder) {
        return await axiosInstance.post(`/info/document_folder/`, data, config)
    }

    // Get Document
    static async getDocument(folder_id: number) {
        return await axiosInstance.get(`/info/document_folder/${folder_id}`, config)
    }

    // Delete Document
    static async deleteDocument(folder_id: number) {
        return await axiosInstance.delete(`/info/document_folder/${folder_id}`, config)
    }

    // Update Document
    static async updateDocument(folder_id: number, data: IDocument) {
        return await axiosInstance.patch(`/info/document_folder/${folder_id}`, data, config)
    }

    // Get Docs
    static async getDocs(folder_id: number) {
        return await axiosInstance.get(`/info/documents/${folder_id}`, config)
    }

    // Upload Files
    static async uploadFiles(folder_id: number, files: FormData, setUploadProgress?: (value: number) => void) {
        return await axiosInstance.post(`/info/documents/upload/${folder_id}`, files, {
            onUploadProgress: (progressEvent: AxiosProgressEvent) => {
                const percentCompleted = progressEvent.progress
                setUploadProgress?.(percentCompleted || 0)
            },
            ...config
        })
    }

    // Download Doc
    static async downloadDoc(folder_id: number, doc_id: number) {
        return await axiosInstance.get(`/info/document/${folder_id}&${doc_id}`, {
            responseType: 'blob',
            ...config
        })
    }

    // Delete Doc
    static async deleteDoc(folder_id: number, doc_id: number) {
        return await axiosInstance.delete(`/info/document/`, {
            params: {
                folder_id: folder_id,
                doc_id: doc_id
            },
            ...config
        })
    }

    // Convert Phones
    static async convertPhones(data: FormData) {
        return await axiosInstance.post(`/info/other/phones/`, data, config)
    }

    // Get Budget
    static async getBudget() {
        return await axiosInstance.get(`/info/organization/budget/`, config)
    }

    // Add Bank
    static async addBank(data: IBank) {
        return await axiosInstance.post(`/info/organization/bank`, data, config)
    }

    // Update Bank
    static async updateBank(bank_id: string, bank_1c_id: string, data: IBank) {
        return await axiosInstance.patch(`/info/organization/bank`, data, {
            params: {
                bank_id: bank_id,
                bank_1c_id: bank_1c_id
            },
            ...config
        })
    }

    // Get Banks
    static async getBanks(bank_id?: string, search?: string, limit?: number, offset?: number) {
        return await axiosInstance.get(`/info/organization/banks`, {
            params: {
                bank_id: bank_id,
                search: search || null,
                limit: limit || 10,
                offset: offset || 0
            },
            ...config
        })
    }

    // Get Bank
    static async getBank(id_1c: string) {
        return await axiosInstance.get(`/info/organization/bank/${id_1c}`, config)
    }

    // Delete Bank
    static async deleteBank(bank_id: string, bank_1c_id: string) {
        return await axiosInstance.get(`/info/organization/bank/`, {
            params: {
                bank_id: bank_id,
                bank_1c_id: bank_1c_id
            },
            ...config
        })
    }

    // Add Org
    static async addOrganization(data: IOrganization) {
        return await axiosInstance.post(`/info/organization/org/`, data, config)
    }

    // Update Org
    static async updateOrganization(org_id: string, org_1c_id: string, data: IOrganization) {
        return await axiosInstance.patch(`/info/organization/org`, data, {
            params: {
                org_id: org_id,
                org_1c_id: org_1c_id
            },
            ...config
        })
    }

    // Delete Org
    static async deleteOrganization(org_id: string, org_1c_id: string) {
        return await axiosInstance.delete(`/info/organization/org`, {
            params: {
                org_id: org_id,
                org_1c_id: org_1c_id
            },
            ...config
        })
    }

    // Get Orgs
    static async getOrganizations(org_id?: string, search?: string, limit?: number, offset?: number) {
        return await axiosInstance.get(`/info/organization/orgs`, {
            params: {
                org_id: org_id,
                search: search || null,
                limit: limit || 10,
                offset: offset || 0
            },
            ...config
        })
    }

    // Get Org
    static async getOrganization(id_1c: string) {
        return await axiosInstance.get(`/info/organization/org/${id_1c}`, config)
    }

    // Add Orgbank
    static async addOrganizationBank(data: IOrganizationBank) {
        return await axiosInstance.post(`/info/organization/org_bank`, data, config)
    }
}