import { z } from "zod"

const SongSchema = z.object({
    uuid: z.string(),
    title: z.string(),
    artist: z.string(),
    tags: z.array(z.string()),
    image_id: z.string(),
    youtube_id: z.string(),
})
export type Song = z.infer<typeof SongSchema>

export const parseSong = async function (song: any): Promise<Song> {
    let resp = await SongSchema.parseAsync(song)
    return resp
}

export const createEmptySong = function (): Song {
    return {
        uuid: "-1",
        title: "",
        artist: "",
        tags: [""],
        image_id: "",
        youtube_id: "",
    }
}

const SuggestionSchema = SongSchema.extend({
    upvote: z.number(),
})
export type Suggestion = z.infer<typeof SuggestionSchema>

export const parseSuggestion = async function (sugg: any): Promise<Suggestion> {
    let resp = await SuggestionSchema.parseAsync(sugg)
    return resp
}

const RoomSchema = z.object({
    id: z.number(),
    name: z.string(),
    private: z.boolean(),
    coords: z.object({ latitude: z.number(), longitude: z.number() }),
    range: z.number().int(),
    distance: z.number()
})
export type Room = z.infer<typeof RoomSchema>

export const parseRoom = async function (room: any): Promise<Room> {
    let resp = await RoomSchema.parseAsync(room)
    return resp
}

export type FetchError = {
    code: number
    message: string
}
