use super::split_args;

#[derive(Debug, Clone)]
pub struct CharacterData {
    pub name: Option<String>,
    pub tags: Vec<String>,
}

impl CharacterData {
    pub fn parse_args(raw: &str) -> Self {
        let p = split_args(raw);
        CharacterData {
            name: p.attrs.get("name").cloned(),
            tags: p.tags,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_args_empty() {
        let d = CharacterData::parse_args("");
        assert!(d.name.is_none());
        assert!(d.tags.is_empty());
    }

    #[test]
    fn test_parse_args_name_only() {
        let d = CharacterData::parse_args("name: Mahfuz Vai");
        assert_eq!(d.name.as_deref(), Some("Mahfuz Vai"));
        assert!(d.tags.is_empty());
    }

    #[test]
    fn test_parse_args_name_and_single_tag() {
        let d = CharacterData::parse_args("name: Mahfuz Vai, mahfuz-vai");
        assert_eq!(d.name.as_deref(), Some("Mahfuz Vai"));
        assert_eq!(d.tags, vec!["mahfuz-vai"]);
    }

    #[test]
    fn test_parse_args_name_and_multiple_tags() {
        let d = CharacterData::parse_args("name: Mahfuz Vai, mahfuz-vai, favor");
        assert_eq!(d.name.as_deref(), Some("Mahfuz Vai"));
        assert_eq!(d.tags, vec!["mahfuz-vai", "favor"]);
    }

    #[test]
    fn test_parse_args_tags_only_no_name() {
        let d = CharacterData::parse_args("mahfuz-vai, favor");
        assert!(d.name.is_none());
        assert_eq!(d.tags, vec!["mahfuz-vai", "favor"]);
    }

    #[test]
    fn test_parse_args_name_with_spaces_in_value() {
        let d = CharacterData::parse_args("name: Dr. Alice Smith");
        assert_eq!(d.name.as_deref(), Some("Dr. Alice Smith"));
    }

    #[test]
    fn test_parse_args_name_trimmed() {
        let d = CharacterData::parse_args("name:  Mahfuz Vai ");
        assert_eq!(d.name.as_deref(), Some("Mahfuz Vai"));
    }

    #[test]
    fn test_parse_args_name_with_hyphenated_tag() {
        let d = CharacterData::parse_args("name: Alex, friend-of-work, trusted");
        assert_eq!(d.name.as_deref(), Some("Alex"));
        assert_eq!(d.tags, vec!["friend-of-work", "trusted"]);
    }

    #[test]
    fn test_parse_args_no_name_single_tag() {
        let d = CharacterData::parse_args("colleague");
        assert!(d.name.is_none());
        assert_eq!(d.tags, vec!["colleague"]);
    }
}
