import useSWR, { SWRConfiguration } from "swr";
import RoleService from "../services/RoleService";
import UserService from "../services/UserService";
import { fetcher } from "../http/axiosInstance";
import { fileTypeFromBlob } from "file-type/core";
import { BASE_URL } from "../constants";

const swrOptions: SWRConfiguration = {
    revalidateOnFocus: false,
}

export function useRoles() {
    const { data, error, isLoading } = useSWR(`/auth/roles`, RoleService.getRoles, swrOptions)

    return {
        roles: data?.data,
        isLoading,
        isError: error
    }
}

export function useUsers() {
    const { data, error, isLoading } = useSWR(`/auth/user`, UserService.getUsers, swrOptions)

    return {
        users: data?.data,
        isLoading,
        isError: error
    }
}

export function useCompanies(limit?: number, offset?: number) {
    const { data, error, isLoading } = useSWR(`/info/companies?limit=${limit || 10}&offset=${offset || 0}`, fetcher, swrOptions)

    return {
        companies: data,
        isLoading,
        isError: error
    }
}

export function useFolders(limit?: number, offset?: number) {
    const { data, error, isLoading } = useSWR(
        `/info/document_folder?limit=${limit || 10}&offset=${offset || 0}`,
        fetcher,
        swrOptions
    )

    return {
        folders: data,
        isLoading,
        isError: error
    }
}

export function useDocuments(folder_id?: number) {
    const { data, error, isLoading } = useSWR(
        folder_id ? `/info/documents/${folder_id}` : null,
        fetcher,
        swrOptions
    )

    return {
        documents: data,
        isLoading,
        isError: error
    }
}

export function useDownload(folder_id?: number | null, id?: number | null) {
    const { data, error, isLoading } = useSWR(
        folder_id && id ? `/info/document/${folder_id}&${id}` : null,
        folder_id && id ? (url) => fetcher(url, BASE_URL.info, "blob") : null,
        swrOptions
    )

    return {
        file: data,
        isLoading,
        isError: error
    }
}

export function useFileType(fileName?: string | null, file?: Blob | null) {
    const { data, error, isLoading } = useSWR(
        fileName && file ? `/filetype/${fileName}` : null,
        file ? () => fileTypeFromBlob(file) : null,
        swrOptions
    )

    return {
        fileType: data?.mime,
        isLoading,
        isError: error
    }
}

export function useReport(city_id?: number | null) {
    const { data, error, isLoading } = useSWR(
        city_id ? `/info/reports/${city_id}?to_export=false` : null,
        (url) => fetcher(url, BASE_URL.info),
        swrOptions
    )

    return {
        report: data ? JSON.parse(data) : [],
        isLoading,
        isError: error
    }
}

export function useReportExport(city_id?: number | null, to_export?: boolean) {
    const { data, error, isLoading } = useSWR(
        city_id && to_export ? `/info/reports/${city_id}?to_export=${to_export}` : null,
        (url) => fetcher(url, BASE_URL.info, 'blob'),
        swrOptions
    )

    return {
        reportExported: data ? data : null,
        isLoading,
        isError: error
    }
}


// API general (fuel)

export function useAddress(limit?: number, page?: number) {
    const { data, error, isLoading } = useSWR(
        `/general/address?limit=${limit || 10}&page=${page || 1}`,
        (url) => fetcher(url, BASE_URL.fuel),
        swrOptions
    )

    return {
        address: data,
        isLoading,
        isError: error
    }
}

export function useRegions(limit?: number, page?: number, search?: string | null) {
    const { data, error, isLoading } = useSWR(
        `/general/regions?limit=${limit || 10}&page=${page || 1}${search ? `&search=${search}` : ''}`,
        (url) => fetcher(url, BASE_URL.fuel),
        swrOptions
    )

    return {
        regions: data,
        isLoading,
        isError: error
    }
}

