import type {Space} from "@/models/space";
import {User} from "@/models/user";
import {SettingType} from "@/models/enums";
import api from "@/network/axiosSetup";

// async function getSpaces(): Promise<Space[]> {
//     const response = await api.get("/api/spaces");
//     if (!response.status.toString().startsWith('2')) throw new Error("Failed to fetch spaces");
//     return response.data;
// }


async function fetchSpaces(): Promise<Space[]> {
    try {
        let response = await api.get('/spaces');
        return response.data;
    } catch (error) {
        console.error(error);
        throw error;
    }
}

async function fetchSpace(spaceId: number): Promise<Space> {
    try {
        let response = await api.get(`/spaces/${spaceId}`);
        return response.data;
    } catch (error) {
        console.error(error);
        throw error;
    }
}

async function getSettings(): Promise<Record<string, string>[]> {
    return [
        {
            key: 'period-start',
            value: '15',
        },
        {
            key: 'sub-period-start',
            value: '25',
        },
        {
            key: 'notifications-enabled',
            value: '1',
        },
        {
            key: 'notifications-days',
            value: 'Mon,Tue,Thu,Fri',
        },
        {
            key: 'notifications-time',
            value: '19:30',
        },
    ]
}

async function getSetting(key: string): Promise<Record<string, string>> {
    let settings = [
        {
            key: SettingType.PERIOD_START,
            value: '10',
        },
        {
            key: SettingType.SUB_PERIOD_START,
            value: '25',
        },
        {
            key: SettingType.NOTIFICATIONS_ENABLED,
            value: '1',
        },
        {
            key: SettingType.NOTIFICATIONS_DAYS,
            value: 'Mon,Tue,Thu',
        },
        {
            key: SettingType.NOTIFICATIONS_TIME,
            value: '19:30',
        },
    ]
    return settings.find((i) => i.key == key) ?? {}
}

async function setSetting(key: SettingType, value: any): Promise<void> {
    console.debug(`Setting ${key} is ${JSON.stringify(value)}`);
}

async function createSpace(spaceName: string): Promise<number> {
    try {
        let response = await api.post(`/spaces`, {name: spaceName});
        return response.data;
    } catch (error) {
        console.error(error);
        throw error;
    }
}

export const spaceService = {
    fetchSpaces, fetchSpace, getSetting, getSettings, setSetting, createSpace
};