import { SerializationClass } from '@humand-packages/common';
import { CategorizationExplanation } from '../../business/models/timeTracking/categorizationExplanation';
import { RunNode } from '../../business/models/timeTracking/ruleAst';
import { TimeTrackingCategorizedHourStatus } from '../../infrastructure/constants/timeTracking/categorizedHourStatus';

/**
 * Per stored hour shown alongside the AST. The full user enrichment in
 * `TimeTrackingCategorizedHoursSC` is intentionally omitted: the explain endpoint is
 * already scoped to one (user, date), so the UI knows the user. Status-override fields
 * are included so the UI can surface admin overrides (which the AST does not encode).
 */
export interface CategorizedHourSummaryDTO {
    id: string;
    hourCategoryId: number;
    hourCategoryName: string;
    hours: number;
    status: TimeTrackingCategorizedHourStatus;
    note: string | null;
    statusOverriddenByAdminId: number | null;
    statusOverrideComment: string | null;
}

export interface CategorizationExplanationDTO {
    run: RunNode;
    categorizedHours: CategorizedHourSummaryDTO[];
    computedAt: string;
}

export class CategorizationExplanationSC extends SerializationClass<CategorizationExplanation, CategorizationExplanationDTO> {
    serialize(explanation: CategorizationExplanation): CategorizationExplanationDTO {
        return {
            run: explanation.run,
            categorizedHours: explanation.categorizedHours.map((ch) => ({
                id: ch.id,
                hourCategoryId: ch.hourCategoryId,
                hourCategoryName: ch.hourCategoryName,
                hours: ch.hours,
                status: ch.status,
                note: ch.note,
                statusOverriddenByAdminId: ch.statusOverriddenByAdminId,
                statusOverrideComment: ch.statusOverrideComment,
            })),
            computedAt: explanation.computedAt.toISOString(),
        };
    }
}
