import axios from "axios";
import { Network } from "@/app/types";

const useNetworks = () => {
   
    const addNetwork = (network: Network) => {
        if (!network.name) {
            return 'Network name is required';
        }
        axios.post('/api/sites/networks/add', network)
        .then(() => {
            return;
        })
        .catch(err => {
            return err.response?.data?.error || 'An error occurred';
        });
    };

    const editNetwork = (network: Network) => {
        if (!network.id) {
            return 'Network ID is required';
        }
        if(network.name.length < 1) {
            return 'Network name must be at least 1 character long';
        }
        axios.post('/api/sites/networks/edit', network)
        .then(() => {
            return;
        })
        .catch(err => {
            return err.response?.data?.error || 'An error occurred';
        });
    };

    const deleteNetwork = (networkId: string) => {
        if (!networkId) {
            return 'Network ID is required';
        }
        axios.delete('/api/sites/networks/delete', {
            params: { networkId }
        })
        .then(() => {
            return;
        })
        .catch(err => {
            return err.response?.data?.error || 'An error occurred';
        });
    };

    return {
        addNetwork,
        editNetwork,
        deleteNetwork,
    };
};

export default useNetworks;