/**
 * @description Domain class for {SObject}.
 *              Encapsulates field-level defaults, derivations, and validations.
 *              Operates only on in-memory SObject data — no SOQL or DML.
 * @author Generated by Apex Class Writer Skill
 */
public with sharing class {SObject}Domain {

    // ─── Constants ───────────────────────────────────────────────────────
    // TODO: Add constants for default values, statuses, etc.

    // ─── Field Defaults ──────────────────────────────────────────────────

    /**
     * @description Applies default field values to new {SObject} records.
     *              Call this before insert to ensure consistent defaults.
     * @param records List of {SObject} records to apply defaults to
     */
    public static void applyDefaults(List<{SObject}> records) {
        if (records == null || records.isEmpty()) {
            return;
        }

        for ({SObject} record : records) {
            // TODO: Set default field values
            // Example:
            // if (record.Status__c == null) {
            //     record.Status__c = DEFAULT_STATUS;
            // }
        }
    }

    // ─── Derivations ────────────────────────────────────────────────────

    /**
     * @description Derives calculated field values based on other fields.
     *              Call this before insert and before update.
     * @param records List of {SObject} records to derive values for
     */
    public static void deriveFields(List<{SObject}> records) {
        if (records == null || records.isEmpty()) {
            return;
        }

        for ({SObject} record : records) {
            // TODO: Derive calculated field values
            // Example:
            // record.FullAddress__c = buildFullAddress(record);
        }
    }

    // ─── Validations ────────────────────────────────────────────────────

    /**
     * @description Validates {SObject} records and adds errors for any violations.
     *              Call this before insert and before update.
     * @param records List of {SObject} records to validate
     */
    public static void validate(List<{SObject}> records) {
        if (records == null || records.isEmpty()) {
            return;
        }

        for ({SObject} record : records) {
            // TODO: Add validation rules
            // Example:
            // if (String.isBlank(record.Name)) {
            //     record.addError('Name is required.');
            // }
        }
    }

    // ─── Comparisons ────────────────────────────────────────────────────

    /**
     * @description Determines which fields have changed between old and new record versions.
     *              Useful in before update context.
     * @param oldRecord The previous version of the record
     * @param newRecord The current version of the record
     * @return Set of field API names that have changed
     */
    public static Set<String> getChangedFields({SObject} oldRecord, {SObject} newRecord) {
        Set<String> changedFields = new Set<String>();

        if (oldRecord == null || newRecord == null) {
            return changedFields;
        }

        Map<String, Schema.SObjectField> fieldMap = Schema.SObjectType.{SObject}.fields.getMap();
        for (String fieldName : fieldMap.keySet()) {
            if (oldRecord.get(fieldName) != newRecord.get(fieldName)) {
                changedFields.add(fieldName);
            }
        }

        return changedFields;
    }

    // ─── Private Helpers ─────────────────────────────────────────────────

    // TODO: Add private helper methods as needed
}
