// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Course AI i18n loader.
 *
 * @module     local_coursegen/local/courseai/i18n
 * @copyright  2026 Wilber Narvaez <https://datacurso.com>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

import {get_strings, get_string} from 'core/str';

const STRING_KEYS = [
    'courseai_header_title',
    'courseai_header_subtitle',
    'courseai_step_context',
    'courseai_step_planning',
    'courseai_step_generating',
    'courseai_context_title',
    'courseai_context_subtitle',
    'courseai_prompt_placeholder',
    'courseai_prompt_arialabel',
    'courseai_chip_remove_syllabus',
    'courseai_chip_view_guideline',
    'courseai_chip_remove_guideline',
    'courseai_btn_syllabus',
    'courseai_btn_syllabus_title',
    'courseai_btn_guidelines',
    'courseai_btn_guidelines_title',
    'courseai_guidelines_dialog_label',
    'courseai_guidelines_search_placeholder',
    'courseai_guidelines_note_one',
    'courseai_guidelines_list_label',
    'courseai_lang_title',
    'courseai_lang_arialabel',
    'courseai_images_title',
    'courseai_images_label',
    'courseai_images_arialabel',
    'courseai_subsections_title',
    'courseai_subsections_label',
    'courseai_subsections_arialabel',
    'courseai_subsection_label',
    'courseai_subsections_decision_title',
    'courseai_subsections_decision_enable',
    'courseai_subsections_decision_continue',
    'courseai_subsections_decision_exit',
    'courseai_subsections_structure_other',
    'courseai_subsections_structure_send',
    'courseai_subsections_confirm',
    'courseai_subsections_back',
    'courseai_hint_enter',
    'courseai_btn_generate',
    'courseai_form_tip',
    'courseai_progress_label',
    'courseai_toggle_details',
    'courseai_prv_sub_init',
    'courseai_prv_live_note',
    'courseai_plan_actions_hint',
    'courseai_btn_adjust',
    'courseai_btn_retry',
    'courseai_btn_approve',
    'courseai_adjust_placeholder',
    'courseai_btn_cancel',
    'courseai_btn_discard',
    'courseai_delete_section_confirm_title',
    'courseai_delete_section_confirm_body',
    'courseai_delete_activity_confirm_title',
    'courseai_delete_activity_confirm_body',
    'courseai_btn_send_adjust',
    'courseai_btn_back_context',
    'courseai_btn_cancel_flow',
    'courseai_btn_cancel_and_exit',
    'courseai_modal_close',
    'courseai_modal_subtitle_text',
    'courseai_modal_fullcontext',
    'courseai_no_results',
    'courseai_category_general',
    'courseai_state_planning',
    'courseai_state_starting',
    'courseai_state_structuring',
    'courseai_state_completed',
    'courseai_state_error',
    'courseai_progress_percent',
    'courseai_plan_counter',
    'courseai_plan_adding',
    'courseai_plan_sections_counter',
    'courseai_plan_review_title',
    'courseai_plan_review_subtitle',
    'courseai_plan_detailed_title',
    'courseai_plan_detailed_subtitle',
    'courseai_plan_detailed_stats',
    'courseai_plan_review_hint_detailed',
    'courseai_plan_detailed_done_title',
    'courseai_plan_detailed_done_subtitle',
    'courseai_plan_detailed_markdown_title',
    'courseai_plan_detailed_markdown_subtitle',
    'courseai_generating_details',
    'courseai_generating_details_for',
    'courseai_finalizing_course',
    'courseai_section_label',
    'courseai_section_progress_with_total',
    'courseai_section_progress_no_total',
    'courseai_activities_count',
    'courseai_processing_status',
    'courseai_error_stream_url',
    'courseai_error_connection',
    'courseai_error_create_course',
    'courseai_error_init_session',
    'courseai_error_upload_syllabus',
    'courseai_error_send_feedback',
    'courseai_error_init_filepicker',
    'courseai_error_generic',
    'courseai_generate_starting',
    'courseai_generate_uploading_syllabus',
    'courseai_course_creating',
    'courseai_course_creating_subtitle',
    'courseai_completion_title',
    'courseai_completion_summary_default',
    'courseai_completion_summary_no_images',
    'courseai_completion_summary_with_images',
    'courseai_completion_btn_open_course',
    'courseai_completion_btn_create_another',
    'courseai_back_to_context',
    'courseai_live_note_detailed',
    'courseai_activity_default',
    'courseai_activity_quiz',
    'courseai_activity_book',
    'courseai_activity_assign',
    'courseai_activity_forum',
    'courseai_activity_lesson',
    'courseai_activity_url',
    'courseai_activity_resource',
    'courseai_activity_page',
    'courseai_activity_data',
    'courseai_activity_glossary',
    'courseai_activity_h5pactivity',
    'courseai_plan_default_unnamed',
    'courseai_status_approving',
    'courseai_status_adjusting',
    'courseai_btn_regenerate',
    'courseai_btn_pause',
    'courseai_btn_stop',
    'courseai_btn_resume',
    'courseai_chapters_label',
    'courseai_questions_label',
    'courseai_notes_label',
    'courseai_pages_label',
    'courseai_discussions_label',
    'courseai_entries_label',
    'courseai_options_label',
    'courseai_images_suggested_label',
    'courseai_image_count_one',
    'courseai_image_count_many',
    'courseai_review_title',
    'courseai_review_subtitle',
    'courseai_review_step_label',
    'courseai_review_fullname_label',
    'courseai_review_fullname_placeholder',
    'courseai_review_shortname_label',
    'courseai_review_shortname_placeholder',
    'courseai_review_shortname_note',
    'courseai_review_category_label',
    'courseai_review_category_loading',
    'courseai_review_cancel',
    'courseai_review_confirm',
    'courseai_proposals_title',
    'courseai_proposals_clarification_label',
    'courseai_proposals_other_label',
    'courseai_proposals_other_placeholder',
    'courseai_btn_execute_proposal',
    'courseai_btn_discard_proposals',
    'courseai_proposals_fallen_label',
    'courseai_proposals_destructive_badge',
    'courseai_btn_add_section',
    'courseai_btn_add_activity',
    'courseai_btn_add_subsection',
    'courseai_btn_add',
    'courseai_menu_activity_or_resource',
    'courseai_add_section_placeholder',
    'courseai_add_activity_placeholder',
    'courseai_add_subsection_placeholder',
    'courseai_drag_handle_label',
    'courseai_resize_panels',
    'courseai_log_label',
    'courseai_log_user_request',
    'courseai_log_deleted_section',
    'courseai_log_deleted_activity',
    'courseai_log_regenerated_section',
    'courseai_log_regenerated_activity',
    'courseai_log_added_section',
    'courseai_log_added_activity',
    'courseai_log_image_discarded',
    'courseai_log_image_regenerated',
    'courseai_log_proposal_applied',
    'courseai_log_proposals_dismissed',
    'courseai_log_ai_thinking',
    'courseai_log_ai_working',
    'courseai_log_ai_course_configuration',
    'courseai_log_ai_review_ready',
    'courseai_log_ai_proposals_ready',
    'courseai_log_ai_completed',
    'courseai_log_user_approved',
    'courseai_log_reordered_sections',
    'courseai_log_moved_section',
    'courseai_log_added_section_named',
    'courseai_log_added_activity_named',
    'courseai_log_add_activity_target',
    'courseai_log_add_section_target',
    'proposal_add_activity',
    'proposal_add_section',
    'proposal_add_subsection',
    'courseai_log_reordered_activities',
    'courseai_log_reordered_activities_generic',
    'courseai_log_moved_activity',
    'courseai_log_ai_review_updated',
    'courseai_show_more',
    'courseai_show_less',
    'courseai_show_full_message',
    'courseai_log_ai_planned_structure',
    'courseai_checklist_section_count',
    'courseai_decision_title',
    'courseai_decision_accept',
    'courseai_decision_adjust',
    // Server-side thread log string_ids (match the service Message names). Prefetched
    // so replayThread localizes the full transcript on reload without N round-trips.
    'ai_planned_structure',
    'ai_course_configuration',
    'log_user_approved',
    'log_user_adjusted',
    'log_proposal_applied',
    'log_proposal_custom',
    'log_proposals_dismissed',
    'log_user_stopped',
    'log_user_resumed',
    'log_add_section',
    'log_add_subsection',
    'log_delete_section',
    'log_delete_section_named',
    'log_reorder_sections',
    'log_moved_section',
    'log_replan_section',
    'log_add_activity',
    'log_delete_activity',
    'log_delete_activity_named',
    'log_reorder_activities',
    'log_moved_activity',
    'log_move_activity_to',
    'proposal_move_activity',
    'log_replan_activity',
    'log_discard_image',
    'log_replan_image',
    'course_completed',
    'course_failed',
    'review_plan_detailed',
];

