'use server'

export interface IPhotoData {
    albumId: number,
    title: string,
    url: string,
    thumbnailUrl: string
}

export async function createPhoto(data: IPhotoData) {
    try {
        const response = await fetch('https://jsonplaceholder.typicode.com/photos', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(data)
        });

        if (!response.ok)
            throw new Error(`Response status: ${response.status}`);

        return response.json()
    } 
    catch (error) {
        throw new Error(error.message);
    }
}

export async function deletePhoto(id: number) {
    try {
        const response = await fetch(`https://jsonplaceholder.typicode.com/photos?id=${id}`, {
            method: 'DELETE'
        });

        if (!response.ok)
            throw new Error(`Response status: ${response.status}`);
    } 
    catch (error) {
        throw new Error(error.message);
    }
}