# BibTeX/Citation Export Schema
# Based on REF-061 OAIS (Dissemination Information Package)
# Issues: #109, #121

$schema: "https://json-schema.org/draft/2020-12/schema"
$id: "https://aiwg.io/schemas/bibtex-export/v1"
title: "Citation Export Schema"
description: |
  Schema for exporting research corpus as BibTeX, CSL JSON, or RIS formats.
  Implements OAIS DIP (Dissemination Information Package) generation.

type: object
required:
  - export_id
  - format
  - entries

properties:
  export_id:
    type: string
    format: uuid

  generated_at:
    type: string
    format: date-time

  format:
    type: string
    enum:
      - bibtex
      - csl_json
      - ris
      - endnote
    description: "Export format"

  filters:
    type: object
    properties:
      quality:
        type: array
        items:
          type: string
          enum: [high, moderate, low, very_low]
        description: "Filter by GRADE quality"
      relevance:
        type: array
        items:
          type: string
          enum: [critical, high, medium, low]
        description: "Filter by AIWG relevance"
      categories:
        type: array
        items:
          type: string
        description: "Filter by category"
      year_range:
        type: object
        properties:
          min:
            type: integer
          max:
            type: integer
      ref_ids:
        type: array
        items:
          type: string
        description: "Specific REF-XXX to include"
    description: "Export filters"

  entries:
    type: array
    items:
      $ref: "#/$defs/Entry"
    description: "Exported entries"

  statistics:
    type: object
    properties:
      total_in_corpus:
        type: integer
      filtered_count:
        type: integer
      exported_count:
        type: integer

$defs:
  Entry:
    type: object
    required:
      - ref_id
      - entry_type
      - fields
    properties:
      ref_id:
        type: string
        pattern: "^REF-[0-9]{3}(-[a-z]+)?$"

      entry_type:
        type: string
        enum:
          - article
          - inproceedings
          - book
          - incollection
          - techreport
          - misc
          - phdthesis
          - mastersthesis
        description: "BibTeX entry type"

      cite_key:
        type: string
        description: "Citation key (e.g., FAIR2016)"

      fields:
        type: object
        properties:
          title:
            type: string
          author:
            type: string
            description: "Authors in BibTeX format"
          year:
            type: string
          month:
            type: string
          journal:
            type: string
          booktitle:
            type: string
          publisher:
            type: string
          volume:
            type: string
          number:
            type: string
          pages:
            type: string
          doi:
            type: string
          url:
            type: string
          arxiv:
            type: string
          abstract:
            type: string
          keywords:
            type: string

      aiwg_metadata:
        type: object
        properties:
          quality:
            type: string
          relevance:
            type: string
          categories:
            type: array
            items:
              type: string
        description: "AIWG-specific metadata (as BibTeX keywords)"

# Format templates
format_templates:
  bibtex:
    template: |
      @{{entry_type}}{{{cite_key}},
        author = {{{fields.author}}},
        title = {{{fields.title}}},
        year = {{{fields.year}}},
        {{#if fields.journal}}journal = {{{fields.journal}}},{{/if}}
        {{#if fields.booktitle}}booktitle = {{{fields.booktitle}}},{{/if}}
        {{#if fields.volume}}volume = {{{fields.volume}}},{{/if}}
        {{#if fields.pages}}pages = {{{fields.pages}}},{{/if}}
        {{#if fields.doi}}doi = {{{fields.doi}}},{{/if}}
        {{#if fields.url}}url = {{{fields.url}}},{{/if}}
        keywords = {aiwg-{{aiwg_metadata.relevance}}}
      }

  csl_json:
    description: "Citation Style Language JSON format"
    structure: |
      {
        "id": "REF-XXX",
        "type": "article-journal",
        "title": "...",
        "author": [{"family": "...", "given": "..."}],
        "issued": {"date-parts": [[2024]]},
        "DOI": "...",
        "URL": "..."
      }

  ris:
    template: |
      TY  - {{ris_type}}
      AU  - {{author_ris}}
      TI  - {{fields.title}}
      PY  - {{fields.year}}
      {{#if fields.journal}}JO  - {{fields.journal}}{{/if}}
      {{#if fields.doi}}DO  - {{fields.doi}}{{/if}}
      {{#if fields.url}}UR  - {{fields.url}}{{/if}}
      KW  - aiwg-{{aiwg_metadata.relevance}}
      ER  -

# Mapping tables
entry_type_mapping:
  peer_reviewed_journal: article
  peer_reviewed_conference: inproceedings
  preprint: misc
  technical_report: techreport
  book_chapter: incollection
  industry_whitepaper: misc
  standard: misc
  thesis: phdthesis

ris_type_mapping:
  article: JOUR
  inproceedings: CONF
  book: BOOK
  techreport: RPRT
  misc: GEN

# Agent protocol
agent_protocol:
  export_workflow:
    steps:
      - load_frontmatter_from_corpus
      - apply_filters
      - map_to_entry_type
      - generate_cite_keys
      - render_to_format
      - validate_output

  cite_key_generation:
    pattern: "{{first_author_last}}{{year}}{{title_word}}"
    example: "Wilkinson2016FAIR"
    collision_handling: append_letter  # Wilkinson2016a, Wilkinson2016b

# Examples
examples:
  bibtex_output: |
    @article{Wilkinson2016FAIR,
      author = {Wilkinson, Mark D. and Dumontier, Michel and others},
      title = {The FAIR Guiding Principles for scientific data management and stewardship},
      year = {2016},
      journal = {Scientific Data},
      volume = {3},
      doi = {10.1038/sdata.2016.18},
      keywords = {aiwg-critical}
    }

    @inproceedings{Hong2023MetaGPT,
      author = {Hong, Sirui and others},
      title = {MetaGPT: Meta Programming for Multi-Agent Collaboration},
      year = {2023},
      booktitle = {arXiv preprint},
      arxiv = {2308.00352},
      keywords = {aiwg-critical}
    }

# References
references:
  research:
    - "@.aiwg/research/findings/REF-061-oais.md"
  implementation:
    - "#109"
    - "#121"
  related:
    - "@agentic/code/frameworks/sdlc-complete/schemas/research/frontmatter-schema.yaml"
