% Copyright 2026 Open-Guji (https://github.com/open-guji)
%
% Licensed under the Apache License, Version 2.0 (the "License");
% you may not use this file except in compliance with the License.
% You may obtain a copy of the License at
%
%     http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS,
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
% See the License for the specific language governing permissions and
% limitations under the License.

\RequirePackage{core/luatex-cn-core-base}
\ProvidesExplPackage{core/luatex-cn-core-metadata}{2026/02/26} {0.3.1}{Book metadata management}

% Load Lua module
\lua_now:n { vertical_metadata = require('core.luatex-cn-core-metadata') }

% ============================================================================
% Centralized variables for book metadata
% ============================================================================
\tl_new:N \l__luatexcn_metadata_book_name_tl
\tl_new:N \l__luatexcn_metadata_chapter_title_tl
\tl_new:N \l__luatexcn_metadata_publisher_tl

% ============================================================================
% Key-Value Interface
% ============================================================================
\keys_define:nn { luatexcn / metadata }
  {
    book-name .tl_set:N = \l__luatexcn_metadata_book_name_tl,
    book-name .initial:n = {},

    chapter-title .tl_set:N = \l__luatexcn_metadata_chapter_title_tl,
    chapter-title .initial:n = {},

    publisher .tl_set:N = \l__luatexcn_metadata_publisher_tl,
    publisher .initial:n = {},
  }

% ============================================================================
% Setup Command
% ============================================================================
\NewDocumentCommand{\metadataSetup}{ m }
  {
    \keys_set:nn { luatexcn / metadata } { #1 }
    \lua_now:e {
      vertical_metadata.setup({
        book_name~=~[=[\luaescapestring{\l__luatexcn_metadata_book_name_tl}]=],
        chapter_title~=~[=[\luaescapestring{\l__luatexcn_metadata_chapter_title_tl}]=],
        publisher~=~[=[\luaescapestring{\l__luatexcn_metadata_publisher_tl}]=]
      })
    }
  }

% ============================================================================
% Document Metadata Commands
% ============================================================================
% \title - Set book title (also sets banxin book-name)
% Uses RenewDocumentCommand since article class defines \title
\RenewDocumentCommand{\title}{ m }
  {
    \tl_set:Nn \l__luatexcn_metadata_book_name_tl { #1 }
    % Forward to banxin if available
    \cs_if_exist:NT \l__luatexcn_banxin_book_name_tl
      { \keys_set:nn { luatexcn / banxin } { book-name = {#1} } }
    % Set LaTeX's @title for compatibility
    \gdef\@title{#1}
  }

% \publisher - Set publisher name (also sets banxin publisher)
\NewDocumentCommand{\publisher}{ m }
  {
    \tl_set:Nn \l__luatexcn_metadata_publisher_tl { #1 }
    % Forward to banxin if available
    \cs_if_exist:NT \l__luatexcn_banxin_publisher_tl
      { \keys_set:nn { luatexcn / banxin } { publisher = {#1} } }
  }

% ============================================================================
% Chapter Marker Command
% ============================================================================
% Insert a chapter marker that will trigger chapter title change on subsequent pages
\NewDocumentCommand{\ChapterMarker}{ m }
  {
    \lua_now:e { vertical_metadata.insert_chapter_marker_to_box([=[\luaescapestring{#1}]=]) }
    \box_use:N \c_zero_int
  }

% ============================================================================
% Chapter Command
% ============================================================================
% Sets chapter title in banxin and metadata, then resets page number
\DeclareDocumentCommand{\chapter}{ m }
  {
    \keys_set:nn { luatexcn / banxin } { chapter-title = {#1} }
    % Also set global metadata for layout phase (fixes issue #45 - chapter not showing on first page)
    \metadataSetup{ chapter-title = {#1} }
    \ResetPageNumber
  }

% ============================================================================
% CJK Key Aliases (中文键值别名)
% ============================================================================
\keys_define:nn { luatexcn / metadata }
  {
    书名 .tl_set:N = \l__luatexcn_metadata_book_name_tl,
    書名 .tl_set:N = \l__luatexcn_metadata_book_name_tl,
    章节标题 .tl_set:N = \l__luatexcn_metadata_chapter_title_tl,
    章節標題 .tl_set:N = \l__luatexcn_metadata_chapter_title_tl,
    出版商 .tl_set:N = \l__luatexcn_metadata_publisher_tl,
  }

\ExplSyntaxOff

% ============================================================
% Chinese aliases / 中文别名
% ============================================================
% Simplified Chinese / 简体
\NewCommandCopy{\元数据设置}{\metadataSetup}
\NewCommandCopy{\章节标记}{\ChapterMarker}
\NewCommandCopy{\出版商}{\publisher}
% Traditional Chinese / 繁体
\NewCommandCopy{\元數據設置}{\metadataSetup}
\NewCommandCopy{\章節標記}{\ChapterMarker}

\endinput
