"""
Content-type system configuration for Mnemosyne library types.

Each library type has a default configuration that governs chunking,
embedding, re-ranking, LLM context injection, and vision analysis prompts.
"""

# Default configurations per library type.
# These are loaded into Library nodes via the load_library_types management command.
LIBRARY_TYPE_DEFAULTS = {
    "fiction": {
        "chunking_config": {
            "strategy": "chapter_aware",
            "chunk_size": 1024,
            "chunk_overlap": 128,
            "respect_boundaries": ["chapter", "scene", "paragraph"],
        },
        "embedding_instruction": (
            "Represent this passage from a work of fiction for retrieval. "
            "Focus on narrative elements: characters, plot events, themes, "
            "setting, and emotional tone."
        ),
        "reranker_instruction": (
            "Re-rank passages from fiction based on narrative relevance to the query. "
            "Prioritize character actions, dialogue, plot developments, and thematic elements."
        ),
        "llm_context_prompt": (
            "The following excerpts are from fiction (novels, short stories, etc.). "
            "Treat this as creative/narrative content. Respect the literary context — "
            "characters, settings, and events are fictional. Cite specific passages "
            "when answering."
        ),
        "vision_prompt": (
            "Analyze this image from a work of fiction. Identify:\n"
            "1) Image type (illustration, cover, map, portrait, scene).\n"
            "2) What it depicts — characters, scenes, settings.\n"
            "3) Any visible text, titles, or captions.\n"
            "4) The artistic style and mood."
        ),
    },
    "nonfiction": {
        "chunking_config": {
            "strategy": "section_aware",
            "chunk_size": 768,
            "chunk_overlap": 96,
            "respect_boundaries": ["chapter", "section", "paragraph"],
        },
        "embedding_instruction": (
            "Represent this passage from a non-fiction work for retrieval. "
            "Focus on factual claims, historical events, people, places, "
            "arguments, and supporting evidence."
        ),
        "reranker_instruction": (
            "Re-rank passages from non-fiction based on factual relevance. "
            "Prioritize historical events, arguments, evidence, and key figures."
        ),
        "llm_context_prompt": (
            "The following excerpts are from non-fiction (history, biography, essays, "
            "science writing, philosophy, etc.). Treat claims as the author's perspective "
            "— note when interpretations may vary. Cite specific passages and distinguish "
            "fact from analysis."
        ),
        "vision_prompt": (
            "Analyze this image from a non-fiction work. Identify:\n"
            "1) Image type (photograph, map, chart, illustration, portrait, document).\n"
            "2) What it depicts — people, places, events, data.\n"
            "3) Any visible text, labels, captions, or dates.\n"
            "4) Historical or factual context."
        ),
    },
    "technical": {
        "chunking_config": {
            "strategy": "section_aware",
            "chunk_size": 512,
            "chunk_overlap": 64,
            "respect_boundaries": ["section", "subsection", "code_block", "list"],
        },
        "embedding_instruction": (
            "Represent this passage from technical documentation for retrieval. "
            "Focus on procedures, configurations, API references, code examples, "
            "and technical concepts."
        ),
        "reranker_instruction": (
            "Re-rank passages from technical documentation based on procedural relevance. "
            "Prioritize step-by-step instructions, code examples, and specific configurations."
        ),
        "llm_context_prompt": (
            "The following excerpts are from technical documentation (manuals, guides, "
            "reference material). Provide precise, actionable answers. Include code "
            "examples and exact configurations when available. Cite source sections."
        ),
        "vision_prompt": (
            "Analyze this image from technical documentation. Identify:\n"
            "1) Image type (diagram, chart, table, screenshot, schematic, flowchart).\n"
            "2) What it depicts — components, processes, architecture, data.\n"
            "3) Any visible text, labels, values, or annotations.\n"
            "4) The technical concept or procedure being illustrated."
        ),
    },
    "music": {
        "chunking_config": {
            "strategy": "song_level",
            "chunk_size": 512,
            "chunk_overlap": 32,
            "respect_boundaries": ["song", "verse", "chorus"],
        },
        "embedding_instruction": (
            "Represent this music content (lyrics, liner notes, metadata) for retrieval. "
            "Focus on artist, album, genre, lyrical themes, and musical elements."
        ),
        "reranker_instruction": (
            "Re-rank music content based on relevance to the query. "
            "Consider artist, genre, lyrical themes, and musical characteristics."
        ),
        "llm_context_prompt": (
            "The following excerpts are song lyrics and music metadata. "
            "Consider the artistic and cultural context. Reference specific "
            "songs, albums, and artists when answering."
        ),
        "vision_prompt": (
            "Analyze this image from a music collection. Identify:\n"
            "1) Image type (album cover, liner notes, band photo, concert photo, logo).\n"
            "2) What it depicts — artwork style, imagery, people.\n"
            "3) Any visible text, band names, album titles, or track listings.\n"
            "4) The era, aesthetic, and genre associations."
        ),
    },
    "film": {
        "chunking_config": {
            "strategy": "scene_level",
            "chunk_size": 768,
            "chunk_overlap": 64,
            "respect_boundaries": ["scene", "act", "sequence"],
        },
        "embedding_instruction": (
            "Represent this film content (scripts, synopses, reviews) for retrieval. "
            "Focus on scenes, characters, visual elements, dialogue, and narrative structure."
        ),
        "reranker_instruction": (
            "Re-rank film content based on cinematic relevance. "
            "Prioritize scene descriptions, character interactions, and visual elements."
        ),
        "llm_context_prompt": (
            "The following excerpts are from film-related content (scripts, synopses, "
            "reviews). Consider the cinematic context — visual storytelling, "
            "direction, and performance. Cite specific scenes and films."
        ),
        "vision_prompt": (
            "Analyze this image from a film or film-related content. Identify:\n"
            "1) Image type (movie still, poster, storyboard, behind-the-scenes, screenshot).\n"
            "2) What it depicts — scene, characters, setting, action.\n"
            "3) Any visible text, titles, or credits.\n"
            "4) Cinematographic elements — composition, lighting, mood."
        ),
    },
    "art": {
        "chunking_config": {
            "strategy": "description_level",
            "chunk_size": 512,
            "chunk_overlap": 32,
            "respect_boundaries": ["artwork", "description", "analysis"],
        },
        "embedding_instruction": (
            "Represent this art content (descriptions, catalogs, analysis) for retrieval. "
            "Focus on visual elements, style, medium, artist, period, and artistic movements."
        ),
        "reranker_instruction": (
            "Re-rank art content based on visual and stylistic relevance. "
            "Prioritize descriptions of artwork, technique, composition, and artistic context."
        ),
        "llm_context_prompt": (
            "The following excerpts describe artworks and artistic content. "
            "Consider visual elements, artistic technique, historical context, "
            "and the artist's intent. Reference specific works and movements."
        ),
        "vision_prompt": (
            "Describe this artwork in detail. Identify:\n"
            "1) Image type (painting, sculpture, photograph, print, drawing, mixed media).\n"
            "2) Subject matter — what the artwork depicts.\n"
            "3) Style, medium, and technique.\n"
            "4) Composition, color palette, mood, and artistic period or movement."
        ),
    },
    "journal": {
        "chunking_config": {
            "strategy": "entry_level",
            "chunk_size": 512,
            "chunk_overlap": 32,
            "respect_boundaries": ["entry", "date", "paragraph"],
        },
        "embedding_instruction": (
            "Represent this personal journal entry for retrieval. "
            "Focus on temporal context, personal reflections, mentioned people "
            "and places, and emotional content."
        ),
        "reranker_instruction": (
            "Re-rank journal entries based on temporal and thematic relevance. "
            "Prioritize entries matching the time period, people, places, or topics in the query."
        ),
        "llm_context_prompt": (
            "The following excerpts are from personal journal entries. "
            "This is private, reflective content. Respect the personal nature — "
            "answer with sensitivity. Note dates and temporal context when relevant."
        ),
        "vision_prompt": (
            "Analyze this image from a personal journal. Identify:\n"
            "1) Image type (photograph, sketch, document, receipt, ticket, screenshot).\n"
            "2) What it depicts — people, places, events, objects.\n"
            "3) Any visible text, dates, or handwriting.\n"
            "4) Context clues about when and where this was taken or created."
        ),
    },
    "business": {
        "chunking_config": {
            "strategy": "section_aware",
            "chunk_size": 640,
            "chunk_overlap": 96,
            "respect_boundaries": ["section", "subsection", "list", "table"],
        },
        "embedding_instruction": (
            "Represent this passage from a business document for retrieval. "
            "Focus on value propositions, positioning, pricing, scope of work, "
            "client outcomes, and commercial commitments."
        ),
        "reranker_instruction": (
            "Re-rank passages from business documents based on commercial relevance. "
            "Prioritize value framing, deliverables, client outcomes, and specific "
            "pricing or scope language."
        ),
        "llm_context_prompt": (
            "The following excerpts are from business documents (proposals, marketing, "
            "sales, strategy). Interpret in commercial context. Distinguish positioning "
            "claims from committed deliverables. Preserve numbers, scope language, and "
            "client names exactly as written."
        ),
        "vision_prompt": (
            "Analyze this image from a business document. Identify:\n"
            "1) Image type (logo, chart, diagram, screenshot, photograph, table).\n"
            "2) What it depicts — brand marks, data, organizational structure, products.\n"
            "3) Any visible text — company names, figures, captions, headings.\n"
            "4) The commercial purpose — positioning, pricing, capability demonstration."
        ),
    },
    "email": {
        "chunking_config": {
            "strategy": "entry_level",
            "chunk_size": 512,
            "chunk_overlap": 32,
            "respect_boundaries": ["message", "quote", "paragraph"],
        },
        "embedding_instruction": (
            "Represent this email message for retrieval. "
            "Focus on the sender, recipients, subject, dates, requests and "
            "commitments made, and the people, organizations, and events discussed."
        ),
        "reranker_instruction": (
            "Re-rank email messages based on relevance to the query. "
            "Prioritize messages matching the correspondents, subject matter, "
            "time period, and any specific commitments or requests mentioned."
        ),
        "llm_context_prompt": (
            "The following excerpts are from personal email correspondence. "
            "This is private content — answer with discretion. Attribute "
            "statements to their senders, note dates, and distinguish what was "
            "asked from what was agreed. Quoted text below a reply is earlier "
            "context, not the sender's own words."
        ),
        "vision_prompt": (
            "Analyze this image from an email message. Identify:\n"
            "1) Image type (photograph, screenshot, scanned document, chart, signature graphic).\n"
            "2) What it depicts — people, places, documents, data.\n"
            "3) Any visible text, dates, or figures.\n"
            "4) Its role in the message — attachment content, inline illustration, or boilerplate."
        ),
    },
    "finance": {
        "chunking_config": {
            "strategy": "section_aware",
            "chunk_size": 512,
            "chunk_overlap": 64,
            "respect_boundaries": ["section", "table", "row", "paragraph"],
        },
        "embedding_instruction": (
            "Represent this passage from a financial document for retrieval. "
            "Focus on accounts, instruments, dates, amounts, balances, and "
            "analytical commentary."
        ),
        "reranker_instruction": (
            "Re-rank passages from financial documents based on relevance to the query. "
            "Prioritize the matching account, instrument, time period, and figures."
        ),
        "llm_context_prompt": (
            "The following excerpts are from financial documents (statements, tax "
            "documents, market commentary, planning). Distinguish factual figures "
            "(statements, transactions, balances) from opinion (forecasts, commentary). "
            "Quote numbers, dates, and account identifiers exactly as they appear. "
            "Do not infer, round, or fabricate financial figures. If a figure is not "
            "present in the excerpts, say so explicitly."
        ),
        "vision_prompt": (
            "Analyze this image from a financial document. Identify:\n"
            "1) Image type (chart, table, statement scan, dashboard screenshot, receipt).\n"
            "2) What it depicts — account, instrument, time period, data series.\n"
            "3) Any visible text — figures, dates, account identifiers, labels.\n"
            "4) Whether the data is factual (statement) or analytical (forecast/commentary)."
        ),
    },
}


def get_library_type_config(library_type):
    """
    Get the default configuration for a library type.

    Args:
        library_type: One of 'fiction', 'nonfiction', 'technical', 'music',
                      'film', 'art', 'journal', 'email', 'business', 'finance'

    Returns:
        dict with keys: chunking_config, embedding_instruction,
        reranker_instruction, llm_context_prompt, vision_prompt

    Raises:
        ValueError: If library_type is not recognized
    """
    if library_type not in LIBRARY_TYPE_DEFAULTS:
        raise ValueError(
            f"Unknown library type '{library_type}'. "
            f"Valid types: {', '.join(LIBRARY_TYPE_DEFAULTS.keys())}"
        )
    return LIBRARY_TYPE_DEFAULTS[library_type]
