import { EStatuses } from "../enum/EStatuses";
import IPhoto from "../interfaces/IPhoto";
import IResponse from "../interfaces/IResponse";
import { instance } from "./instances";


class PhotosApi {
    public getAllPhotos = async(): Promise<IResponse<IPhoto[] | null>> => {
        try{
            const response = await instance.get('/photos')
            return response.data
        } catch(err: unknown){
            console.log(err);
            const error = err as Error
            const response: IResponse<null> = {
                status: EStatuses.FAILURE,
                result: null,
                message: error.message
            }
            return response
        }
    }

    public addPhoto = async(photoDto: FormData): Promise<IResponse<IPhoto | null>> => {
        try{
            const response = await instance.post('/photos', photoDto)
            return response.data
        } catch(err: unknown){
            console.log(err);
            const error = err as Error
            const response: IResponse<null> = {
                status: EStatuses.FAILURE,
                result: null,
                message: error.message
            }
            return response
        }
    }

    public getPhotosByUserId = async(id: string): Promise<IResponse<IPhoto[] | null>> => {
        try{
            const response = await instance.get(`/photos/${id}`)
            return response.data
        } catch(err: unknown){
            console.log(err);
            const error = err as Error
            const response: IResponse<null> = {
                status: EStatuses.FAILURE,
                result: null,
                message: error.message
            }
            return response
        }
    }

    public deletePhotoById = async(id: string): Promise<IResponse<IPhoto | null>> => {
        try{
            const response = await instance.delete(`/photos/${id}`)
            return response.data
        } catch(err: unknown){
            console.log(err);
            const error = err as Error
            const response: IResponse<null> = {
                status: EStatuses.FAILURE,
                result: null,
                message: error.message
            }
            return response
        }
    }
}

export const photosApi = new PhotosApi()