// if there's musicbrainz why no musicorganz

use crate::disc_id;
use serde::Deserialize;

#[derive(Deserialize, Debug)]
pub struct DiscResponse {
    pub releases: Vec<Release>,
}
#[derive(Deserialize, Debug)]
pub struct Release {
    pub title: String,
    pub media: Vec<Medium>,
    pub id: String,
}
#[derive(Deserialize, Debug)]
pub struct Medium {
    pub tracks: Vec<Track>,
}
#[derive(Deserialize, Debug)]
pub struct Track {
    pub title: String,
    pub position: u32,
    #[serde(rename = "artist-credit")]
    pub artist_credit: Vec<ArtistCredit>,
}
#[derive(Deserialize, Debug)]
pub struct ArtistCredit {
    pub name: String,
}

pub fn get_disc_info(base_url: &str) -> DiscResponse {
    let disc_id = disc_id::get_disc_id();
    let url = format!("{base_url}/ws/2/discid/{disc_id}");

    let body = ureq::get(&url)
        .query("inc", "recordings+artist-credits")
        .query("fmt", "json")
        .header(
            "User-Agent",
            "Timbre/0.1 ( https://gitea.owendeed.com/Toasterkitten/timbre )",
        )
        .call()
        .expect("i couldn't connect to the musicbrainz server\n¯\\_(ツ)_/¯\ncheck your internet or something?\n")
        .body_mut()
        .read_to_string()
        .expect("musicbrainz isn't happy with you. ¯\\_(ツ)_/¯\n");
    let parsed: DiscResponse = serde_json::from_str(&body).expect(
        "musicbrainz said something i don't understand\n¯\\_(ツ)_/¯\nmaybe ur disc is unique\n",
    );
    parsed
}
