# #region orchestrator_config [C:3] [TYPE Module] [SEMANTICS hash, config, snapshot, translate]
# @BRIEF Config hash and dictionary snapshot hash utilities for translation planning.
# @RELATION DEPENDS_ON -> [TranslationJob]
# @RELATION DEPENDS_ON -> [TranslationJobDictionary]

import hashlib
import json


# #region compute_config_hash [C:2] [TYPE Function] [SEMANTICS config, hash, deterministic]
# @BRIEF Compute a deterministic hash of job configuration for snapshot comparison.
# @PRE job has valid configuration attributes.
# @POST Returns 16-char hex SHA-256 hash.
def compute_config_hash(job) -> str:
    """Compute a hash of the job's current configuration for snapshot comparison."""
    config_str = json.dumps({
        "source_dialect": job.source_dialect,
        "target_dialect": job.target_dialect,
        "source_datasource_id": job.source_datasource_id,
        "translation_column": job.translation_column,
        "context_columns": job.context_columns,
        "target_languages": sorted(job.target_languages) if job.target_languages else [],
        "provider_id": job.provider_id,
        "batch_size": job.batch_size,
        "upsert_strategy": job.upsert_strategy,
    }, sort_keys=True)
    return hashlib.sha256(config_str.encode()).hexdigest()[:16]
# #endregion compute_config_hash


# #region compute_dict_snapshot_hash [C:2] [TYPE Function] [SEMANTICS dictionary, hash, snapshot]
# @BRIEF Compute a hash of dictionary state for snapshot comparison.
# @PRE db_session is valid.
# @POST Returns 16-char hex SHA-256 hash of concatenated dictionary IDs.
def compute_dict_snapshot_hash(db_session, job_id: str) -> str:
    """Compute a hash of dictionary state for snapshot comparison."""
    from ...models.translate import TranslationJobDictionary
    dict_links = (
        db_session.query(TranslationJobDictionary)
        .filter(TranslationJobDictionary.job_id == job_id)
        .all()
    )
    dict_ids = sorted([dl.dictionary_id for dl in dict_links])
    hash_input = ",".join(dict_ids)
    return hashlib.sha256(hash_input.encode()).hexdigest()[:16]
# #endregion compute_dict_snapshot_hash
# #endregion orchestrator_config
