// SPDX-FileCopyrightText: 2025 Olivier 'reivilibre'
//
// SPDX-License-Identifier: GPL-3.0-or-later

use super::{gather_comments::TotalHarvest, intermediate::IntermediateSchema};

/// Filters out (removes) comments in the harvest, if:
/// - the same comment already exists on the object in the schema; or
/// - the object doesn't exist in the schema
///
/// # LLMs
/// Partially generated by LLM.
pub fn filter_harvested_by_schema_comparison(
    harvest: &mut TotalHarvest,
    schema: &IntermediateSchema,
) {
    // Remove tables that don't exist in the schema
    harvest
        .tables
        .retain(|table_name, _| schema.tables.contains_key(table_name));

    // For each remaining table, filter comments
    for (table_name, table) in &mut harvest.tables {
        let schema_table = schema.tables.get(table_name).unwrap();

        // Remove table comment if it already exists in schema
        if let Some(doc_comment) = &table.doc_comment {
            if !schema_table.comment.is_empty() && schema_table.comment == doc_comment.comment {
                table.doc_comment = None;
            }
        }

        // Remove columns that don't exist in schema or have duplicate comments
        table.columns.retain(|col_name, column| {
            let Some(schema_column) = schema_table.columns.iter().find(|c| c.name == *col_name)
            else {
                return false; // Column doesn't exist in schema - remove it
            };
            // Column exists in schema - keep it only if its comment is different
            let Some(doc_comment) = &column.doc_comment else {
                return true; // No comment to filter
            };
            schema_column.comment.is_empty() || schema_column.comment != doc_comment.comment
        });
    }

    // Remove tables that have no comments and no columns with comments left
    harvest.tables.retain(|_, table| {
        table.doc_comment.is_some() || table.columns.values().any(|col| col.doc_comment.is_some())
    });
}