export function useCities(limit?: number, page?: number, search?: string | null) {
    const { data, error, isLoading } = useSWR(
        `/general/cities?limit=${limit || 10}&page=${page || 1}${search ? `&search=${search}` : ''}`,
        (url) => fetcher(url, BASE_URL.fuel),
        swrOptions
    )

    return {
        cities: data,
        isLoading,
        isError: error
    }
}

export function useBoilers(limit?: number, page?: number, search?: string) {
    const { data, error, isLoading } = useSWR(
        `/general/boilers?limit=${limit || 10}&page=${page || 1}${search ? `&search=${search}` : ''}`,
        (url) => fetcher(url, BASE_URL.fuel),
        swrOptions
    )

    return {
        boilers: data,
        isLoading,
        isError: error
    }
}

// Servers

export function useServers(region_id?: number | null, offset?: number, limit?: number) {
    const { data, error, isLoading } = useSWR(
        region_id ? `/api/servers?region_id=${region_id}&offset=${offset || 0}&limit=${limit || 10}` : `/api/servers?offset=${offset || 0}&limit=${limit || 10}`,
        (url: string) => fetcher(url, BASE_URL.servers),
        swrOptions
    )

    return {
        servers: data,
        isLoading,
        isError: error
    }
}

export function useServersInfo(region_id?: number | null, offset?: number, limit?: number) {
    const { data, error, isLoading } = useSWR(
        region_id ? `/api/servers_info?region_id=${region_id}&offset=${offset || 0}&limit=${limit || 10}` : `/api/servers_info?offset=${offset || 0}&limit=${limit || 10}`,
        (url: string) => fetcher(url, BASE_URL.servers),
        swrOptions
    )

    return {
        serversInfo: data,
        isLoading,
        isError: error
    }
}

export function useServer(server_id?: number) {
    const { data, error, isLoading } = useSWR(
        server_id ? `/api/server/${server_id}` : null,
        (url) => fetcher(url, BASE_URL.servers),
        swrOptions
    )

    return {
        server: data,
        isLoading,
        isError: error
    }
}

export function useServerIps(server_id?: number | null, offset?: number, limit?: number) {
    const { data, error, isLoading } = useSWR(
        server_id ? `/api/server_ips?server_id=${server_id}&offset=${offset || 0}&limit=${limit || 10}` : `/api/server_ips?offset=${offset || 0}&limit=${limit || 10}`,
        (url: string) => fetcher(url, BASE_URL.servers),
        swrOptions
    )

    return {
        serverIps: data,
        isLoading,
        isError: error
    }
}

// Hardware

export function useHardwares(server_id?: number, offset?: number, limit?: number) {
    const { data, error, isLoading } = useSWR(
        server_id ? `/api/hardwares?server_id=${server_id}&offset=${offset || 0}&limit=${limit || 10}` : `/api/hardwares?offset=${offset || 0}&limit=${limit || 10}`,
        (url: string) => fetcher(url, BASE_URL.servers),
        swrOptions
    )

    return {
        hardwares: data,
        isLoading,
        isError: error
    }
}


export function useHardware(hardware_id?: number) {
    const { data, error, isLoading } = useSWR(
        hardware_id ? `/api/hardware/${hardware_id}` : null,
        (url) => fetcher(url, BASE_URL.servers),
        swrOptions
    )

    return {
        hardware: data,
        isLoading,
        isError: error
    }
}

// Storage

export function useStorages(hardware_id?: number, offset?: number, limit?: number) {
    const { data, error, isLoading } = useSWR(
        hardware_id ? `/api/storages?hardware_id=${hardware_id}&offset=${offset || 0}&limit=${limit || 10}` : `/api/storages?offset=${offset || 0}&limit=${limit || 10}`,
        (url: string) => fetcher(url, BASE_URL.servers),
        swrOptions
    )

    return {
        storages: data,
        isLoading,
        isError: error
    }
}

export function useStorage(storage_id?: number) {
    const { data, error, isLoading } = useSWR(
        storage_id ? `/api/storage/${storage_id}` : null,
        (url) => fetcher(url, BASE_URL.servers),
        swrOptions
    )

    return {
        storage: data,
        isLoading,
        isError: error
    }
}