/**
 * Load all courseai strings from lang pack.
 *
 * @returns {Promise<Object>}
 */
export const loadCourseaiStrings = async() => {
    const values = await get_strings(STRING_KEYS.map((key) => ({key, component: 'local_coursegen'})));
    return Object.fromEntries(STRING_KEYS.map((key, i) => [key, values[i]]));
};

/**
 * Localize a backend message into the user's language.
 *
 * The backend streams every human-readable message as
 * `{string_id, string, string_args}`. We localize by `string_id` against the
 * plugin's lang pack (the catalog is mirrored 1:1), passing `string_args` as
 * the Moodle `$a` object. When the key is missing locally (e.g. a brand-new
 * backend string not yet shipped in this lang pack), we fall back to the
 * server-rendered `string`.
 *
 * @param {Object} message - { string_id, string, string_args? }
 * @returns {Promise<string>}
 */
export const localizeMessage = async(message) => {
    if (!message || !message.string_id) {
        return (message && message.string) || '';
    }
    try {
        const text = await get_string(message.string_id, 'local_coursegen', message.string_args || null);
        // Moodle renders "[[key]]" for an unknown string id: prefer the server text.
        if (typeof text === 'string' && text.startsWith('[[')) {
            return message.string || text;
        }
        return text;
    } catch (e) {
        return message.string || '';
    }
};
