/*
    This file is part of jellything (https://codeberg.org/metamuffin/jellything)
    which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
    Copyright (C) 2026 metamuffin <metamuffin.org>
*/

use crate::{
    plugins::{ImportPlugin, PluginContext, PluginInfo},
    source_rank::ObjectImportSourceExt,
};
use anyhow::Result;
use jellycommon::*;
use jellydb::RowNum;
use jellyremuxer::matroska::Segment;
use std::{collections::HashMap, path::Path};

pub struct Tags;
impl ImportPlugin for Tags {
    fn info(&self) -> PluginInfo {
        PluginInfo {
            name: "tags",
            tag: MSOURCE_TAGS,
            handle_media: true,
            ..Default::default()
        }
    }
    fn media(&self, ct: &PluginContext, node: RowNum, _path: &Path, seg: &Segment) -> Result<()> {
        let tags = seg
            .tags
            .first()
            .map(|tags| {
                tags.tags
                    .iter()
                    .flat_map(|t| t.simple_tags.clone())
                    .map(|st| (st.name, st.string.unwrap_or_default()))
                    .collect::<HashMap<_, _>>()
            })
            .unwrap_or_default();

        ct.ic.update_node(node, |mut node| {
            if let Some(title) = &seg.info.title {
                node = node.as_object().insert_s(ct.is, NO_TITLE, title);
            }
            for (key, value) in &tags {
                match key.as_str() {
                    "DESCRIPTION" | "SYNOPSIS" => {
                        node = node.as_object().insert_s(ct.is, NO_DESCRIPTION, &value)
                    }
                    "COMMENT" => node = node.as_object().insert(NO_TAGLINE, &value),
                    "CONTENT_TYPE" => {
                        node = node.as_object().insert_s(
                            ct.is,
                            NO_KIND,
                            match value.to_lowercase().trim() {
                                "movie" | "documentary" | "film" => KIND_MOVIE,
                                "music" | "recording" => KIND_MUSIC,
                                _ => continue,
                            },
                        );
                    }
                    _ => {
                        let idty = match key.as_str() {
                            "MUSICBRAINZ_TRACKID" => IDENT_MUSICBRAINZ_RECORDING,
                            "MUSICBRAINZ_ARTISTID" => IDENT_MUSICBRAINZ_ARTIST,
                            "MUSICBRAINZ_ALBUMID" => IDENT_MUSICBRAINZ_RELEASE,
                            "MUSICBRAINZ_ALBUMARTISTID" => continue,
                            "MUSICBRAINZ_RELEASEGROUPID" => IDENT_MUSICBRAINZ_RELEASE_GROUP,
                            "ISRC" => IDENT_ISRC,
                            "BARCODE" => IDENT_BARCODE,
                            _ => continue,
                        };
                        node = node.as_object().update(NO_IDENTIFIERS, |idents| {
                            idents.insert_s(ct.is, idty, &value)
                        });
                    }
                }
            }
            node
        })
    }
}
